-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathclient.presence.test.js
569 lines (499 loc) · 19.6 KB
/
client.presence.test.js
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* globals describe, it, beforeEach, afterEach, before, after */
const common = require('./common.js')
const assert = require('assert')
const Tracker = require('callback_tracker')
const { PresenceMessage } = require('./lib/assert_helper.js')
let radar
let client
let client2
let client3
describe('given two clients and a presence resource', function () {
let p
before(function (done) {
radar = common.spawnRadar()
radar.sendCommand('start', common.configuration, done)
})
after(function (done) {
common.stopRadar(radar, done)
})
beforeEach(function (done) {
p = new PresenceMessage('dev', 'test')
const track = Tracker.create('beforeEach', done)
client = common.getClient('dev', 123, 0, { name: 'tester' }, track('client 1 ready'))
client2 = common.getClient('dev', 246, 0, { name: 'tester2' }, track('client 2 ready'))
client3 = common.getClient('dev', 300, 0, {}, track('client 3 ready'))
})
afterEach(function () {
p.teardown()
client.presence('test').set('offline').removeAllListeners()
client2.presence('test').set('offline').removeAllListeners()
client3.presence('test').set('offline').removeAllListeners()
client.dealloc('test')
client2.dealloc('test')
client3.dealloc('test')
})
describe('when using subscribe/unsubscribe', function () {
it('can subscribe a presence scope', function (done) {
client.presence('test').on(p.notify).subscribe(function (message) {
p.for_client(client).assert_ack_for_subscribe(message)
client2.presence('test').set('online')
client2.presence('test').set('offline')
})
p.fail_on_more_than(4)
p.once(4, function () {
// Ensure that no more messages come
setTimeout(function () {
p.for_client(client2).assert_message_sequence([
'online', 'client_online', 'client_explicit_offline', 'offline'
])
done()
}, 5)
})
})
it('can unsubscribe a presence scope', function (done) {
client.presence('test').on(p.notify).subscribe()
client2.presence('test').set('online')
p.fail_on_more_than(2)
p.on(2, function () {
// Online and client_online
p.for_client(client2).assert_message_sequence(
['online', 'client_online']
)
client.presence('test').unsubscribe(function (message) {
p.for_client(client).assert_ack_for_unsubscribe(message)
client2.presence('test').set('offline')
setTimeout(done, 10)
})
})
})
describe('with a client subscribed', function () {
it('should receive presence notifications if a client comes online', function (done) {
// Subscribe online with client 2
client2.presence('test').on(p.notify).subscribe(function () {
// Set client 1 to online
client.presence('test').set('online', function () {
client2.presence('test').get(function (message) {
// Should show client 1 as online
p.for_online_clients(client).assert_get_response(message)
p.for_client(client).assert_message_sequence(
['online', 'client_online']
)
done()
})
})
})
})
it('should not receive any notification if an offline client unsubscribes', function (done) {
p.fail_on_any_message()
// Subscribe online with client 2
client2.presence('test').on(p.notify).subscribe(function () {
// client 1 disconnect
client.presence('test').unsubscribe()
setTimeout(done, 20)
})
})
it('should not receive notifications after we unsubscribe', function (done) {
// Subscribe online with client 2
client2.presence('test').on(p.notify).subscribe(function () {
client.presence('test').set('online')
})
p.fail_on_more_than(2)
p.on(2, function () {
p.for_client(client).assert_message_sequence(['online', 'client_online'])
client2.presence('test').unsubscribe()
client.presence('test').set('online')
setTimeout(done, 20)
})
})
})
})
describe('when using set()', function () {
it('can set online', function (done) {
client.presence('test').set('online', function (ack) {
p.for_client(client).assert_ack_for_set_online(ack)
client.presence('test').get(function (message) {
p.for_online_clients(client).assert_get_response(message)
done()
})
})
})
it('can set offline without setting online first', function (done) {
client.presence('test').set('offline', function (ack) {
p.for_client(client).assert_ack_for_set_offline(ack)
client.presence('test').get(function (message) {
p.for_online_clients().assert_get_response(message)
done()
})
})
})
it('can set offline after setting online', function (done) {
p = p.for_client(client)
client.presence('test').set('online', function (ack) {
p.assert_ack_for_set_online(ack)
client.presence('test').set('offline', function (ack) {
p.assert_ack_for_set_offline(ack)
client.presence('test').get(function (message) {
p.for_online_clients().assert_get_response(message)
done()
})
})
})
})
it('does not implicitly subscribe to the presence', function (done) {
p = p.for_client(client)
p.fail_on_any_message(0)
client.presence('test').set('online', function () {
setTimeout(done, 500)
})
})
describe('with another client listening for notifications', function () {
it('should remain online after a while if the state does not change', function (done) {
client.presence('test').set('online', function () {
const presence = client2.presence('test').sync({ version: 2 }, function (message) {
p.for_online_clients(client).assert_sync_v2_response(message)
// Reread after a while
setTimeout(function () {
presence.get({ version: 2 }, function (message) {
p.for_online_clients(client).assert_get_v2_response(message)
done()
})
}, 1500)
})
})
})
it('should not send notifications for a set(offline) if already offline', function (done) {
p.fail_on_any_message()
// Subscribe online with client 2
client2.presence('test').on(p.notify).subscribe(function () {
client.presence('test').set('offline')
setTimeout(done, 50)
})
})
it('should send online notification only once for multiple set(online)', function (done) {
// Subscribe online with client 2
client2.presence('test').on(p.notify)
.subscribe(function () {
client.presence('test').set('online')
client.presence('test').set('online')
client.presence('test').set('online')
setTimeout(done, 50)
})
p.fail_on_more_than(2)
p.on(2, function () {
p.for_client(client).assert_message_sequence(['online', 'client_online'])
})
})
it('should send online notification only once for multiple set(online), unless updated clientData', function (done) {
const clientData = { data: 2 }
// Subscribe online with client 2
client2.presence('test').on(p.notify)
.subscribe(function () {
client.presence('test').set('online')
client.presence('test').set('online', clientData)
setTimeout(done, 50)
})
p.fail_on_more_than(3)
p.on(3, function () {
p.for_client(client).assert_message_sequence([
['online'],
['client_online'],
['client_updated', clientData]
])
})
})
it('should send online notification only once for multiple set(online), unless updated clientData, ' +
'but only if it differs', function (done) {
const clientData = { data: 2 }
const clientData2 = { data: 3 }
client2.presence('test').on(p.notify)
.subscribe(function () {
client.presence('test').set('online')
client.presence('test').set('online', clientData)
client.presence('test').set('online', clientData2)
client.presence('test').set('online', clientData2)
setTimeout(done, 50)
})
p.fail_on_more_than(4)
p.on(4, function () {
p.for_client(client).assert_message_sequence([
['online'],
['client_online'],
['client_updated', clientData],
['client_updated', clientData2]
])
})
})
it('should send presence messages correctly when toggling back and forth', function (done) {
let expected = []
const count = 8
const toggle = function (index) {
if (index % 2 === 0) {
expected = expected.concat(['online', 'client_online'])
client.presence('test').set('online')
} else {
expected = expected.concat(['client_explicit_offline', 'offline'])
client.presence('test').set('offline')
}
}
client2.presence('test')
.on(p.notify)
.subscribe(function () {
for (let i = 0; i < count; i++) {
setTimeout(toggle, 10, i)
}
})
const verify = function () {
assert.strictEqual(2 * count, p.notifications.length)
p.for_client(client).assert_message_sequence(expected)
done()
}
p.on(count * 2, function () {
setTimeout(verify, 50)
})
})
it('should implicitly disconnect after set(online) if unsubscribed', function (done) {
client2.presence('test').on(p.notify).subscribe(function () {
client.presence('test').set('online')
})
p.on(2, function () {
p.for_client(client).assert_message_sequence(
['online', 'client_online']
)
client.presence('test').unsubscribe()
})
p.on(4, function () {
p.for_client(client).assert_message_sequence([
'online', 'client_online', 'client_implicit_offline', 'offline'
])
done()
})
})
it('should implicitly disconnect after set(online) if unsubscribed, after default user expiry', function (done) {
this.timeout(16000)
p = new PresenceMessage('dev', 'no_default')
client2.presence('no_default').on(p.notify).subscribe(function () {
client.presence('no_default').set('online')
})
p.on(2, function () {
p.for_client(client).assert_message_sequence(
['online', 'client_online']
)
client.presence('no_default').unsubscribe()
})
p.on(4, function () {
p.for_client(client).assert_message_sequence([
'online', 'client_online', 'client_implicit_offline', 'offline'
])
// Ensure time between implicit_offline and offline is within range
p.assert_delay_between_notifications_within_range(2, 3, 14500, 15500)
done()
})
})
it('should notify correctly if disconnecting immediately after online', function (done) {
client2.presence('test').on(p.notify).subscribe(function () {
client.presence('test').set('online')
client.presence('test').unsubscribe()
})
p.on(4, function () {
p.for_client(client).assert_message_sequence(
['online', 'client_online', 'client_implicit_offline', 'offline']
)
done()
})
})
})
})
describe('when using get()', function () {
it('should respond correctly when using v1 API', function (done) {
client.presence('test').get(function (message) {
p.for_online_clients().assert_get_response(message)
client.presence('test').set('online', function () {
client.presence('test').get(function (message) {
p.for_online_clients(client).assert_get_response(message)
done()
})
})
})
})
it('should respond correctly when using v2 API (with userData)', function (done) {
client.presence('test').get({ version: 2 }, function (message) {
p.for_online_clients().assert_get_v2_response(message)
client.presence('test').set('online', function () {
client.presence('test').get({ version: 2 }, function (message) {
p.for_online_clients(client).assert_get_v2_response(message)
done()
})
})
})
})
it('should respond correctly when using v2 API (without userData)', function (done) {
client3.presence('test').get({ version: 2 }, function (message) {
p.for_online_clients().assert_get_v2_response(message)
client3.presence('test').set('online', function () {
client3.presence('test').get({ version: 2 }, function (message) {
p.for_online_clients(client3).assert_get_v2_response(message)
done()
})
})
})
})
it('should respond correctly when using v2 API (with clientData)', function (done) {
const clientData = { 1: '1' }
client.presence('test').get({ version: 2 }, function (message) {
p.for_online_clients().assert_get_v2_response(message)
client.presence('test').set('online', clientData, function () {
client.presence('test').get({ version: 2 }, function (message) {
p.for_online_clients(client).assert_get_v2_response(message, clientData)
done()
})
})
})
})
it('can be called multiple times without changing the result', function (done) {
this.timeout(6000)
client2.presence('test').on(p.notify).subscribe(function () {
// Set client 1 to online
client.presence('test').set('online', function () {
const foo = setInterval(function () {
client2.presence('test').get(function (message) {
// Both should show client 1 as online
p.for_online_clients(client).assert_get_response(message)
p.for_client(client).assert_message_sequence(
['online', 'client_online']
)
})
}, 200)
setTimeout(function () {
clearInterval(foo)
done()
}, 2000)
})
})
})
})
describe('when using sync', function () {
it('should respond correclty when using via v1 API', function (done) {
client.presence('test').set('online', function () {
client.presence('test').sync(function (message) {
p.for_online_clients(client).assert_sync_response(message)
done()
})
})
})
it('should respond correctly when using via v2 API (with userData)', function (done) {
// Not supported in v1 api because the result.op == "online" which is handled
// by the message listener but not by the sync() callbackFn
client.presence('test').set('online', function () {
client.presence('test').sync({ version: 2 }, function (message) {
// Sync is implemented as subscribe + get, hence the return op is "get"
p.for_online_clients(client).assert_sync_v2_response(message)
done()
})
})
})
it('should respond correctly when using via v2 API (without userData)', function (done) {
// Not supported in v1 api because the result.op == "online" which is handled
// by the message listener but not by the sync() callbackFn
client3.presence('test').set('online', function () {
client3.presence('test').sync({ version: 2 }, function (message) {
// Sync is implemented as subscribe + get, hence the return op is "get"
p.for_online_clients(client3).assert_sync_v2_response(message)
done()
})
})
})
it('should respond correctly when using v2 API (with clientData)', function (done) {
const clientData = { 1: '1' }
client3.presence('test').set('online', clientData, function () {
client3.presence('test').sync({ version: 2 }, function (message) {
// Sync is implemented as subscribe + get, hence the return op is "get"
p.for_online_clients(client3).assert_sync_v2_response(message, clientData)
done()
})
})
})
it('should also subscribe to that resource', function (done) {
client.presence('test').on(p.notify).sync(function (message) {
// Wait for sync to complete
p.for_online_clients().assert_sync_response(message)
client.presence('test').set('online')
})
p.fail_on_more_than(2)
p.on(2, function () {
p.for_client(client).assert_message_sequence(['online', 'client_online'])
setTimeout(done, 10)
})
})
it('can be called multiple times without changing the result', function (done) {
this.timeout(6000)
client2.presence('test').on(p.notify).subscribe(function () {
// Set client 1 to online
client.presence('test').set('online', function () {
const foo = setInterval(function () {
client2.presence('test').sync(function (message) {
// Both should show client 1 as online
p.for_online_clients(client).assert_sync_response(message)
// Not more than 2 notifications ever
p.for_client(client).assert_message_sequence(
['online', 'client_online'])
})
}, 200)
setTimeout(function () {
clearInterval(foo)
done()
}, 2000)
})
})
})
})
it('should implicitly disconnect after a request timeout', function (done) {
this.timeout(55 * 1000)
client.presence('test').on(p.notify).subscribe(function () {
client2.presence('test').set('online')
setTimeout(function () {
// Hack a bit so that clientSessionId is saved
const clientId = client2.currentClientId()
client2.currentClientId = function () { return clientId }
// Disconnect, causing a request timeout or socket close
client2.manager.close()
}, 10)
})
p.on(4, function () {
p.for_client(client2).assert_message_sequence(
['online', 'client_online', 'client_implicit_offline', 'offline']
)
done()
})
})
it('should keep a user online for a grace period for disconnections', function (done) {
client2.presence('test').on(p.notify).subscribe(function () {
client.presence('test').set('online')
})
p.on(2, function () {
p.for_client(client).assert_message_sequence(
['online', 'client_online']
)
// Hack a bit so that clientSessionId is saved
const clientId = client.currentClientId()
client.currentClientId = function () { return clientId }
client.dealloc('test')
const time = Date.now()
setTimeout(function () {
client2.presence('test').get(function (message) {
p.for_online_clients(client).assert_get_response(message)
})
p.on(4, function () {
// Timeout is 1 second
assert.ok(Date.now() - time > 950)
p.for_client(client).assert_message_sequence([
'online', 'client_online', 'client_implicit_offline', 'offline'
])
client2.presence('test').get(function (message) {
p.for_online_clients().assert_get_response(message)
client.alloc('test', done)
})
})
}, 900)
})
})
})