Skip to content

Commit

Permalink
🤖 Add alive scrub test, increase default scrub time
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed Jan 24, 2022
1 parent 0e26c2c commit 49d1be4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
12 changes: 6 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package cmd
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"time"
"net"

"github.com/mudler/edgevpn/api"
edgevpn "github.com/mudler/edgevpn/pkg/node"
Expand Down Expand Up @@ -65,8 +65,8 @@ func MainFlags() []cli.Flag {
Usage: "API listening port",
},
&cli.BoolFlag{
Name: "dhcp",
Usage: "Enables p2p ip negotiation (experimental)",
Name: "dhcp",
Usage: "Enables p2p ip negotiation (experimental)",
},
&cli.StringFlag{
Name: "lease-dir",
Expand Down Expand Up @@ -107,14 +107,14 @@ func Main() func(c *cli.Context) error {
}
o, vpnOpts, ll := cliToOpts(c)

o = append(o, services.Alive(30*time.Second)...)
o = append(o, services.Alive(30*time.Second, 10*time.Minute)...)
if c.Bool("dhcp") {
address, _, err := net.ParseCIDR(c.String("address"));
address, _, err := net.ParseCIDR(c.String("address"))
if err != nil {
return err
}
nodeOpts, vO := vpn.DHCP(ll, 10*time.Second, c.String("lease-dir"), address.String())
o = append(o, nodeOpts...,)
o = append(o, nodeOpts...)
vpnOpts = append(vpnOpts, vO...)
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/services/alive.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/mudler/edgevpn/pkg/blockchain"
)

func Alive(announcetime time.Duration) []node.Option {
func Alive(announcetime, scrubTime time.Duration) []node.Option {
return []node.Option{
node.WithNetworkService(
func(ctx context.Context, c node.Config, n *node.Node, b *blockchain.Ledger) error {
Expand All @@ -44,10 +44,11 @@ func Alive(announcetime time.Duration) []node.Option {
// Keep-alive scrub
nodes := AvailableNodes(b)
lead := utils.Leader(nodes)
if !t.Add(2 * time.Minute).After(time.Now()) {
if !t.Add(scrubTime).After(time.Now()) {
// Update timer so not-leader do not attempt to delete bucket afterwards
// prevent cycles
t = time.Now()

if lead == n.Host().ID().String() {
// Automatically scrub after some time passed
b.DeleteBucket(protocol.HealthCheckKey)
Expand Down
73 changes: 69 additions & 4 deletions pkg/services/alive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ var _ = Describe("Alive service", func() {
l := node.Logger(logg)

opts := append(
Alive(5*time.Second),
Alive(5*time.Second, 100*time.Second),
node.FromBase64(true, true, token),
l)
e2 := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
e1 := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)

Context("Aliveness check", func() {
It("detect both nodes alive after a while", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e2 := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
e1 := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)

e1.Start(ctx)
e2.Start(ctx)
Expand All @@ -54,13 +54,78 @@ var _ = Describe("Alive service", func() {

ll.Persist(ctx, 1*time.Second, 100*time.Second, "t", "t", "test")

matches := And(ContainElement(e2.Host().ID().String()),
ContainElement(e1.Host().ID().String()))

index := ll.LastBlock().Index
Eventually(func() []string {
ll, err := e1.Ledger()
if err != nil {
return []string{}
}
return AvailableNodes(ll)
}, 100*time.Second, 1*time.Second).Should(ContainElement(e2.Host().ID().String()))
}, 100*time.Second, 1*time.Second).Should(matches)

Expect(ll.LastBlock().Index).ToNot(Equal(index))
})
})

Context("Aliveness Scrub", func() {
BeforeEach(func() {
opts = append(
Alive(2*time.Second, 4*time.Second),
node.FromBase64(true, true, token),
l)
})

It("cleans up after a while", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e2 := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
e1 := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)

e1.Start(ctx)
e2.Start(ctx)

ll, _ := e1.Ledger()

ll.Persist(ctx, 1*time.Second, 100*time.Second, "t", "t", "test")

matches := And(ContainElement(e2.Host().ID().String()),
ContainElement(e1.Host().ID().String()))

index := ll.LastBlock().Index
Eventually(func() []string {
ll, err := e1.Ledger()
if err != nil {
return []string{}
}
return AvailableNodes(ll)
}, 100*time.Second, 1*time.Second).Should(matches)

Expect(ll.LastBlock().Index).ToNot(Equal(index))
index = ll.LastBlock().Index

Eventually(func() []string {
ll, err := e1.Ledger()
if err != nil {
return []string{}
}
return AvailableNodes(ll)
}, 30*time.Second, 1*time.Second).Should(BeEmpty())

Expect(ll.LastBlock().Index).ToNot(Equal(index))
index = ll.LastBlock().Index

Eventually(func() []string {
ll, err := e1.Ledger()
if err != nil {
return []string{}
}
return AvailableNodes(ll)
}, 10*time.Second, 1*time.Second).Should(matches)
Expect(ll.LastBlock().Index).ToNot(Equal(index))

})
})
})

0 comments on commit 49d1be4

Please sign in to comment.