Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert cluster test to a testable Go example. #38

Merged
merged 9 commits into from
Jun 26, 2018
2 changes: 1 addition & 1 deletion examples/clusters/scripts.go → examples/basic/scripts.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//go:generate protoc --go_out=plugins=grpc:. messages/cluster.proto

package main
package basic
60 changes: 5 additions & 55 deletions examples/clusters/main_test.go → examples/basic/setup.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main
package basic

import (
"flag"
"fmt"
"testing"
"time"

"github.com/perlin-network/noise/crypto"
"github.com/perlin-network/noise/examples/clusters/messages"
"github.com/perlin-network/noise/examples/basic/messages"
"github.com/perlin-network/noise/grpc_utils"
"github.com/perlin-network/noise/network"
"github.com/perlin-network/noise/network/builders"
Expand Down Expand Up @@ -43,7 +41,8 @@ func (c *ClusterNode) PopMessage() *messages.ClusterTestMessage {

var blockTimeout = 10 * time.Second

func setupCluster(t *testing.T, nodes []*ClusterNode) error {
// SetupCluster - Sets up a fully connected group of nodes in a cluster
func SetupCluster(nodes []*ClusterNode) error {
peers := []string{}

for i := 0; i < len(nodes); i++ {
Expand Down Expand Up @@ -78,58 +77,9 @@ func setupCluster(t *testing.T, nodes []*ClusterNode) error {
for i := 0; i < len(nodes); i++ {
nodes[i].Net.Bootstrap(peers...)

// HACK: seems there's another race condition with Bootstrap
// TODO: seems there's another race condition with Bootstrap, use a sleep for now
time.Sleep(1 * time.Second)
}

return nil
}

func TestClusters(t *testing.T) {
flag.Parse()

host := "localhost"
cluster1StartPort := 3001
cluster1NumPorts := 3
nodes := []*ClusterNode{}

for i := 0; i < cluster1NumPorts; i++ {
node := &ClusterNode{}
node.Host = host
node.Port = cluster1StartPort + i

nodes = append(nodes, node)
}

if err := setupCluster(t, nodes); err != nil {
t.Fatal(err)
}

for i, node := range nodes {
if node.Net == nil {
t.Fatalf("Expected %d nodes, but node %d is missing a network", len(nodes), i)
}
}

// check if you can send a message from node 1 and will it be received only in node 2,3
{
testMessage := "message from node 0"
nodes[0].Net.Broadcast(&messages.ClusterTestMessage{Message: testMessage})

// HACK: TODO: replace sleep with something else
time.Sleep(1 * time.Second)

if result := nodes[0].PopMessage(); result != nil {
t.Errorf("Expected nothing in node 0, got %v", result)
}
for i := 1; i < len(nodes); i++ {
if result := nodes[i].PopMessage(); result == nil {
t.Errorf("Expected a message in node %d but it was blank", i)
} else {
if result.Message != testMessage {
t.Errorf("Expected message %s in node %d but got %v", testMessage, i, result)
}
}
}
}
}
65 changes: 65 additions & 0 deletions examples/basic/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package basic

import (
"flag"
"fmt"
"time"

"github.com/golang/glog"
"github.com/perlin-network/noise/examples/basic/messages"
)

func ExampleSetupClusters() {
// parse to flags to silence the glog library
flag.Parse()

host := "localhost"
cluster1StartPort := 5000
cluster1NumPorts := 3
nodes := []*ClusterNode{}

for i := 0; i < cluster1NumPorts; i++ {
node := &ClusterNode{}
node.Host = host
node.Port = cluster1StartPort + i

nodes = append(nodes, node)
}

if err := SetupCluster(nodes); err != nil {
fmt.Print(err)
}

for i, node := range nodes {
if node.Net == nil {
fmt.Printf("Expected %d nodes, but node %d is missing a network", len(nodes), i)
}
}

// check if you can send a message from node 1 and will it be received only in node 2,3
{
testMessage := "message from node 0"

// Broadcast is an asynchronous call to send a message to other nodes
nodes[0].Net.Broadcast(&messages.ClusterTestMessage{Message: testMessage})

// Simplificiation: message broadcasting is asynchronous, so need the messages to settle
time.Sleep(1 * time.Second)

if result := nodes[0].PopMessage(); result != nil {
glog.Errorf("Expected nothing in node 0, got %v", result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowercase and no punctuation for error messages.

}
for i := 1; i < len(nodes); i++ {
if result := nodes[i].PopMessage(); result == nil {
fmt.Printf("Expected a message in node %d but it was blank", i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errorf and code styling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment guideline pretty long, give me a minute to go through it

} else {
if result.Message != testMessage {
fmt.Printf("Expected message %s in node %d but got %v", testMessage, i, result)
}
}
}
}

fmt.Printf("Success")
// Output: Success
}