-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
158 lines (139 loc) · 3.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package nexus
import (
"log"
"net"
"testing"
"time"
)
func Test_NodeBroadcast(t *testing.T) {
testNode := NewNode(NodeConfig{
Groups: -1,
}, "")
broadcastNet := NewGroup("broadcast", NexusGroupAddress{
IP: net.IPv4(224, 0, 0, 250),
Port: 9999,
})
if broadcastNet.Connection.IPV4 == nil {
t.Fail()
}
testNode.BroadcastGroup = ConnectedNexusGroup{
Group: broadcastNet,
Output: make(chan NodeMessage, 10),
Input: make(chan interface{}, 10),
InputHandler: testNode.BroadcastInputHandler,
}
testNode.BroadcastGroup.Group.Connect(testNode.Config.Intf)
go func() {
testNode.HandleBroadcastStream()
}()
go testNode.HandleConnectStream()
time.Sleep(25 * time.Second)
}
func Test_TwoNodes(t *testing.T) {
testNode1 := NewNode(NodeConfig{
Groups: -1,
}, "")
testNode2 := NewNode(NodeConfig{
Groups: -1,
}, "")
if testNode2.BroadcastGroup.Group.Connection.IPV4 == nil {
t.Fail()
}
testNode1.Run()
time.Sleep(5 * time.Second)
testNode2.Run()
time.Sleep(25 * time.Second)
}
func Test_NodeExclusion(t *testing.T) {
testNode1 := NewNode(NodeConfig{
Groups: -1,
}, "node1")
testNode2 := NewNode(NodeConfig{
Groups: -1,
}, "node2")
testNode3 := NewNode(NodeConfig{
Groups: -1,
}, "node3")
oneAndTwoGroup := NewGroup("1+2", NexusGroupAddress{
IP: net.IPv4(224, 0, 0, 1),
Port: 4200,
})
oneAndTwoGroup.Private = true
oneAndTwoGrouptwo := NewGroup("1+2", NexusGroupAddress{
IP: net.IPv4(224, 0, 0, 1),
Port: 4200,
})
oneAndTwoGrouptwo.Private = true
threeSoloGroup := NewGroup("3", NexusGroupAddress{
IP: net.IPv4(224, 0, 0, 2),
Port: 6900,
})
threeSoloGroup.Private = true
threeSoloGroupTwo := NewGroup("3", NexusGroupAddress{
IP: net.IPv4(224, 0, 0, 2),
Port: 6900,
})
threeSoloGroupTwo.Private = true
testNode1.Run()
log.Println("Node 1 started")
// time.Sleep(1 * time.Second)
testNode2.Run()
// time.Sleep(1 * time.Second)
testNode3.Run()
testNode1.ConnectGroup(oneAndTwoGroup)
log.Printf("Connected: %v\n", testNode1)
testNode2.ConnectGroup(oneAndTwoGrouptwo)
log.Printf("Connected: %v\n", testNode2)
testNode3.ConnectGroup(threeSoloGroup)
testNode2.ConnectGroup(threeSoloGroupTwo)
go func() {
ticker := time.NewTicker(500 * time.Millisecond)
for range ticker.C {
testNode2.Groups[oneAndTwoGroup.Id].Output <- NodeMessage{
SenderId: testNode2.Id,
Routing: MessageRouting{
MessageType: Message,
DestinationAddress: oneAndTwoGroup.Address.Address,
},
Data: map[string]interface{}{
"1+2": "I can only be seen by 1+2",
},
}
testNode3.Groups[threeSoloGroup.Id].Output <- NodeMessage{
SenderId: testNode3.Id,
Routing: MessageRouting{
MessageType: Message,
DestinationAddress: threeSoloGroup.Address.Address,
},
Data: map[string]interface{}{
"3": "I can only be seen by 2",
},
}
}
}()
go func() {
for msg := range testNode1.Groups[oneAndTwoGroup.Id].Input {
log.Printf("Test Node 1 received: %v\n", msg)
}
}()
go func() {
for msg := range testNode2.Groups[oneAndTwoGroup.Id].Input {
log.Printf("Test Node 2 received: %v\n", msg)
}
}()
go func() {
for msg := range testNode2.Groups[threeSoloGroup.Id].Input {
log.Printf("Test Node 2 received: %v\n", msg)
}
}()
go func() {
for msg := range testNode3.Groups[threeSoloGroup.Id].Input {
log.Printf("Test Node 3 received: %v\n", msg)
}
}()
log.Printf("Start sleep: %v\n", time.Now())
time.Sleep(25 * time.Second)
// testNode1.Groups[oneAndTwoGroup.Id]
// testNode2.Groups[oneAndTwoGroup.Id] = oneAndTwoGroup
// testNode3.Groups[threeSoloGroup.Id] = threeSoloGroup
}