-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask-polling-hangup-pubsub-test.js
118 lines (90 loc) · 3.28 KB
/
task-polling-hangup-pubsub-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
#!/usr/bin/env node
/*
* Spade, test for polling task in pubsub mode.
*/
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( '../' )
, opt = {
socket : {
address : {
port : 6380
}
}
, security : {
'127.0.0.1:6380' : {
requirepass : 'secret'
}
}
, queue : {
timestamps : true
}
}
, client = Spade( opt )
, Vapid = null
, vp = null
, vapid_opt = {
secret : 'secret'
, maxdb : 16
}
, vport = 6380
// expected events
, evts = []
// collected events
, collected = client.logger.collected
, intval = 2000
, exit = typeof done === 'function' ? done : function () {}
, assert = assertions || require( 'assert' )
;
try {
Vapid = require( 'vapid' );
vp = Vapid( vapid_opt );
} catch ( e ) {
log( '- this test needs Vapid devDependency(see Readme): %s.', e.message );
return;
}
log( '- a new Spade client was created with default options:', inspect( client.options ) );
log( '- enable Vapid server, now it is listening on port: %s.', inspect( vport ) );
// vapid.cli();
vp.listen( vport );
log( '- enable CLI logging.' );
client.cli( true, function ( ename, args ) {
dbg( ' !%s %s', ename, format( ename, args || [] ) );
}, true );
log( '- now #initTasks.' );
client.initTasks();
log( '- opening client connection.' );
client.connect( null, function () {
log( '- now client is connected and ready to send.' );
// push expected events
evts.push( 'connect', 'reply', 'authorized', 'reply', 'dbselected', 'scanqueue', 'ready' );
evts.push( 'listen', 'message' );
log( '- client enters in pubsub mode.' );
client.commands.subscribe( 'a', function () {
client.on( 'polling', function () {
if ( vp.silent ) return;
log( '- now #mute Vapid Server.. ');
vp.mute();
} );
// push expected events
evts.push( 'polling', 'hangup', 'offline', 'lost' );
log( '- run polling task, with reconnect option set to %s, hanghup timeout is set to %s secs.', inspect( false ), inspect( 2 ) );
client.tasks.polling.run( intval, [ null, 'BANG!', 1000, false ] );
log( '- now waiting %s secs to collect events..', inspect( 5 ) );
setTimeout( function () {
log( '- check collected events for client, should be:', inspect( evts ) );
assert.deepEqual( collected.events, evts, 'got: ' + inspect( collected.events ) );
vp.close();
exit();
}, 5000 );
} );
} );
};
// single test execution with node
if ( process.argv[ 1 ] === __filename ) exports.test = exports.test();