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
Prev Previous commit
Updated some of the comments
  • Loading branch information
doug-perlin committed Jun 26, 2018
commit 8683488a5167df23478301d40c8892182a13ee6b
22 changes: 12 additions & 10 deletions examples/basic/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ import (
)

type ClusterNode struct {
Host string
Port int
Peers []string
Net *network.Network
BufferedMessages []*messages.BasicMessage
Host string
Port int
Peers []string
Net *network.Network
Messages []*messages.BasicMessage
}

// Handle implements the network interface callback
func (c *ClusterNode) Handle(client *network.PeerClient, raw *network.IncomingMessage) error {
message := raw.Message.(*messages.BasicMessage)

c.BufferedMessages = append(c.BufferedMessages, message)
c.Messages = append(c.Messages, message)

return nil
}

// PopMessage returns the oldest message from it's buffer and removes it from the list
func (c *ClusterNode) PopMessage() *messages.BasicMessage {
if len(c.BufferedMessages) == 0 {
if len(c.Messages) == 0 {
return nil
}
var retVal *messages.BasicMessage
retVal, c.BufferedMessages = c.BufferedMessages[0], c.BufferedMessages[1:]
retVal, c.Messages = c.Messages[0], c.Messages[1:]
return retVal
}

var blockTimeout = 10 * time.Second

// SetupCluster - Sets up a connected group of nodes in a cluster
// SetupCluster sets up a connected group of nodes in a cluster.
func SetupCluster(nodes []*ClusterNode) error {
for i := 0; i < len(nodes); i++ {
node := nodes[i]
Expand All @@ -65,7 +67,7 @@ func SetupCluster(nodes []*ClusterNode) error {

for i := 0; i < len(nodes); i++ {
if err := grpc_utils.BlockUntilConnectionReady(nodes[i].Host, nodes[i].Port, blockTimeout); err != nil {
return fmt.Errorf("Error: port was not available, cannot bootstrap node %d peers, err=%+v", i, err)
return fmt.Errorf("port was not available, cannot bootstrap node %d peers: %+v", i, err)
}
}

Expand Down