-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
connection.js
58 lines (50 loc) · 1.21 KB
/
connection.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
var inject = require('..')
var test = require('tape')
var Stream = require('stream')
var Emitter = require('events').EventEmitter
var noop = function () {}
test('connection', function (t) {
t.plan(4) // reconnect and connect
var ee = new Emitter
var reconnect = inject(function () {
var s = new Stream
ee.on('start server', function () {
s.emit('connect')
s.emit('close')
})
s.end = noop
return s
})
var reconnector = reconnect({initialDelay: 10}, function (stream) {
reconnector.reconnect = false
reconnector.disconnect()
})
var connected = false
var onConnect = function () {
connected = true
t.ok(reconnector.connected)
}
var onReconnect = function (n) {
t.notOk(connected)
t.notOk(reconnector.connected)
t.ok(reconnector.reconnect)
if (n < 4) process.nextTick(function () {
ee.emit('start server')
})
}
reconnector
.on('connect', onConnect)
.on('reconnect', onReconnect)
.connect()
})
test('this arg', function (t) {
var thisArg
var reconnect = inject(function () {
thisArg = this
return new Stream
})
var reconnector = reconnect()
reconnector.connect()
t.equal(thisArg, reconnector)
t.end()
})