From c6dac0d37856d232e2ac5571b5ab11772e1db15a Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Wed, 23 Aug 2017 17:04:33 +0800 Subject: [PATCH] tests: test block refactoring and use wait methods --- tests/block_sync_test.go | 81 ++++++++++++++++-------------- tests/dynamic_test.go | 79 ++++++++++++++++++----------- tests/general_consensus_test.go | 45 ++++------------- tests/gossip_network_test.go | 19 +++---- tests/non_byzantine_faulty_test.go | 36 ++++--------- 5 files changed, 117 insertions(+), 143 deletions(-) diff --git a/tests/block_sync_test.go b/tests/block_sync_test.go index 782d839f..4483b5f0 100644 --- a/tests/block_sync_test.go +++ b/tests/block_sync_test.go @@ -96,54 +96,59 @@ var _ = Describe("Block synchronization testing", func() { It("TFS-06-02: Node synchronization", func(done Done) { const targetBlockHeight = 10 - By("Wait for blocks") - waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) { - Expect(geth.WaitForBlocks(targetBlockHeight)).To(BeNil()) - wg.Done() + By("Wait for blocks", func() { + waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) { + Expect(geth.WaitForBlocks(targetBlockHeight)).To(BeNil()) + wg.Done() + }) }) - By("Stop consensus") - for _, v := range blockchain.Validators() { - client := v.NewIstanbulClient() - Expect(client).NotTo(BeNil()) - err := client.StopMining(context.Background()) - Expect(err).To(BeNil()) - client.Close() - } - - By("Get target block") - latestBlock, err := blockchain.Validators()[0].NewClient().BlockByNumber(context.Background(), big.NewInt(targetBlockHeight)) - Expect(err).To(BeNil()) - Expect(latestBlock).NotTo(BeNil()) - - By("Connect all nodes to the validators") - for _, n := range nodes { + By("Stop consensus", func() { for _, v := range blockchain.Validators() { - Expect(n.AddPeer(v.NodeAddress())).To(BeNil()) + client := v.NewIstanbulClient() + Expect(client).NotTo(BeNil()) + err := client.StopMining(context.Background()) + Expect(err).To(BeNil()) + client.Close() } - } - - By("Wait for p2p connection") - waitFor(nodes, func(node container.Ethereum, wg *sync.WaitGroup) { - Expect(node.WaitForPeersConnected(numberOfValidators)).To(BeNil()) - wg.Done() }) - By("Wait for block synchronization between nodes and validators") - waitFor(nodes, func(geth container.Ethereum, wg *sync.WaitGroup) { - Expect(geth.WaitForBlockHeight(targetBlockHeight)).To(BeNil()) - wg.Done() + By("Connect all nodes to the validators", func() { + for _, n := range nodes { + for _, v := range blockchain.Validators() { + Expect(n.AddPeer(v.NodeAddress())).To(BeNil()) + } + } }) - By("Check target block hash of nodes") - for i := 0; i < numberOfNodes; i++ { - nodeClient := nodes[i].NewClient() - block, err := nodeClient.BlockByNumber(context.Background(), big.NewInt(targetBlockHeight)) + By("Wait for p2p connection", func() { + waitFor(nodes, func(node container.Ethereum, wg *sync.WaitGroup) { + Expect(node.WaitForPeersConnected(numberOfValidators)).To(BeNil()) + wg.Done() + }) + }) + By("Wait for block synchronization between nodes and validators", func() { + waitFor(nodes, func(geth container.Ethereum, wg *sync.WaitGroup) { + Expect(geth.WaitForBlockHeight(targetBlockHeight)).To(BeNil()) + wg.Done() + }) + }) + + By("Check target block hash of nodes", func() { + expectedBlock, err := blockchain.Validators()[0].NewClient().BlockByNumber(context.Background(), big.NewInt(targetBlockHeight)) Expect(err).To(BeNil()) - Expect(block).NotTo(BeNil()) - Expect(latestBlock.Hash()).To(BeEquivalentTo(block.Hash())) - } + Expect(expectedBlock).NotTo(BeNil()) + + for _, n := range nodes { + nodeClient := n.NewClient() + block, err := nodeClient.BlockByNumber(context.Background(), big.NewInt(targetBlockHeight)) + + Expect(err).To(BeNil()) + Expect(block).NotTo(BeNil()) + Expect(expectedBlock.Hash()).To(BeEquivalentTo(block.Hash())) + } + }) close(done) }, 30) diff --git a/tests/dynamic_test.go b/tests/dynamic_test.go index e5e9142f..30ea1f49 100644 --- a/tests/dynamic_test.go +++ b/tests/dynamic_test.go @@ -47,9 +47,9 @@ var _ = Describe("Dynamic validators addition/removal testing", func() { }) It("TFS-02-01 Add validators", func() { - testValidators := 3 + testValidators := 1 - By("Ensure that numbers of validator is equal to $numberOfValidators", func() { + By("Ensure the number of validators is correct", func() { for _, v := range blockchain.Validators() { client := v.NewIstanbulClient() validators, err := client.GetValidators(context.Background(), nil) @@ -58,18 +58,26 @@ var _ = Describe("Dynamic validators addition/removal testing", func() { } }) - _, err := blockchain.AddValidators(testValidators) - Expect(err).Should(BeNil()) - - By("Ensure that consensus is working in 50 seconds", func() { - Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 50*time.Second)).Should(BeNil()) - }) - for _, v := range blockchain.Validators() { - client := v.NewIstanbulClient() - validators, err := client.GetValidators(context.Background(), nil) + By("Add validators", func() { + _, err := blockchain.AddValidators(testValidators) Expect(err).Should(BeNil()) - Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+testValidators)) - } + }) + + By("Wait for several blocks", func() { + waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) { + Expect(geth.WaitForBlocks(5)).To(BeNil()) + wg.Done() + }) + }) + + By("Ensure the number of validators is correct", func() { + for _, v := range blockchain.Validators() { + client := v.NewIstanbulClient() + validators, err := client.GetValidators(context.Background(), nil) + Expect(err).Should(BeNil()) + Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+testValidators)) + } + }) }) It("TFS-02-03 Remove validators", func() { @@ -84,33 +92,44 @@ var _ = Describe("Dynamic validators addition/removal testing", func() { } }) - _, err := blockchain.AddValidators(numOfCandidates) - Expect(err).Should(BeNil()) + By("Add validators", func() { + _, err := blockchain.AddValidators(numOfCandidates) + Expect(err).Should(BeNil()) + }) By("Ensure that consensus is working in 50 seconds", func() { Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 50*time.Second)).Should(BeNil()) }) - for _, v := range blockchain.Validators() { - client := v.NewIstanbulClient() - validators, err := client.GetValidators(context.Background(), nil) - Expect(err).Should(BeNil()) - Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+numOfCandidates)) - } + + By("Check if the number of validators is correct", func() { + for _, v := range blockchain.Validators() { + client := v.NewIstanbulClient() + validators, err := client.GetValidators(context.Background(), nil) + Expect(err).Should(BeNil()) + Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+numOfCandidates)) + } + }) // remove validators [1,2,3] - removalCandidates := blockchain.Validators()[:numOfCandidates] - processingTime := time.Duration(math.Pow(2, float64(len(removalCandidates)))*7) * time.Second - Expect(blockchain.RemoveValidators(removalCandidates, processingTime)).Should(BeNil()) + By("Remove validators", func() { + removalCandidates := blockchain.Validators()[:numOfCandidates] + processingTime := time.Duration(math.Pow(2, float64(len(removalCandidates)))*7) * time.Second + Expect(blockchain.RemoveValidators(removalCandidates, processingTime)).Should(BeNil()) + }) + By("Ensure that consensus is working in 20 seconds", func() { Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 20*time.Second)).Should(BeNil()) }) - for _, v := range blockchain.Validators() { - client := v.NewIstanbulClient() - validators, err := client.GetValidators(context.Background(), nil) - Expect(err).Should(BeNil()) - Expect(len(validators)).Should(BeNumerically("==", numberOfValidators)) - } + By("Check if the number of validators is correct", func() { + for _, v := range blockchain.Validators() { + client := v.NewIstanbulClient() + validators, err := client.GetValidators(context.Background(), nil) + Expect(err).Should(BeNil()) + Expect(len(validators)).Should(BeNumerically("==", numberOfValidators)) + } + }) + By("Ensure that consensus is working in 30 seconds", func() { Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 30*time.Second)).Should(BeNil()) }) diff --git a/tests/general_consensus_test.go b/tests/general_consensus_test.go index 819c119a..924e21f7 100644 --- a/tests/general_consensus_test.go +++ b/tests/general_consensus_test.go @@ -17,9 +17,7 @@ package tests import ( - "context" - "errors" - "time" + "sync" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -45,38 +43,13 @@ var _ = Describe("TFS-01: General consensus", func() { blockchain.Finalize() }) - It("TFS-01-03: Peer connection", func() { + It("TFS-01-03: Peer connection", func(done Done) { + expectedPeerCount := len(blockchain.Validators()) - 1 + waitFor(blockchain.Validators(), func(v container.Ethereum, wg *sync.WaitGroup) { + Expect(v.WaitForPeersConnected(expectedPeerCount)).To(BeNil()) + wg.Done() + }) - By("Check peer count") - errc := make(chan error, numberOfValidators) - for _, v := range blockchain.Validators() { - go func(v container.Ethereum) { - c := v.NewIstanbulClient() - ticker := time.NewTicker(time.Millisecond * 100) - timeout := time.NewTimer(time.Second * 10) - expPeerCnt := numberOfValidators - 1 - for { - select { - case <-ticker.C: - peers, err := c.AdminPeers(context.Background()) - Expect(err).To(BeNil()) - if len(peers) != expPeerCnt { - continue - } else { - errc <- nil - return - } - case <-timeout.C: - errc <- errors.New("Check peer count timeout.") - return - } - } - }(v) - } - - for i := 0; i < numberOfValidators; i++ { - err := <-errc - Expect(err).To(BeNil()) - } - }) + close(done) + }, 20) }) diff --git a/tests/gossip_network_test.go b/tests/gossip_network_test.go index a4a6d962..4b234129 100644 --- a/tests/gossip_network_test.go +++ b/tests/gossip_network_test.go @@ -18,7 +18,7 @@ package tests import ( "context" - "time" + "sync" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -55,19 +55,12 @@ var _ = Describe("TFS-07: Gossip Network", func() { }) By("Checking blockchain progress", func() { - v0 := blockchain.Validators()[0] - c0 := v0.NewClient() - ticker := time.NewTicker(time.Millisecond * 100) - for _ = range ticker.C { - b, e := c0.BlockByNumber(context.Background(), nil) - Expect(e).To(BeNil()) - // Check if new blocks are getting generated - if b.Number().Int64() > 1 { - ticker.Stop() - break - } - } + waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) { + Expect(geth.WaitForBlocks(3)).To(BeNil()) + wg.Done() + }) }) + close(done) }, 240) }) diff --git a/tests/non_byzantine_faulty_test.go b/tests/non_byzantine_faulty_test.go index d0d3ed5d..7fa7ee5d 100644 --- a/tests/non_byzantine_faulty_test.go +++ b/tests/non_byzantine_faulty_test.go @@ -17,7 +17,7 @@ package tests import ( - "context" + "sync" "time" . "github.com/onsi/ginkgo" @@ -45,22 +45,15 @@ var _ = Describe("TFS-04: Non-Byzantine Faulty", func() { }) It("TFS-04-01: Stop F validators", func(done Done) { - v0 := blockchain.Validators()[0] By("Generating blockchain progress before stopping validator", func() { - c0 := v0.NewClient() - ticker := time.NewTicker(time.Millisecond * 100) - for _ = range ticker.C { - b, e := c0.BlockByNumber(context.Background(), nil) - Expect(e).To(BeNil()) - // Check if new blocks are getting generated - if b.Number().Int64() > 1 { - ticker.Stop() - break - } - } + waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) { + Expect(geth.WaitForBlocks(3)).To(BeNil()) + wg.Done() + }) }) By("Stopping validator 0", func() { + v0 := blockchain.Validators()[0] e := v0.Stop() Expect(e).To(BeNil()) ticker := time.NewTicker(time.Millisecond * 100) @@ -75,19 +68,10 @@ var _ = Describe("TFS-04: Non-Byzantine Faulty", func() { }) By("Checking blockchain progress after stopping validator", func() { - v1 := blockchain.Validators()[1] - c1 := v1.NewClient() - b1, e := c1.BlockByNumber(context.Background(), nil) - Expect(e).To(BeNil()) - ticker := time.NewTicker(time.Millisecond * 100) - for _ = range ticker.C { - newB1, e := c1.BlockByNumber(context.Background(), nil) - Expect(e).To(BeNil()) - if newB1.Number().Int64() > b1.Number().Int64() { - ticker.Stop() - break - } - } + waitFor(blockchain.Validators()[1:], func(geth container.Ethereum, wg *sync.WaitGroup) { + Expect(geth.WaitForBlocks(3)).To(BeNil()) + wg.Done() + }) }) close(done)