Browse Source

:robot: Tests fixups

Ettore Di Giacinto 3 years ago
parent
commit
37be32339c

+ 1 - 1
.github/tests.sh

@@ -15,4 +15,4 @@ go install github.com/onsi/ginkgo/v2/ginkgo
 
 export TEST_INSTANCE="http://localhost:8080"
 
-ginkgo -r --flake-attempts 3 --coverprofile=coverage.txt --covermode=atomic --race ./pkg/... ./api/...
+ginkgo -v -r --flake-attempts 5 --coverprofile=coverage.txt --covermode=atomic --race ./pkg/... ./api/...

+ 29 - 29
.github/vpntest.sh

@@ -1,8 +1,8 @@
 #!/bin/bash
 
-echo "Creating big file to send over"
-dd if=/dev/urandom of=big_file bs=1G count=2 iflag=fullblock
-sha256sum big_file > big_file.sha256
+# echo "Creating big file to send over"
+# dd if=/dev/urandom of=big_file bs=1G count=2 iflag=fullblock
+# sha256sum big_file > big_file.sha256
 
 ((count = 100))                        
 while [[ $count -ne 0 ]] ; do
@@ -21,35 +21,35 @@ else
     exit 1
 fi
 
-host=$1
+# host=$1
 
-if [ "$3" == "download" ]; then
-    set -e
-    echo "Downloading big file"
-    curl -v -L $host/big_file -O big_file
-    curl -v -L $host/big_file.sha256 -O big_file.sha256
+# if [ "$3" == "download" ]; then
+#     set -e
+#     echo "Downloading big file"
+#     curl -v -L $host/big_file -O big_file
+#     curl -v -L $host/big_file.sha256 -O big_file.sha256
 
-    echo "Verifying checksum"
-    sha256sum -c "big_file.sha256"
+#     echo "Verifying checksum"
+#     sha256sum -c "big_file.sha256"
 
-    curl -X PUT http://localhost:8080/api/ledger/tests/vpn$2
-    sleep 30
-    curl -X PUT http://localhost:8080/api/ledger/tests/vpn$2
+#     curl -X PUT http://localhost:8080/api/ledger/tests/vpn$2
+#     sleep 30
+#     curl -X PUT http://localhost:8080/api/ledger/tests/vpn$2
 
-    sleep 30
+#     sleep 30
 
-else
+# else
+
+#     set +e
+#     ((count = 640))                        
+#     while [[ $count -ne 0 ]] ; do
+#         sleep 5
+#         curl http://localhost:8080/api/ledger/tests/vpn$1 | grep "24"
+#         rc=$?
+#         if [[ $rc -eq 0 ]] ; then
+#             ((count = 1))
+#         fi
+#         ((count = count - 1))
+#     done
 
-    set +e
-    ((count = 640))                        
-    while [[ $count -ne 0 ]] ; do
-        sleep 5
-        curl http://localhost:8080/api/ledger/tests/vpn$1 | grep "24"
-        rc=$?
-        if [[ $rc -eq 0 ]] ; then
-            ((count = 1))
-        fi
-        ((count = count - 1))
-    done
-
-fi
+# fi

+ 3 - 3
pkg/node/node_test.go

@@ -64,8 +64,8 @@ var _ = Describe("Node", func() {
 			ctx, cancel := context.WithCancel(context.Background())
 			defer cancel()
 
-			e, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), l)
-			e2, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), l)
+			e, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), WithDiscoveryInterval(10*time.Second), l)
+			e2, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), WithDiscoveryInterval(10*time.Second), l)
 
 			e.Start(ctx)
 			e2.Start(ctx)
