-
Notifications
You must be signed in to change notification settings - Fork 6
/
channel_test.go
498 lines (409 loc) · 11.4 KB
/
channel_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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright (c) 2015-2017. Oleg Sklyar & teris.io. All rights reserved.
// See the LICENSE file in the project root for licensing information.
package longpoll_test
import (
"sort"
"testing"
"time"
"github.com/teris-io/longpoll"
)
func TestChannel_onNewChannel_active(t *testing.T) {
timeout := 400 * time.Millisecond
ch, err := longpoll.NewChannel(timeout, func(id string) {}, "any")
if err != nil {
t.Error(err)
}
if !ch.IsAlive() {
t.Error("channel down on construction")
}
if len(ch.ID()) < 9 || len(ch.ID()) > 11 {
t.Error("expected an Id from go-shortid")
}
}
func TestChannel_onMustNewChannel_active(t *testing.T) {
timeout := 400 * time.Millisecond
ch := longpoll.MustNewChannel(timeout, func(id string) {}, "any")
if !ch.IsAlive() {
t.Error("channel down on construction")
}
}
func TestChannel_onNewChannel_withZeroTimeout_error(t *testing.T) {
_, err := longpoll.NewChannel(0*time.Second, nil, "any")
if err == nil {
t.Errorf("error expected")
}
}
func TestChannel_onNewChannel_withEmptyTopics_error(t *testing.T) {
var topics []string
_, err := longpoll.NewChannel(10*time.Second, nil, topics...)
if err == nil {
t.Errorf("error expected")
}
}
func TestChannel_onMustNewChannel_withEmptyTopics_panics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("panic expected")
}
}()
var topics []string
longpoll.MustNewChannel(10*time.Second, nil, topics...)
}
func TestChannel_onDuplicateTopics_uniqued(t *testing.T) {
timeout := 400 * time.Millisecond
ch := longpoll.MustNewChannel(timeout, func(id string) {}, "A", "B", "C", "B", "A")
if len(ch.Topics()) != 3 {
t.Error("wrong topics")
}
}
func TestChannel_onNewChannel_andNoAction_expires(t *testing.T) {
timeout := 400 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
var end time.Time
ch := longpoll.MustNewChannel(timeout, func(id string) {
end = time.Now()
}, "any")
defer ch.Drop()
if !ch.IsAlive() {
t.Errorf("channel not alive on start")
}
time.Sleep(timeout - tolerance)
if !ch.IsAlive() {
t.Errorf("channel not alive before timeout")
}
time.Sleep(tolerance + tolerance)
if ch.IsAlive() {
t.Errorf("channel alive on timeout")
}
if end.Sub(start) < timeout {
t.Errorf("timeout too early")
}
if end.Sub(start) > timeout+tolerance {
t.Errorf("timeout too late")
}
}
func TestChannel_onTimeout_handlerCalledWithCorrectId(t *testing.T) {
timeout := 400 * time.Millisecond
tolerance := 25 * time.Millisecond
var idx string
ch := longpoll.MustNewChannel(timeout, func(id string) {
idx = id
}, "any")
defer ch.Drop()
time.Sleep(timeout + tolerance)
if idx != ch.ID() {
t.Errorf("no or incorrect channel id in onClose handler")
}
}
func TestChannel_onNoHandler_whenTimeout_success(t *testing.T) {
timeout := 400 * time.Millisecond
tolerance := 25 * time.Millisecond
ch := longpoll.MustNewChannel(timeout, nil, "any")
defer ch.Drop()
time.Sleep(timeout - tolerance)
if !ch.IsAlive() {
t.Errorf("channel not alive before timeout")
}
time.Sleep(tolerance + tolerance)
if ch.IsAlive() {
t.Errorf("channel alive on timeout")
}
}
func TestChannel_onGetAndNoPublish_expiresGetAndChannel(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "any")
defer ch.Drop()
time.Sleep(polltime)
datach, _ := ch.Get(polltime)
data := <-datach
if time.Now().Sub(start) < polltime+polltime {
t.Errorf("get returned before polltime")
}
if time.Now().Sub(start) > polltime+polltime+tolerance {
t.Errorf("get returned way too late")
}
if len(data) > 0 {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
time.Sleep(timeout - polltime + tolerance)
if ch.IsAlive() {
t.Errorf("channel alive on timeout")
}
}
func TestChannel_onGet_withZeroPollTime_error(t *testing.T) {
timeout := 400 * time.Millisecond
ch := longpoll.MustNewChannel(timeout, nil, "any")
defer ch.Drop()
_, err := ch.Get(0 * time.Millisecond)
if err == nil {
t.Error("error expected")
}
}
func TestChannel_onDrop_withGetWaiting_cleanup(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
ch := longpoll.MustNewChannel(timeout, nil, "A", "B")
if ch.QueueSize() != 0 {
t.Errorf("unexpected queue size")
}
topics := ch.Topics()
sort.Strings(topics)
if topics[0] != "A" || topics[1] != "B" {
t.Errorf("unexpected topics")
}
if ch.IsGetWaiting() {
t.Errorf("unexpected get waiting")
}
datach, _ := ch.Get(polltime)
time.Sleep(tolerance)
if !ch.IsGetWaiting() {
t.Errorf("get not waiting")
}
ch.Drop()
time.Sleep(tolerance)
if len(<-datach) > 0 {
t.Errorf("unexpected results in get")
}
if ch.IsGetWaiting() {
t.Errorf("unexpected get waiting")
}
if len(ch.Topics()) != 2 {
t.Errorf("expected unchanged topics")
}
if ch.IsAlive() {
t.Errorf("channel unexpectedly alive")
}
}
type pubdata struct {
value int
}
func TestChannel_onPublishThenGetThenGet_Get1ComesBackImmediately_Get2Expires(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A")
defer ch.Drop()
outdata := pubdata{value: 351}
ch.Publish(&outdata, "A")
time.Sleep(tolerance)
datach, _ := ch.Get(polltime)
data := <-datach
if time.Now().Sub(start) > 2*tolerance {
t.Errorf("get returned late")
}
if len(data) != 1 || data[0] != &outdata {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
time.Sleep(polltime)
datach, _ = ch.Get(polltime)
data = <-datach
if time.Now().Sub(start) < 2*polltime+tolerance {
t.Errorf("get returned before polltime")
}
if time.Now().Sub(start) > 2*polltime+2*tolerance {
t.Errorf("get returned way too late")
}
if len(data) > 0 {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
}
func TestChannel_onGetThenPublish_GetComesBackUponPublish(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A")
defer ch.Drop()
datach, _ := ch.Get(polltime)
time.Sleep(polltime / 2)
outdata := pubdata{value: 351}
ch.Publish(&outdata, "A")
time.Sleep(tolerance)
data := <-datach
if time.Now().Sub(start) > polltime/2+2*tolerance {
t.Errorf("get returned late")
}
if len(data) != 1 || data[0] != &outdata {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
}
func TestChannel_onGetThenGetThenPublish_Get1Expires_andGet2ComesWithData(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A")
defer ch.Drop()
datach1, _ := ch.Get(polltime)
time.Sleep(polltime / 2)
datach2, _ := ch.Get(polltime)
time.Sleep(polltime / 2)
data1 := <-datach1
if time.Now().Sub(start) < polltime {
t.Errorf("get1 returned early")
}
if time.Now().Sub(start) > polltime+tolerance {
t.Errorf("get1 returned late")
}
if len(data1) > 0 {
t.Errorf("unexpected data in get1")
}
outdata := pubdata{value: 351}
ch.Publish(&outdata, "A")
time.Sleep(tolerance)
data2 := <-datach2
if time.Now().Sub(start) > polltime+2*tolerance {
t.Errorf("get2 returned late")
}
if len(data2) != 1 || data2[0] != &outdata {
t.Errorf("unexpected data in get2")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
}
func TestChannel_onNxPublishThenGet_GetReceivesAll(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A", "C")
defer ch.Drop()
outdata1 := pubdata{value: 1}
outdata2 := pubdata{value: 2}
outdata3 := pubdata{value: 3}
outdata4 := pubdata{value: 4}
ch.Publish(&outdata1, "A")
time.Sleep(polltime / 5)
ch.Publish(&outdata2, "B")
time.Sleep(polltime / 5)
ch.Publish(&outdata3, "C")
time.Sleep(polltime / 5)
ch.Publish(&outdata4, "A")
time.Sleep(polltime / 5)
datach, _ := ch.Get(polltime)
data := <-datach
if time.Now().Sub(start) > 4*polltime/5+tolerance {
t.Errorf("get returned late")
}
if len(data) != 3 || data[0] != &outdata1 || data[1] != &outdata3 || data[2] != &outdata4 {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
}
func TestChannel_onPublish_withAnyMatchingTopic_GetReceives(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A", "B", "C", "D")
defer ch.Drop()
datach, _ := ch.Get(polltime)
time.Sleep(polltime / 2)
outdata := pubdata{value: 351}
ch.Publish(&outdata, "C")
// ch.Publish(&outdata, "D") -- nondeterministic if get gets 1 or 2 due to concurrency
time.Sleep(tolerance)
data := <-datach
if time.Now().Sub(start) > polltime/2+2*tolerance {
t.Errorf("get returned late")
}
if len(data) != 1 || data[0] != &outdata {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
}
func TestChannel_onPublish_withNonmatchingTopic_GetIndifferent(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A", "B", "C", "D")
defer ch.Drop()
datach, _ := ch.Get(polltime)
time.Sleep(polltime / 2)
outdata := pubdata{value: 351}
ch.Publish(&outdata, "Z")
ch.Publish(&outdata, "25")
ch.Publish(&outdata, "foo")
ch.Publish(&outdata, "A")
time.Sleep(tolerance)
data := <-datach
if time.Now().Sub(start) > polltime/2+2*tolerance {
t.Errorf("get returned late")
}
if len(data) != 1 || data[0] != &outdata {
t.Errorf("unexpected data in get")
}
if !ch.IsAlive() {
t.Errorf("channel not upon get polltime exit")
}
}
func TestChannel_onDroppedSub_GetErrors(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A", "B", "C")
outdata := pubdata{value: 351}
ch.Publish(&outdata, "A")
ch.Drop()
time.Sleep(tolerance)
_, err := ch.Get(polltime)
if time.Now().Sub(start) > 2*tolerance {
t.Errorf("get returned late")
}
if err == nil {
t.Errorf("error expected")
}
}
func TestChannel_onDroppedSub_PublishErrors(t *testing.T) {
timeout := 400 * time.Millisecond
tolerance := 25 * time.Millisecond
ch := longpoll.MustNewChannel(timeout, nil, "A", "B", "C")
outdata := pubdata{value: 351}
ch.Publish(&outdata, "A")
ch.Drop()
time.Sleep(tolerance)
err := ch.Publish(&outdata, "B")
if err == nil {
t.Errorf("error expected")
}
}
func TestChannel_onDropRightAfterGet_GetReturnsEmpty(t *testing.T) {
timeout := 400 * time.Millisecond
polltime := 200 * time.Millisecond
tolerance := 25 * time.Millisecond
start := time.Now()
ch := longpoll.MustNewChannel(timeout, nil, "A", "B", "C")
datach, _ := ch.Get(polltime)
ch.Drop()
if len(<-datach) > 0 {
t.Errorf("data coming from nowhere")
}
if time.Now().Sub(start) > tolerance {
t.Errorf("get returned late")
}
}