forked from thejerf/reign
/
node_test.go
276 lines (225 loc) · 8.29 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package reign
// In this file, we take advantage of the provisions we've made for testing
// within the rest of the code base. Thus, mailboxes will belong to one
// cluster or another. By convention, we name the test mailboxes as so:
// mailbox#A_#B
// where #A is an incrementing counter keeping track of the mailbox itself,
// and #B is the node number that the mailbox corresponds to.
// adding From#C means that the mailboxs is from the point of view
// of the given node, so, mailbox1_1From2 means "a boundRemoteAddress
// that will send to mailbox1_1 from the point of view of node 2"
// Since we use the connection server a lot, c# is the connection server
// for the given node number.
// TODO:
// * Test lost connection restored.
// * Test linking works normally
// * Test linking works when connection terminated.
import (
"testing"
"time"
"github.com/thejerf/reign/internal"
)
// this goes ahead and just lets the nodes talk over the network
// This function grabs the test bed and runs basic tests on it to
// establish that it is fundamentally working.
func TestMinimalTestBed(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
if ntb.addr1_1.GetID() == ntb.addr1_2.GetID() {
t.Fatal("Both mailboxs have the same ID")
}
ntb.rem1_1.Send("hello")
msg := ntb.mailbox1_1.ReceiveNext()
if msg.(string) != "hello" {
t.Fatal("Could not send message between nodes for some reason")
}
ntb.rem1_2.Send("world")
msg = ntb.mailbox1_2.ReceiveNext()
if msg.(string) != "world" {
t.Fatal("Did not get expected response from 'hello'")
}
ntb.rem1_2.Send("Checking 1_2")
msg = ntb.mailbox1_2.ReceiveNext()
if msg.(string) != "Checking 1_2" {
t.Fatal("Mailbox 1_2 is broken remotely.")
}
ntb.rem2_2.Send("Checking 2_2")
msg = ntb.mailbox2_2.ReceiveNext()
if msg.(string) != "Checking 2_2" {
t.Fatal("Mailbox 2_2 is broken remotely.")
}
}
func TestMessagesCanSendMailboxes(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
// Here we send address1_2 a reference to address1_1, from the POV
// where address1_1 is local. Can we then use that to send to
// address1_1 from the remote node?
ntb.rem1_2.Send(ntb.addr1_1)
sentAddr := ntb.mailbox1_2.ReceiveNext().(*Address)
// fixup the connectionServer
sentAddr.connectionServer = ntb.c2
sentAddr.Send("ack")
received := ntb.mailbox1_1.ReceiveNext()
if received.(string) != "ack" {
t.Fatal("Can not marshal around mailbox references.")
}
}
// Little tests which just get some coverage out of the way.
func TestCoverage(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
if !panics(func() { newConnections(nil, NodeID(1)) }) {
t.Fatal("Didn't panic with bad newConnections: no cluster")
}
c := &Cluster{}
if !panics(func() { newConnections(c, NodeID(1)) }) {
t.Fatal("Didn't panic with bad newConnections: no clusterlogger")
}
// set a null cluster so that we've already declared one, then test
// that we can't declare another.
NoClustering()
if !panics(func() { createFromSpec(testSpec(), 10, NullLogger) }) {
t.Fatal("createFromSpec does not object to double-creating connections")
}
var err error
nilConnections()
_, err = createFromSpec(testSpec(), 10, NullLogger)
if err.Error() != "the node claimed to be the local node is not defined" {
t.Fatal("Failed to verify the claimed local node is in the cluster")
}
}
// There's the following basic situations for remote links:
// * We link the remote address, and unlink it before it terminates.
// * We link the remote address, and it terminates before we're done.
// * We link the remote address, and it terminates, but before the
// message goes across, the link to the node terminates.
func TestHappyPathRemoteLink(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
// from the perspective of node 1, "Mr. Mailbox 1_2 on node 2,
// please tell me when you terminate."
ntb.rem1_2.NotifyAddressOnTerminate(ntb.addr1_1)
// yes, we run it twice; this covers the duplicate code path
ntb.rem1_2.NotifyAddressOnTerminate(ntb.addr1_1)
// "Let's wait on our testing until 1_2 has successfully recorded
// that 1_1 wants notification on termination.", which, due to
// how the internals work, is actually done when the Node 2
// remoteMailbox.Address is registered on mailbox 1_2.
ntb.addr1_2.getAddress().(*Mailbox).blockUntilNotifyStatus(ntb.remote2to1.Address, true)
// now that we know the listens are all set up "correctly", let's see
// if we get the terminate.
ntb.mailbox1_2.Terminate()
termNotice := ntb.mailbox1_1.ReceiveNext()
if termNotice.(mailboxID) != ntb.mailbox1_2.getID() {
t.Fatal("Got a termination notice for the wrong mailbox.")
}
}
// This tests what happens if we have two notifications, and then
// unnotify one of them. We still want to receive the termination notice.
func TestHappyPathPartialUnnotify(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
// this is used to determine when the remote mailboxes have
// successfully processed a unnotifyRemote on node1. As this
// should produce no message sent to node 2 (since there will still
// be a listener for the message), that's all we have to sync on.
gotUnnotifyRemote := make(chan struct{})
ntb.remote1to2.Send(newDoneProcessing{func(x interface{}) bool {
_, isUnnotifyRemote := x.(internal.UnnotifyRemote)
if isUnnotifyRemote {
gotUnnotifyRemote <- void
}
return !isUnnotifyRemote
}})
// Add notifications on 1_1 to both mailboxes on node 1
ntb.rem1_2.NotifyAddressOnTerminate(ntb.addr1_1)
ntb.rem1_2.NotifyAddressOnTerminate(ntb.addr2_1)
ntb.addr1_2.getAddress().(*Mailbox).blockUntilNotifyStatus(ntb.remote2to1.Address, true)
// remove it from one node
ntb.rem1_2.RemoveNotifyAddress(ntb.addr1_1)
<-gotUnnotifyRemote
// now, ensure that we still get notified on the remaining address
ntb.mailbox1_2.Terminate()
termNotice := ntb.mailbox1_2.ReceiveNext()
if termNotice.(mailboxID) != ntb.mailbox1_2.getID() {
t.Fatal("didn't get the right termination notice or something:", termNotice)
}
}
// This is like the previous test, except that we add two notifications
// and remove both of them.
func TestHappyPathFullUnnotify(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
ntb.rem1_2.NotifyAddressOnTerminate(ntb.addr1_1)
ntb.rem1_2.NotifyAddressOnTerminate(ntb.addr2_1)
// now, verify that the other side does indeed get a full Remove
// command when we remove the other notify address
gotRemoveNotifyNode := make(chan struct{})
ntb.remote2to1.Send(newDoneProcessing{func(x interface{}) bool {
_, isRNNOT := x.(*internal.RemoveNotifyNodeOnTerminate)
if isRNNOT {
gotRemoveNotifyNode <- void
}
return !isRNNOT
}})
ntb.rem1_2.RemoveNotifyAddress(ntb.addr1_1)
ntb.rem1_2.RemoveNotifyAddress(ntb.addr2_1)
// if we got this far, then the unnotify got processed.
<-gotRemoveNotifyNode
}
func TestRemoteLinkErrorPaths(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
// Send the remoteMailbox a message for the wrong node. (Verified that
// this goes down the right code path via coverage analysis.)
ntb.remote2to1.Send(&internal.IncomingMailboxMessage{internal.IntMailboxID(ntb.mailbox1_1.getID().(mailboxID)), "moo"})
time.Sleep(time.Second)
}
func TestConnectionPanicsClient(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
c := make(chan struct{})
ntb.remote2to1.connectionEstablished = func() {
c <- struct{}{}
}
ntb.remote2to1.Send(internal.PanicHandler{})
// this proves the connection was re-established.
<-c
}
func TestConnectionPanicsServer(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
c := make(chan struct{})
ntb.remote1to2.connectionEstablished = func() {
c <- struct{}{}
}
ntb.remote1to2.Send(internal.PanicHandler{})
// this proves the connection was re-established.
<-c
}
func TestConnectionDiesClient(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
c := make(chan struct{})
ntb.remote2to1.connectionEstablished = func() {
c <- struct{}{}
}
ntb.remote2to1.Send(internal.DestroyConnection{})
<-c
}
func TestConnectionDiesServer(t *testing.T) {
ntb := testbed(nil)
defer ntb.terminate()
c := make(chan struct{})
ntb.remote1to2.connectionEstablished = func() {
c <- struct{}{}
}
ntb.remote1to2.Send(internal.DestroyConnection{})
<-c
}
func TestCoverRemoteMailboxes(t *testing.T) {
rm := new(remoteMailboxes)
rm.ClusterLogger = NullLogger
rm.send(internal.PanicHandler{}, "")
}