-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode_test.go
73 lines (57 loc) · 1.31 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cluster_test
import (
"sync"
"testing"
"time"
"github.com/aerogo/cluster"
"github.com/aerogo/cluster/client"
"github.com/aerogo/cluster/server"
"github.com/aerogo/packet"
"github.com/akyoto/assert"
)
const nodeCount = 5
func TestClusterClose(t *testing.T) {
nodes := make([]cluster.Node, nodeCount)
for i := 0; i < nodeCount; i++ {
nodes[i] = cluster.New(3000)
}
time.Sleep(100 * time.Millisecond)
for i := 0; i < nodeCount; i++ {
assert.False(t, nodes[i].IsClosed())
nodes[i].Close()
assert.True(t, nodes[i].IsClosed())
}
}
func TestClusterBroadcast(t *testing.T) {
nodes := make([]cluster.Node, nodeCount)
wg := sync.WaitGroup{}
message := "hello"
for i := 0; i < nodeCount; i++ {
nodes[i] = cluster.New(3000)
if nodes[i].IsServer() {
continue
}
wg.Add(1)
go func(node *client.Node) {
for msg := range node.Stream.Incoming {
switch msg.Type {
case 0:
assert.Equal(t, message, string(msg.Data))
wg.Done()
return
default:
panic("Unknown message")
}
}
}(nodes[i].(*client.Node))
}
// Wait for clients to connect
for nodes[0].(*server.Node).ClientCount() < nodeCount-1 {
time.Sleep(10 * time.Millisecond)
}
nodes[0].Broadcast(packet.New(0, []byte(message)))
wg.Wait()
for i := 0; i < nodeCount; i++ {
nodes[i].Close()
}
}