Prechádzať zdrojové kódy

:robot: Add alive scrub test, increase default scrub time

Ettore Di Giacinto 3 rokov pred
rodič
commit
49d1be4841
3 zmenil súbory, kde vykonal 78 pridanie a 12 odobranie
  1. 6 6
      cmd/main.go
  2. 3 2
      pkg/services/alive.go
  3. 69 4
      pkg/services/alive_test.go

+ 6 - 6
cmd/main.go

@@ -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"
@@ -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",
@@ -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...)
 		}
 

+ 3 - 2
pkg/services/alive.go

@@ -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 {
@@ -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)

+ 69 - 4
pkg/services/alive_test.go

@@ -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)
@@ -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))
+
 		})
 	})
 })