forked from foreversd/nssocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-reconnect-test.js
80 lines (74 loc) · 2.5 KB
/
tcp-reconnect-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
/*
* nssocket-test.js : namespace socket unit test for TCP
*
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
*
*/
var assert = require('assert'),
fs = require('fs'),
net = require('net'),
path = require('path'),
vows = require('vows'),
NsSocket = require('../lib/nssocket').NsSocket;
var TCP_PORT = 30105;
var tcpServer = net.createServer(),
tcpOpt;
tcpOpt = {
type : 'tcp4',
delimiter: '.}',
reconnect: true,
retryInterval: 1000
};
tcpServer.listen(TCP_PORT);
vows.describe('nssocket/tcp/reconnect').addBatch({
"When using NsSocket with TCP": {
topic: new NsSocket(tcpOpt),
"the connect() method": {
topic: function (outbound) {
var that = this;
tcpServer.on('connection', this.callback.bind(null, null, outbound));
outbound.connect(TCP_PORT);
},
"should actually connect": function (_, outbound, inbound) {
assert.instanceOf(outbound, NsSocket);
assert.instanceOf(inbound, net.Socket);
},
"when the server closes": {
topic: function (outbound, inbound) {
outbound.once('close', this.callback.bind(this, null, outbound));
tcpServer.close();
inbound.destroy();
},
"and then restarts": {
topic: function (outbound) {
tcpServer = net.createServer();
tcpServer.listen(TCP_PORT);
tcpServer.on('connection', this.callback.bind(null, null, outbound));
},
"the socket should reconnect correctly": function (_, outbound, inbound) {
assert.instanceOf(outbound, NsSocket);
assert.instanceOf(inbound, net.Socket);
},
"the on() method": {
topic: function (outbound, inbound) {
outbound.on('data.}here.}is', this.callback.bind(outbound, null));
inbound.write(JSON.stringify(['here', 'is', 'something.']) + '\n');
},
"should handle namespaced events": function (_, data) {
assert.isArray(this.event);
assert.lengthOf(this.event, 3);
assert.isString(this.event[0]);
assert.isString(this.event[1]);
assert.isString(this.event[2]);
assert.isString(data);
assert.equal(this.event[0], 'data');
assert.equal(this.event[1], 'here');
assert.equal(this.event[2], 'is');
assert.equal(data, 'something.');
}
}
}
}
}
}
}).export(module);