Skip to content

Commit

Permalink
fix top-level integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tok-kkk committed Oct 3, 2019
1 parent 7b8f367 commit 7ccf6ac
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
1 change: 0 additions & 1 deletion block/marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package block_test

43 changes: 34 additions & 9 deletions hyperdrive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
)

func init() {
mrand.Seed(time.Now().Unix())
seed := time.Now().Unix()
log.Printf("seed = %v", seed)
mrand.Seed(seed)
}

var _ = Describe("Hyperdrive", func() {
Expand Down Expand Up @@ -104,7 +106,11 @@ var _ = Describe("Hyperdrive", func() {

numNodesOffline := mrand.Intn(f) + 1 // Making sure at least one node is offline
shuffledIndices := mrand.Perm(3*f + 1)
SleepRandomSeconds(5, 10)

// Wait for all nodes reach consensus
Eventually(func() bool {
return network.HealthCheck(nil)
}, 15*time.Second).Should(BeTrue())

// Simulate connection issue for less than 1/3 nodes
phi.ParForAll(numNodesOffline, func(i int) {
Expand All @@ -128,7 +134,11 @@ var _ = Describe("Hyperdrive", func() {

numNodesOffline := mrand.Intn(f) + 1 // Making sure at least one node is offline
shuffledIndices := mrand.Perm(3*f + 1)
SleepRandomSeconds(5, 10)

// Wait for all nodes reach consensus
Eventually(func() bool {
return network.HealthCheck(nil)
}, 15*time.Second).Should(BeTrue())

// Simulate connection issue for less than 1/3 nodes
phi.ParForAll(numNodesOffline, func(i int) {
Expand All @@ -153,7 +163,11 @@ var _ = Describe("Hyperdrive", func() {

numNodesOffline := mrand.Intn(f) + 1 // Making sure at least one node is offline
shuffledIndices := mrand.Perm(3*f + 1)
SleepRandomSeconds(5, 10)

// Wait for all nodes reach consensus
Eventually(func() bool {
return network.HealthCheck(nil)
}, 15*time.Second).Should(BeTrue())

// Simulate connection issue for less than 1/3 nodes
phi.ParForAll(numNodesOffline, func(i int) {
Expand All @@ -177,7 +191,11 @@ var _ = Describe("Hyperdrive", func() {

numNodesOffline := mrand.Intn(f) + 1 // Making sure at least one node is offline
shuffledIndices := mrand.Perm(3*f + 1)
SleepRandomSeconds(5, 10)

// Wait for all nodes reach consensus
Eventually(func() bool {
return network.HealthCheck(nil)
}, 15*time.Second).Should(BeTrue())

// Simulate connection issue for less than 1/3 nodes
phi.ParForAll(numNodesOffline, func(i int) {
Expand All @@ -203,7 +221,11 @@ var _ = Describe("Hyperdrive", func() {

shuffledIndices := mrand.Perm(3*f + 1)
crashedNodes := shuffledIndices[:f+1]
SleepRandomSeconds(3, 6)

// Wait for all nodes reach consensus
Eventually(func() bool {
return network.HealthCheck(nil)
}, 15*time.Second).Should(BeTrue())

// simulate the nodes crashed.
phi.ParForAll(crashedNodes, func(i int) {
Expand All @@ -223,7 +245,10 @@ var _ = Describe("Hyperdrive", func() {
network.Start()
defer network.Stop()

SleepRandomSeconds(5, 10)
// Wait for all nodes reach consensus
Eventually(func() bool {
return network.HealthCheck(nil)
}, 15*time.Second).Should(BeTrue())

// Crash f + 1 random nodes and expect no blocks produced after that
shuffledIndices := mrand.Perm(3*f + 1)
Expand Down Expand Up @@ -347,7 +372,7 @@ func NewNetwork(f, r int, shards replica.Shards, options networkOptions) Network
}

// Initialize all nodes
genesisBlock := testutil.GenesisBlock(sigs[r:])
genesisBlock := testutil.GenesisBlock(sigs[:3*f+1])
broadcaster := NewMockBroadcaster(keys, options.minNetworkDelay, options.maxNetworkDelay)
nodes := make([]*Node, total)
for i := range nodes {
Expand All @@ -357,7 +382,7 @@ func NewNetwork(f, r int, shards replica.Shards, options networkOptions) Network
}
store := NewMockPersistentStorage(shards)
store.Init(genesisBlock)
nodes[i] = NewNode(logger.WithField("node", i), shards, keys[i], broadcaster, store, i >= r)
nodes[i] = NewNode(logger.WithField("node", i), shards, keys[i], broadcaster, store, i < 3*f+1)
}
ctx, cancel := context.WithCancel(context.Background())

Expand Down
1 change: 0 additions & 1 deletion process/marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
package process_test

20 changes: 6 additions & 14 deletions testutil/block.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package testutil

import (
"fmt"
"math/rand"
"reflect"
"testing/quick"
"time"

"github.com/renproject/hyperdrive/block"
"github.com/renproject/id"
)

var r *rand.Rand

func init() {
r = rand.New(rand.NewSource(time.Now().Unix()))
}

// RandomBytesSlice returns a random bytes slice.
func RandomBytesSlice() []byte {
t := reflect.TypeOf([]byte{})
value, ok := quick.Value(t, r)
if !ok {
panic(fmt.Sprintf("cannot generate random value of type %v", t.Name()))
length := rand.Intn(256)
slice := make([]byte, length)
_, err := rand.Read(slice)
if err != nil {
panic(err)
}
return value.Interface().([]byte)
return slice
}

// RandomBlockKind returns a random valid block kind.
Expand Down

0 comments on commit 7ccf6ac

Please sign in to comment.