This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sessionRegistry_test.go
324 lines (247 loc) · 10.1 KB
/
sessionRegistry_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package webwire
import (
"sync"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSessRegRegistration tests registration
func TestSessRegRegistration(t *testing.T) {
reg := newSessionRegistry(0, nil)
// Register connection with session
clt := newConnection(nil, nil, ConnectionOptions{})
sess := NewSession(nil, func() string { return "testkey_A" })
clt.session = &sess
require.NoError(t, reg.register(clt))
// Expect 1 active session
require.Equal(t, 1, reg.activeSessionsNum())
// Expect 1 active connection on session A
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_A"))
}
// TestSessRegActiveSessionsNum tests the ActiveSessionsNum method
func TestSessRegActiveSessionsNum(t *testing.T) {
expectedSessionsNum := 2
reg := newSessionRegistry(0, nil)
// Register 2 connections on two separate sessions
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA
cltB1 := newConnection(nil, nil, ConnectionOptions{})
sessB := NewSession(nil, func() string { return "testkey_B" })
cltB1.session = &sessB
require.NoError(t, reg.register(cltA1))
require.NoError(t, reg.register(cltB1))
// Expect 2 active sessions
require.Equal(t, expectedSessionsNum, reg.activeSessionsNum())
// Expect 1 active connection on each session
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_A"))
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_B"))
}
// TestSessRegsessionConnectionsNum tests the sessionConnectionsNum method
func TestSessRegsessionConnectionsNum(t *testing.T) {
expectedSessionsNum := 1
reg := newSessionRegistry(0, nil)
// Register first connection on session A
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA1 := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA1
require.NoError(t, reg.register(cltA1))
// Register second connection on same session
cltA2 := newConnection(nil, nil, ConnectionOptions{})
sessA2 := NewSession(nil, func() string { return "testkey_A" })
cltA2.session = &sessA2
require.NoError(t, reg.register(cltA2))
// Expect 1 active session
require.Equal(t, expectedSessionsNum, reg.activeSessionsNum())
// Expect 2 active connections on session A
require.Equal(t, 2, reg.sessionConnectionsNum("testkey_A"))
}
// TestSessRegSessionMaxConns tests the Register method
// when the maximum number of concurrent connections of a session was reached
func TestSessRegSessionMaxConns(t *testing.T) {
// Set the maximum number of concurrent session connection to 1
reg := newSessionRegistry(1, nil)
// Register first connection on session A
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA1 := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA1
require.NoError(t, reg.register(cltA1))
// Register first connection on session A
cltA2 := newConnection(nil, nil, ConnectionOptions{})
sessA2 := NewSession(nil, func() string { return "testkey_A" })
cltA2.session = &sessA2
require.Error(t,
reg.register(cltA1),
"Expected register to return an error "+
"due to the limit of concurrent connection being reached",
)
}
// TestSessRegDeregistration tests deregistration
func TestSessRegDeregistration(t *testing.T) {
reg := newSessionRegistry(0, nil)
// Register 2 connections on two separate sessions
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA
cltB1 := newConnection(nil, nil, ConnectionOptions{})
sessB := NewSession(nil, func() string { return "testkey_B" })
cltB1.session = &sessB
require.NoError(t, reg.register(cltA1))
require.NoError(t, reg.register(cltB1))
// Expect 2 active sessions
require.Equal(t, 2, reg.activeSessionsNum())
// Expect 1 active connection on each session
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_A"))
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_B"))
// Deregister first connection, expect 0 because session was removed
require.Equal(t, 0, reg.deregister(cltA1, false))
// Expect 1 active session after deregistration of the first connection
require.Equal(t, 1, reg.activeSessionsNum())
// Expect 0 active connections on deregistered session
require.Equal(t, -1, reg.sessionConnectionsNum("testkey_A"))
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_B"))
// Deregister second connection, expect 0 because session was removed
require.Equal(t, 0, reg.deregister(cltB1, false))
// Expect no active sessions after deregistration of the second connection
require.Equal(t, 0, reg.activeSessionsNum())
// Expect no active connections on both sessions
require.Equal(t, -1, reg.sessionConnectionsNum("testkey_A"))
require.Equal(t, -1, reg.sessionConnectionsNum("testkey_B"))
}
// TestSessRegDeregistrationMultiple tests deregistration of multiple
// connections of a single session
func TestSessRegDeregistrationMultiple(t *testing.T) {
reg := newSessionRegistry(0, nil)
// Register 2 connections on the same session
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA1 := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA1
cltA2 := newConnection(nil, nil, ConnectionOptions{})
cltA2.session = &sessA1
require.NoError(t, reg.register(cltA1))
require.NoError(t, reg.register(cltA2))
// Expect 1 active session
require.Equal(t, 1, reg.activeSessionsNum())
// Expect 2 active connections on session A
require.Equal(t, 2, reg.sessionConnectionsNum("testkey_A"))
// Deregister first connection, expect 1
require.Equal(t, 1, reg.deregister(cltA1, false))
// Still expect 1 active session
// after deregistration of the first connection
require.Equal(t, 1, reg.activeSessionsNum())
// Expect 1 remaining active connection on deregistered session
require.Equal(t, 1, reg.sessionConnectionsNum("testkey_A"))
// Deregister second connection, expect 0 because session was removed
require.Equal(t, 0, reg.deregister(cltA2, false))
// Expect no active sessions after deregistration of the second connection
require.Equal(t, 0, reg.activeSessionsNum())
// Expect no active connections session A
require.Equal(t, -1, reg.sessionConnectionsNum("testkey_A"))
}
// TestSessRegConcurrentAccess tests concurrent (de)registration
func TestSessRegConcurrentAccess(t *testing.T) {
reg := newSessionRegistry(0, nil)
connsToRegister := uint(16)
registeredConns := make([]*connection, connsToRegister)
var awaitRegistration sync.WaitGroup
var awaitDeregistration sync.WaitGroup
// Populate registered conns map
for i := uint(0); i < connsToRegister; i++ {
// Create a connection on session A
clt := newConnection(nil, nil, ConnectionOptions{})
sess := NewSession(nil, func() string { return "testkey_A" })
clt.session = &sess
registeredConns[i] = clt
awaitRegistration.Add(1)
awaitDeregistration.Add(1)
}
// Concurrently register multiple connections from different goroutines
for i := uint(0); i < connsToRegister; i++ {
index := i
go func() {
assert.NoError(t, reg.register(registeredConns[index]))
awaitRegistration.Done()
}()
}
// Wait for all goroutines to finish before evaluating the results
awaitRegistration.Wait()
// Expect 1 active session
require.Equal(t, 1, reg.activeSessionsNum())
// Expect N active connections on session A
actualSessCons := reg.sessionConnectionsNum("testkey_A")
require.NotEqual(t, -1, actualSessCons, "Missing session testkey_A")
require.Equal(t, connsToRegister, uint(actualSessCons))
// Concurrently deregister multiple connections from different goroutines
negativeReturnCounter := uint32(0)
for i := uint(0); i < connsToRegister; i++ {
index := i
go func() {
// Deregister one of the connections
result := reg.deregister(registeredConns[index], false)
if result == -1 {
atomic.AddUint32(&negativeReturnCounter, 1)
}
awaitDeregistration.Done()
}()
}
// Wait for all goroutines to finish before evaluating the results
awaitDeregistration.Wait()
require.Equal(t, uint32(0), negativeReturnCounter)
// Expect 1 active session
require.Equal(t, 0, reg.activeSessionsNum())
// Expect no active connections on session A
require.Equal(t, -1, reg.sessionConnectionsNum("testkey_A"))
}
// TestSessRegSessionConnections tests the sessionConnections method
func TestSessRegSessionConnections(t *testing.T) {
expectedSessionsNum := 1
reg := newSessionRegistry(0, nil)
// Register first connection on session A
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA1 := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA1
require.NoError(t, reg.register(cltA1))
// Register second connection on same session
cltA2 := newConnection(nil, nil, ConnectionOptions{})
sessA2 := NewSession(nil, func() string { return "testkey_A" })
cltA2.session = &sessA2
require.NoError(t, reg.register(cltA2))
// Expect 1 active session
require.Equal(t, expectedSessionsNum, reg.activeSessionsNum())
// Expect 2 active connections on session A
list := reg.sessionConnections("testkey_A")
require.Len(t, list, 2)
require.Contains(t, list, cltA1)
require.Contains(t, list, cltA2)
}
// TestSessRegDestruction tests deregistration
func TestSessRegDestruction(t *testing.T) {
cb := [2]bool{false, false}
reg := newSessionRegistry(0, func(sessionKey string) {
if sessionKey == "testkey_A" {
cb[0] = true
} else if sessionKey == "testkey_B" {
cb[1] = true
}
})
// Register 2 connections on session A, and 1 on session B
cltA1 := newConnection(nil, nil, ConnectionOptions{})
sessA := NewSession(nil, func() string { return "testkey_A" })
cltA1.session = &sessA
cltA2 := newConnection(nil, nil, ConnectionOptions{})
cltA2.session = &sessA
cltB1 := newConnection(nil, nil, ConnectionOptions{})
sessB := NewSession(nil, func() string { return "testkey_B" })
cltB1.session = &sessB
require.NoError(t, reg.register(cltA1))
require.NoError(t, reg.register(cltA2))
require.NoError(t, reg.register(cltB1))
// Deregister both
reg.deregister(cltA1, true)
require.Equal(t, [2]bool{false, false}, cb)
reg.deregister(cltA2, true)
require.Equal(t, [2]bool{true, false}, cb)
reg.deregister(cltB1, true)
require.Equal(t, [2]bool{true, true}, cb)
}