@@ -75,7 +75,7 @@ var _ = Describe("Node", func() {
 			l2, err := e2.Ledger()
 			Expect(err).ToNot(HaveOccurred())
 
-			l.Announce(ctx, 1*time.Second, func() { l.Add("foo", map[string]interface{}{"bar": "baz"}) })
+			l.Announce(ctx, 2*time.Second, func() { l.Add("foo", map[string]interface{}{"bar": "baz"}) })
 
 			Eventually(func() string {
 				var s string

+ 8 - 6
pkg/services/alive_test.go

@@ -30,13 +30,14 @@ import (
 )
 
 var _ = Describe("Alive service", func() {
-	token := node.GenerateNewConnectionData().Base64()
+	token := node.GenerateNewConnectionData(25).Base64()
 
 	logg := logger.New(log.LevelError)
 	l := node.Logger(logg)
 
 	opts := append(
 		Alive(5*time.Second, 100*time.Second, 15*time.Minute),
+		node.WithDiscoveryInterval(10*time.Second),
 		node.FromBase64(true, true, token),
 		l)
 
@@ -52,7 +53,7 @@ var _ = Describe("Alive service", func() {
 
 			ll, _ := e1.Ledger()
 
-			ll.Persist(ctx, 1*time.Second, 100*time.Second, "t", "t", "test")
+			ll.Persist(ctx, 5*time.Second, 100*time.Second, "t", "t", "test")
 
 			matches := And(ContainElement(e2.Host().ID().String()),
 				ContainElement(e1.Host().ID().String()))
@@ -73,7 +74,8 @@ var _ = Describe("Alive service", func() {
 	Context("Aliveness Scrub", func() {
 		BeforeEach(func() {
 			opts = append(
-				Alive(2*time.Second, 4*time.Second, 15*time.Minute),
+				Alive(5*time.Second, 20*time.Second, 15*time.Minute),
+				node.WithDiscoveryInterval(10*time.Second),
 				node.FromBase64(true, true, token),
 				l)
 		})
@@ -89,7 +91,7 @@ var _ = Describe("Alive service", func() {
 
 			ll, _ := e1.Ledger()
 
-			ll.Persist(ctx, 1*time.Second, 100*time.Second, "t", "t", "test")
+			ll.Persist(ctx, 5*time.Second, 100*time.Second, "t", "t", "test")
 
 			matches := And(ContainElement(e2.Host().ID().String()),
 				ContainElement(e1.Host().ID().String()))
@@ -112,7 +114,7 @@ var _ = Describe("Alive service", func() {
 					return []string{}
 				}
 				return AvailableNodes(ll, 15*time.Minute)
-			}, 30*time.Second, 1*time.Second).Should(BeEmpty())
+			}, 50*time.Second, 1*time.Second).Should(BeEmpty())
 
 			Expect(ll.LastBlock().Index).ToNot(Equal(index))
 			index = ll.LastBlock().Index
@@ -123,7 +125,7 @@ var _ = Describe("Alive service", func() {
 					return []string{}
 				}
 				return AvailableNodes(ll, 15*time.Minute)
-			}, 10*time.Second, 1*time.Second).Should(matches)
+			}, 30*time.Second, 1*time.Second).Should(matches)
 			Expect(ll.LastBlock().Index).ToNot(Equal(index))
 
 		})

+ 3 - 5
pkg/services/files.go

@@ -99,13 +99,13 @@ func ShareFile(ll log.StandardLogger, announcetime time.Duration, fileID, filepa
 
 func ReceiveFile(ctx context.Context, ledger *blockchain.Ledger, n *node.Node, l log.StandardLogger, announcetime time.Duration, fileID string, path string) error {
 	// Announce ourselves so nodes accepts our connection
-
 	ledger.Announce(
 		ctx,
 		announcetime,
 		func() {
 			// Retrieve current ID for ip in the blockchain
 			_, found := ledger.GetKey(protocol.UsersLedgerKey, n.Host().ID().String())
+
 			// If mismatch, update the blockchain
 			if !found {
 				updatedMap := map[string]interface{}{}
@@ -127,10 +127,6 @@ func ReceiveFile(ctx context.Context, ledger *blockchain.Ledger, n *node.Node, l
 
 			l.Debug("Attempting to find file in the blockchain")
 
-			_, found := ledger.GetKey(protocol.UsersLedgerKey, n.Host().ID().String())
-			if !found {
-				continue
-			}
 			existingValue, found := ledger.GetKey(protocol.FilesLedgerKey, fileID)
 			fi := &types.File{}
 			existingValue.Unmarshal(fi)
@@ -155,6 +151,8 @@ func ReceiveFile(ctx context.Context, ledger *blockchain.Ledger, n *node.Node, l
 					return err
 				}
 
+				l.Debug("file found on blockchain, opening stream to", d)
+
 				// Open a stream
 				stream, err := n.Host().NewStream(ctx, d, protocol.FileProtocol.ID())
 				if err != nil {

+ 12 - 7
pkg/services/files_test.go

@@ -17,6 +17,7 @@ package services_test
 
 import (
 	"context"
+	"fmt"
 	"io/ioutil"
 	"os"
 	"time"
@@ -32,12 +33,15 @@ import (
 )
 
 var _ = Describe("File services", func() {
-	token := node.GenerateNewConnectionData().Base64()
+	token := node.GenerateNewConnectionData(25).Base64()
 
 	logg := logger.New(log.LevelError)
 	l := node.Logger(logg)
 
-	e2, _ := node.New(node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
+	e2, _ := node.New(
+		node.WithDiscoveryInterval(10*time.Second),
+		node.WithNetworkService(AliveNetworkService(2*time.Second, 4*time.Second, 15*time.Minute)),
+		node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
 
 	Context("File sharing", func() {
 		It("sends and receive files between two nodes", func() {
@@ -54,7 +58,7 @@ var _ = Describe("File services", func() {
 			ioutil.WriteFile(f.Name(), []byte("testfile"), os.ModePerm)
 
 			// First node expose a file
-			opts, err := ShareFile(logg, 1*time.Second, fileUUID, f.Name())
+			opts, err := ShareFile(logg, 10*time.Second, fileUUID, f.Name())
 			Expect(err).ToNot(HaveOccurred())
 
 			opts = append(opts, node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
@@ -64,7 +68,7 @@ var _ = Describe("File services", func() {
 			e2.Start(ctx)
 
 			Eventually(func() string {
-				ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+				ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
 				defer cancel()
 
 				f, err := ioutil.TempFile("", "test")
@@ -73,11 +77,12 @@ var _ = Describe("File services", func() {
 				defer os.RemoveAll(f.Name())
 
 				ll, _ := e2.Ledger()
-
-				ReceiveFile(ctx, ll, e2, logg, 1*time.Second, fileUUID, f.Name())
+				ll1, _ := e.Ledger()
+				By(fmt.Sprint(ll.CurrentData(), ll.LastBlock().Index, ll1.CurrentData()))
+				ReceiveFile(ctx, ll, e2, logg, 2*time.Second, fileUUID, f.Name())
 				b, _ := ioutil.ReadFile(f.Name())
 				return string(b)
-			}, 100*time.Second, 1*time.Second).Should(Equal("testfile"))
+			}, 190*time.Second, 1*time.Second).Should(Equal("testfile"))
 		})
 	})
 })

+ 4 - 4
pkg/services/services_test.go

@@ -61,8 +61,8 @@ var _ = Describe("Expose services", func() {
 	serviceUUID := "test"
 
 	e2, _ := node.New(
-
-		node.WithNetworkService(ConnectNetworkService(1*time.Second, serviceUUID, "127.0.0.1:9999")),
+		node.WithNetworkService(ConnectNetworkService(5*time.Second, serviceUUID, "127.0.0.1:9999")),
+		node.WithDiscoveryInterval(10*time.Second),
 		node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
 
 	Context("Service sharing", func() {
@@ -70,8 +70,8 @@ var _ = Describe("Expose services", func() {
 			ctx, cancel := context.WithCancel(context.Background())
 			defer cancel()
 
-			opts := RegisterService(logg, 1*time.Second, serviceUUID, "142.250.184.35:80")
-			opts = append(opts, node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
+			opts := RegisterService(logg, 5*time.Second, serviceUUID, "142.250.184.35:80")
+			opts = append(opts, node.FromBase64(true, true, token), node.WithDiscoveryInterval(10*time.Second), node.WithStore(&blockchain.MemoryStore{}), l)
 			e, _ := node.New(opts...)
 
 			// First node expose a service