-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubsub-multiple-sub-multiple-unsub-test.js
103 lines (80 loc) · 3.27 KB
/
pubsub-multiple-sub-multiple-unsub-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
#!/usr/bin/env node
/*
* Spade, pubsub mode events test.
*/
exports.test = function ( done, assertions ) {
var debug = !! true
, emptyFn = function () {}
, log = console.log
, dbg = debug ? console.log : emptyFn
, test_utils = require( './deps/test-utils' )
, inspect = test_utils.inspect
, format = test_utils.format
, Spade = require( '../' )
, client = Spade()
// expected events
, evts = []
// collected events
, collected = client.logger.collected
// channels
, channels = [ 'a', 'a', 'b', 'b', 'c', 'c' ]
, exit = typeof done === 'function' ? done : function () {}
, assert = assertions || require( 'assert' )
;
log( '- a new Spade client was created with default options.' );
log( '- enable CLI logging.' );
client.cli( true, function ( ename, args ) {
dbg( ' !%s %s', ename, format( ename, args || [] ) );
}, true );
log( '- execute/enqueue SUBSCRIBE command in offline mode.' );
log( '- now connecting client.' );
// push expected events
evts.push( 'connect', 'reply', 'dbselected', 'scanqueue', 'ready' );
client.connect( null, function () {
var i = 0
;
client.commands.subscribe( channels, function () {
log( '- I\'m SUBSCRIBE callback.' );
} );
log( '- try to execute a TIME command in pubsub mode.' );
// push expected events
evts.push( 'error' );
evts.push( 'listen' );
for ( ; i < channels.length + 3; ++i ) evts.push( 'message' );
// push a message after shutup for the last unsubscribe message
evts.push( 'shutup' );
evts.push( 'message' );
client.commands.time( function ( is_err, reply, fn ) {
log( '- TIME callback should get an error.' );
assert.ok( is_err );
} );
client.on( 'shutup', function () {
// push expected reply event from PING
evts.push( 'reply' );
client.commands.ping( function ( is_err, reply, fn ) {
log( '- PING callback should get PONG reply, got:', fn( reply )[ 0 ] );
assert.ok( fn( reply )[ 0 ] === 'PONG' );
log( '- now disconnecting clients with QUIT.' );
// push expected reply event from QUIT
evts.push( 'reply' );
// push expected reply events for disconnection
evts.push( 'offline', 'lost' );
client.commands.quit( function ( is_err, reply, fn ) {
log( '- client QUIT callback.', fn( reply ) );
assert.ok( fn( reply )[ 0 ] === 'OK' );
} );
} );
} );
client.commands.unsubscribe( [ 'a', 'b', 'c', 'd' ], function () {
log( '- I\'m UNSUBSCRIBE callback.' );
} );
} );
log( '- wait 2 seconds to collect events..' );
setTimeout( function () {
log( '- deep check collected events, should be:', inspect( evts ) );
assert.deepEqual( collected.events, evts, 'got: ' + inspect( collected.events ) );
exit();
}, 2000 );
};
// single test execution with node
if ( process.argv[ 1 ] === __filename ) exports.test = exports.test();