-
Notifications
You must be signed in to change notification settings - Fork 211
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
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
730eb3e
Moved the code to a basic folder.
doug-perlin 00d5f17
Changed some comments around
doug-perlin 075770c
Update some comments
doug-perlin 4bc6e03
Added an ability to setup topologies for the network in setup
doug-perlin f0e7104
Update a comment
doug-perlin a770c82
Renamed the proto to match the package
doug-perlin 623c4eb
Clean up some variables, fixed up some messages
doug-perlin 4fcd23d
Clean up some variables
doug-perlin 8683488
Updated some of the comments
doug-perlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.