-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_test.go
46 lines (40 loc) · 1012 Bytes
/
node_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package gossip
import (
"fmt"
"math/rand"
"strconv"
"testing"
"time"
)
func TestMessages(t *testing.T) {
bootNodeId := NewNodeId("127.0.0.1:7999")
bootNode := New(bootNodeId, "test topic")
go bootNode.Listen()
var nodes [100]*Node
for i := range nodes {
nodeId := NewNodeId(fmt.Sprintf("127.0.0.1:%d", 8000+i))
nodes[i] = New(nodeId, "test topic")
go nodes[i].Listen()
nodes[i].Join([]NodeId{bootNodeId})
}
time.Sleep(10 * time.Second)
fmt.Printf("bootNode neighbors: %d\n", bootNode.neighbors.Len())
for i := range nodes {
fmt.Printf("node %d neighbors: %d\n", i, nodes[i].neighbors.Len())
}
for i := 0; i < 100; i++ {
nodeIndex := rand.Uint32() % 100
nodes[nodeIndex].Gossip([]byte(strconv.Itoa(i)))
}
fmt.Println("messages sent")
for i := range nodes {
var check map[string]bool
for j := 0; j < 100; j++ {
msg := string(<-nodes[i].GetMsgChan())
if _, ok := check[msg]; ok {
t.Error("duplicated message")
}
}
fmt.Printf("node %d complete\n", i)
}
}