-
Notifications
You must be signed in to change notification settings - Fork 15
/
listener_test.go
197 lines (160 loc) · 4.36 KB
/
listener_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
package reign
import (
"context"
"net"
"sync"
"testing"
)
func TestCoverNilListener(t *testing.T) {
t.Parallel()
var nl *nodeListener
nl.waitForListen()
}
func TestCoverIncomingConnection(t *testing.T) {
t.Parallel()
var ic *incomingConnection
err := ic.send(nil)
if err == nil {
t.Fatal("incoming connection can send to nowhere")
}
}
type S struct{}
func (s S) String() string {
return "string"
}
func TestMyStringCover(t *testing.T) {
t.Parallel()
myString(S{})
}
func TestNodeListenerErrors(t *testing.T) {
ntb := testbed(nil, testLogger{t})
defer ntb.terminate()
nl := &nodeListener{
node: ntb.node1connectionServer.ThisNode,
connectionServer: ntb.node1connectionServer,
ClusterLogger: NullLogger,
remoteMailboxes: make(map[NodeID]*remoteMailboxes),
}
nl.condition = sync.NewCond(&nl.Mutex)
if !panics(func() { nl.mailboxesForNode(10) }) {
t.Fatal("Can get mailboxes that don't exist.")
}
// verify that it doesn't really serve, but terminates as expected.
nl.stopped = true
nl.Serve()
nl.stopped = false
// this is an IP address that hopefully you don't actually have
// permission to bind to.
badTCPAddr, err := net.ResolveTCPAddr("tcp4", "253.255.255.254:8")
if err != nil {
panic(err)
}
nl.node.listenaddr = nil
if !panics(func() { nl.Serve() }) {
t.Fatal("Node is happy to start serving with no listen.")
}
nl.node.listenaddr = badTCPAddr
if !panics(func() { nl.Serve() }) {
t.Fatal("We can listen on an invalid TCP addr?")
}
goodTCPAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:29876")
if err != nil {
panic(err)
}
nl.node.listenaddr = goodTCPAddr
terminated := make(chan struct{})
go func() {
nl.Serve()
terminated <- struct{}{}
}()
nl.waitForListen()
_ = nl.String() // should not block
_ = nl.listener.Close()
<-terminated
}
func TestListenerSSLHandshakeFailures(t *testing.T) {
ntb := unstartedTestbed(nil, testLogger{t})
// we never start the servers, so we only need this
defer ntb.terminateMailboxes()
ntb.node2connectionServer.listener.failOnSSLHandshake = true
thingsTerminateOnFailure(t, ntb)
}
func TestListenerClusterHandshakeFailures(t *testing.T) {
ntb := unstartedTestbed(nil, testLogger{t})
defer ntb.terminateMailboxes()
ntb.node2connectionServer.listener.failOnClusterHandshake = true
thingsTerminateOnFailure(t, ntb)
}
func TestNodeSSLHandshakeFailures(t *testing.T) {
ntb := unstartedTestbed(nil, testLogger{t})
defer ntb.terminateMailboxes()
ntb.node1connectionServer.nodeConnectors[2].failOnSSLHandshake = true
thingsTerminateOnFailure(t, ntb)
}
func TestNodeClusterHandshakeFailure(t *testing.T) {
ntb := unstartedTestbed(nil, testLogger{t})
defer ntb.terminateMailboxes()
ntb.node1connectionServer.nodeConnectors[2].failOnClusterHandshake = true
thingsTerminateOnFailure(t, ntb)
}
func thingsTerminateOnFailure(t *testing.T, ntb *NetworkTestBed) {
// this reaches in to serve the listener socket directly
done := make(chan struct{})
go func() {
defer func() {
done <- struct{}{}
}()
ntb.node2connectionServer.listener.Serve()
}()
ntb.node2connectionServer.waitForListen()
// and this reaches in to directly run the "connect to node 2" function
go func() {
defer func() {
done <- struct{}{}
}()
ntb.node1connectionServer.nodeConnectors[2].Serve()
}()
// Assert that in the case of failure to connect due to SSL negotiation
// failures, both systems terminate, so suture can pick them up.
<-done
ntb.node2connectionServer.listener.Stop()
<-done
}
func TestCoverStopNodeListener(t *testing.T) {
t.Parallel()
nl := new(nodeListener)
nl.Stop()
if !nl.stopped {
t.Fatal("Could not stop the node listener.")
}
}
func BenchmarkMinimalMessageSend(b *testing.B) {
ntb := testbed(nil, nil)
defer ntb.terminate()
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := ntb.fromNode2toNode1mailbox1.Send("a")
if err != nil {
b.Error(err)
}
_, err = ntb.node1mailbox1.Receive(ctx)
if err != nil {
b.Error(err)
}
}
}
func BenchmarkRegisterUnregisterMailbox(b *testing.B) {
ntb := testbed(nil, nil)
defer ntb.terminate()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
a, m := ntb.node1connectionServer.NewMailbox()
_ = ntb.node1connectionServer.registry.Register("test", a)
ntb.node1connectionServer.registry.Sync()
m.Close()
ntb.node1connectionServer.registry.Sync()
}
}