tests: test block refactoring and use wait methods

This commit is contained in:
Alan Chen 2017-08-23 17:04:33 +08:00
parent e48e2dad3b
commit c6dac0d378
5 changed files with 117 additions and 143 deletions

View File

@ -96,54 +96,59 @@ var _ = Describe("Block synchronization testing", func() {
It("TFS-06-02: Node synchronization", func(done Done) { It("TFS-06-02: Node synchronization", func(done Done) {
const targetBlockHeight = 10 const targetBlockHeight = 10
By("Wait for blocks") By("Wait for blocks", func() {
waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) { waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
Expect(geth.WaitForBlocks(targetBlockHeight)).To(BeNil()) Expect(geth.WaitForBlocks(targetBlockHeight)).To(BeNil())
wg.Done() wg.Done()
})
}) })
By("Stop consensus") By("Stop consensus", func() {
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 {
for _, v := range blockchain.Validators() { 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") By("Connect all nodes to the validators", func() {
waitFor(nodes, func(geth container.Ethereum, wg *sync.WaitGroup) { for _, n := range nodes {
Expect(geth.WaitForBlockHeight(targetBlockHeight)).To(BeNil()) for _, v := range blockchain.Validators() {
wg.Done() Expect(n.AddPeer(v.NodeAddress())).To(BeNil())
}
}
}) })
By("Check target block hash of nodes") By("Wait for p2p connection", func() {
for i := 0; i < numberOfNodes; i++ { waitFor(nodes, func(node container.Ethereum, wg *sync.WaitGroup) {
nodeClient := nodes[i].NewClient() Expect(node.WaitForPeersConnected(numberOfValidators)).To(BeNil())
block, err := nodeClient.BlockByNumber(context.Background(), big.NewInt(targetBlockHeight)) 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(err).To(BeNil())
Expect(block).NotTo(BeNil()) Expect(expectedBlock).NotTo(BeNil())
Expect(latestBlock.Hash()).To(BeEquivalentTo(block.Hash()))
} 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) close(done)
}, 30) }, 30)

View File

@ -47,9 +47,9 @@ var _ = Describe("Dynamic validators addition/removal testing", func() {
}) })
It("TFS-02-01 Add validators", 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() { for _, v := range blockchain.Validators() {
client := v.NewIstanbulClient() client := v.NewIstanbulClient()
validators, err := client.GetValidators(context.Background(), nil) validators, err := client.GetValidators(context.Background(), nil)
@ -58,18 +58,26 @@ var _ = Describe("Dynamic validators addition/removal testing", func() {
} }
}) })
_, err := blockchain.AddValidators(testValidators) By("Add validators", func() {
Expect(err).Should(BeNil()) _, err := blockchain.AddValidators(testValidators)
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(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() { It("TFS-02-03 Remove validators", func() {
@ -84,33 +92,44 @@ var _ = Describe("Dynamic validators addition/removal testing", func() {
} }
}) })
_, err := blockchain.AddValidators(numOfCandidates) By("Add validators", func() {
Expect(err).Should(BeNil()) _, err := blockchain.AddValidators(numOfCandidates)
Expect(err).Should(BeNil())
})
By("Ensure that consensus is working in 50 seconds", func() { By("Ensure that consensus is working in 50 seconds", func() {
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 50*time.Second)).Should(BeNil()) Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 50*time.Second)).Should(BeNil())
}) })
for _, v := range blockchain.Validators() {
client := v.NewIstanbulClient() By("Check if the number of validators is correct", func() {
validators, err := client.GetValidators(context.Background(), nil) for _, v := range blockchain.Validators() {
Expect(err).Should(BeNil()) client := v.NewIstanbulClient()
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+numOfCandidates)) validators, err := client.GetValidators(context.Background(), nil)
} Expect(err).Should(BeNil())
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+numOfCandidates))
}
})
// remove validators [1,2,3] // remove validators [1,2,3]
removalCandidates := blockchain.Validators()[:numOfCandidates] By("Remove validators", func() {
processingTime := time.Duration(math.Pow(2, float64(len(removalCandidates)))*7) * time.Second removalCandidates := blockchain.Validators()[:numOfCandidates]
Expect(blockchain.RemoveValidators(removalCandidates, processingTime)).Should(BeNil()) 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() { By("Ensure that consensus is working in 20 seconds", func() {
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 20*time.Second)).Should(BeNil()) Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 20*time.Second)).Should(BeNil())
}) })
for _, v := range blockchain.Validators() { By("Check if the number of validators is correct", func() {
client := v.NewIstanbulClient() for _, v := range blockchain.Validators() {
validators, err := client.GetValidators(context.Background(), nil) client := v.NewIstanbulClient()
Expect(err).Should(BeNil()) validators, err := client.GetValidators(context.Background(), nil)
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators)) Expect(err).Should(BeNil())
} Expect(len(validators)).Should(BeNumerically("==", numberOfValidators))
}
})
By("Ensure that consensus is working in 30 seconds", func() { By("Ensure that consensus is working in 30 seconds", func() {
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 30*time.Second)).Should(BeNil()) Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 30*time.Second)).Should(BeNil())
}) })

View File

@ -17,9 +17,7 @@
package tests package tests
import ( import (
"context" "sync"
"errors"
"time"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -45,38 +43,13 @@ var _ = Describe("TFS-01: General consensus", func() {
blockchain.Finalize() 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") close(done)
errc := make(chan error, numberOfValidators) }, 20)
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())
}
})
}) })

View File

@ -18,7 +18,7 @@ package tests
import ( import (
"context" "context"
"time" "sync"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -55,19 +55,12 @@ var _ = Describe("TFS-07: Gossip Network", func() {
}) })
By("Checking blockchain progress", func() { By("Checking blockchain progress", func() {
v0 := blockchain.Validators()[0] waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
c0 := v0.NewClient() Expect(geth.WaitForBlocks(3)).To(BeNil())
ticker := time.NewTicker(time.Millisecond * 100) wg.Done()
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
}
}
}) })
close(done) close(done)
}, 240) }, 240)
}) })

View File

@ -17,7 +17,7 @@
package tests package tests
import ( import (
"context" "sync"
"time" "time"
. "github.com/onsi/ginkgo" . "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) { It("TFS-04-01: Stop F validators", func(done Done) {
v0 := blockchain.Validators()[0]
By("Generating blockchain progress before stopping validator", func() { By("Generating blockchain progress before stopping validator", func() {
c0 := v0.NewClient() waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
ticker := time.NewTicker(time.Millisecond * 100) Expect(geth.WaitForBlocks(3)).To(BeNil())
for _ = range ticker.C { wg.Done()
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
}
}
}) })
By("Stopping validator 0", func() { By("Stopping validator 0", func() {
v0 := blockchain.Validators()[0]
e := v0.Stop() e := v0.Stop()
Expect(e).To(BeNil()) Expect(e).To(BeNil())
ticker := time.NewTicker(time.Millisecond * 100) 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() { By("Checking blockchain progress after stopping validator", func() {
v1 := blockchain.Validators()[1] waitFor(blockchain.Validators()[1:], func(geth container.Ethereum, wg *sync.WaitGroup) {
c1 := v1.NewClient() Expect(geth.WaitForBlocks(3)).To(BeNil())
b1, e := c1.BlockByNumber(context.Background(), nil) wg.Done()
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
}
}
}) })
close(done) close(done)