Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 7740e60

Browse files
committed
Merge branch 'test/reconnection'
2 parents d97e1d7 + 7acf202 commit 7740e60

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

test/helpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@ var MockIrcd = function(port, encoding, isSecure) {
2828
this.outgoing = [];
2929

3030
this.server = connectionClass.createServer(options, function(c) {
31+
var active = true;
3132
c.on('data', function(data) {
3233
var msg = data.toString(self.encoding).split('\r\n').filter(function(m) { return m; });
3334
self.incoming = self.incoming.concat(msg);
3435
});
3536

3637
self.on('send', function(data) {
38+
if (!active || c.destroyed) return;
3739
self.outgoing.push(data);
3840
c.write(data);
3941
});
4042

4143
c.on('end', function() {
44+
active = false;
4245
self.emit('end');
4346
});
4447
});

test/test-reconnect.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var net = require('net');
2+
3+
var irc = require('../lib/irc');
4+
var test = require('tape');
5+
6+
var testHelpers = require('./helpers');
7+
8+
['when ended', 'when connection breaks'].forEach(function(modifier) {
9+
test('it reconnects with exactly one connection at a time ' + modifier, function(t) {
10+
var mock = testHelpers.MockIrcd();
11+
var client = new irc.Client('localhost', 'testbot', {debug: true, retryDelay: 50});
12+
13+
var conn = null;
14+
var registerCount = 0;
15+
16+
mock.server.on('connection', function(c) {
17+
conn = c;
18+
mock.send(':localhost 001 testbot :Welcome to the Internet Relay Chat Network testbot\r\n');
19+
});
20+
21+
var firstTime = function() {
22+
if (modifier == 'when ended') {
23+
// trigger client connection end, like connection interrupted
24+
client.end();
25+
} else if (modifier == 'when connection breaks') {
26+
// break connection from server end, like connection error
27+
conn.destroy();
28+
} else {
29+
throw new Error("Unexpected test modifier");
30+
}
31+
32+
client.once('registered', secondTime);
33+
34+
// reconnects after 50ms, should take less than 450ms to connect, so end the test after that
35+
setTimeout(function() {
36+
killServer();
37+
}, 500);
38+
};
39+
40+
var secondTime = function() {
41+
// further connections should be considered bad
42+
mock.server.on('connection', function() {
43+
t.ok(false);
44+
});
45+
}
46+
47+
var killServer = function() {
48+
t.equal(registerCount, 2, 'connected to server exactly twice');
49+
t.end();
50+
client.disconnect();
51+
mock.close();
52+
}
53+
54+
client.once('registered', firstTime);
55+
client.on('registered', function() { registerCount += 1; });
56+
});
57+
});

0 commit comments

Comments
 (0)