-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
91 lines (82 loc) · 2.37 KB
/
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
const tap = require('tap')
const ReconnectingSocket = require('./index')
const net = require('net')
tap.test('yolo hook it all up test', t => {
t.plan(9)
const connections = []
const server = net.createServer((socket) => {
console.log('client connected')
connections.push(socket)
socket.on('data', function (buf) {
t.pass(`server received a message: ${buf.toString('utf8')}`)
socket.write(buf.toString('utf8').toUpperCase())
})
socket.on('close', () => {
t.pass('server closed')
connections.splice(connections.find(s => s === socket), 1)
})
socket.on('error', err => t.error(err, 'socket should not error'))
})
server.on('error', (err) => {
t.fail(err, 'server should not emit errors')
})
function closeAllConnections () {
connections.forEach(socket => socket.destroy())
}
const firstConnectPath = (buf) => {
t.equal(buf.toString('utf8'), 'PING1', 'uppercase ping1')
server.close()
closeAllConnections()
setTimeout(() => {
console.log('server starting again')
server.listen(8124)
}, 3000)
}
const secondConnectPath = (buf) => {
t.equal(buf.toString('utf8'), 'PING2', 'uppercase ping2')
server.close()
reconnectingTCP.stop()
setTimeout(() => {
reconnectingTCP.start()
}, 500)
}
const reconnectingTCP = new ReconnectingSocket({
backoff: {
failAfter: 4
},
create () {
const socket = new net.Socket()
socket.on('close', this.close)
socket.on('error', this.error)
socket.on('connect', this.open)
socket.connect(8124)
return socket
},
destroy (socket) {
socket.destroy()
},
onopen (socket, firstOpen) {
if (firstOpen) {
t.pass('fisrtOpen socket ran')
socket.on('data', firstConnectPath)
socket.write('ping1')
} else {
t.pass('!fisrtOpen socket ran')
socket.on('data', secondConnectPath)
socket.write('ping2')
}
},
onclose (socket) {
socket.removeListener('data', firstConnectPath)
socket.removeListener('data', secondConnectPath)
},
onfail (err) {
t.equals(err.message, 'connect ECONNREFUSED 127.0.0.1:8124', 'can\'t connect message looks good')
}
})
reconnectingTCP.on('info', console.log)
server.listen(8124, () => {
console.log('server started')
reconnectingTCP.start()
})
})