-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
reconnection.js
51 lines (40 loc) · 1005 Bytes
/
reconnection.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
var inject = require('..')
var test = require('tape')
var Stream = require('stream')
test('reconnect', function (t) {
t.plan(18)
var start = false
var times = 0
var reconnect = inject(function () {
var s = new Stream
if (start) {
t.ok(true, 'start server')
if (++times >= 2) {
reconnector.reconnect = false
}
process.nextTick(function () {
s.emit('connect')
});
}
process.nextTick(function () {
s.emit('end')
})
return s
})
var reconnector = reconnect({initialDelay: 10}, function () {
t.ok(true, 'reconnect cb fired')
})
function onConnect () {
t.ok(reconnector.connected, 'client connected')
}
function onReconnect (n) {
t.ok(true, 'reconnect ' + n)
t.notOk(reconnector.connected, 'not connected')
t.ok(reconnector.reconnect, 'will reconnect')
if (n == 1) start = true;
}
reconnector
.on('connect', onConnect)
.on('reconnect', onReconnect)
.connect()
})