Skip to content

Commit 9224d0f

Browse files
committed
Clear all transport timeouts on disconnect to avoid holding onto transport object in memory
1 parent 2587cb2 commit 9224d0f

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

lib/sctransport.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ SCTransport.prototype._onClose = function (code, data) {
204204
delete this.socket.onerror;
205205

206206
clearTimeout(this._connectTimeoutRef);
207+
clearTimeout(this._pingTimeoutTicker);
208+
clearTimeout(this._batchTimeout);
207209

208210
if (this.state == this.OPEN) {
209211
this.state = this.CLOSED;

test/integration.js

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ var localStorage = require('localStorage');
66
// Add to the global scope like in browser.
77
global.localStorage = localStorage;
88

9-
var PORT = 8008;
9+
var portNumber = 8008;
1010

1111
var clientOptions;
12-
13-
var serverOptions = {
14-
authKey: 'testkey',
15-
ackTimeout: 200
16-
};
12+
var serverOptions;
1713

1814
var allowedUsers = {
1915
bob: true,
@@ -54,7 +50,12 @@ var connectionHandler = function (socket) {
5450

5551
describe('integration tests', function () {
5652
beforeEach('run the server before start', function (done) {
57-
server = socketClusterServer.listen(PORT, serverOptions);
53+
serverOptions = {
54+
authKey: 'testkey',
55+
ackTimeout: 200
56+
};
57+
58+
server = socketClusterServer.listen(portNumber, serverOptions);
5859
server.on('connection', connectionHandler);
5960

6061
server.addMiddleware(server.MIDDLEWARE_AUTHENTICATE, function (req, next) {
@@ -69,7 +70,7 @@ describe('integration tests', function () {
6970

7071
clientOptions = {
7172
hostname: '127.0.0.1',
72-
port: PORT,
73+
port: portNumber,
7374
multiplex: false,
7475
ackTimeout: 200
7576
};
@@ -97,7 +98,7 @@ describe('integration tests', function () {
9798
}
9899
cleanupTasks.push(new Promise(function (resolve) {
99100
server.close(function () {
100-
PORT++;
101+
portNumber++;
101102
resolve();
102103
});
103104
}));
@@ -840,7 +841,7 @@ describe('integration tests', function () {
840841
it('should destroy all references of socket when socketClusterClient.destroy(socket) is called if the socket was created with query parameters', function () {
841842
var clientOptionsB = {
842843
hostname: '127.0.0.1',
843-
port: PORT,
844+
port: portNumber,
844845
multiplex: true,
845846
ackTimeout: 200,
846847
query: {foo: 123, bar: 456}
@@ -850,7 +851,7 @@ describe('integration tests', function () {
850851

851852
var clientOptionsB = {
852853
hostname: '127.0.0.1',
853-
port: PORT,
854+
port: portNumber,
854855
multiplex: true,
855856
ackTimeout: 200,
856857
query: {foo: 123, bar: 789}
@@ -860,7 +861,7 @@ describe('integration tests', function () {
860861

861862
var clientOptionsB2 = {
862863
hostname: '127.0.0.1',
863-
port: PORT,
864+
port: portNumber,
864865
multiplex: true,
865866
ackTimeout: 200,
866867
query: {foo: 123, bar: 789}
@@ -1237,4 +1238,53 @@ describe('integration tests', function () {
12371238
done();
12381239
});
12391240
});
1241+
1242+
describe('ping/pong', function () {
1243+
it('should disconnect if ping is not received before timeout', function (done) {
1244+
clientOptions.ackTimeout = 500;
1245+
client = socketClusterClient.create(clientOptions);
1246+
1247+
assert.equal(client.pingTimeout, 500);
1248+
1249+
client.on('connect', function () {
1250+
assert.equal(client.transport.pingTimeout, server.options.pingTimeout);
1251+
// Hack to make the client ping independent from the server ping.
1252+
client.transport.pingTimeout = 500;
1253+
});
1254+
1255+
var disconnectCode = null;
1256+
var clientError = null;
1257+
client.on('error', function (err) {
1258+
clientError = err;
1259+
});
1260+
client.on('disconnect', function (code) {
1261+
disconnectCode = code;
1262+
});
1263+
1264+
setTimeout(function () {
1265+
assert.equal(disconnectCode, 4000);
1266+
assert.notEqual(clientError, null);
1267+
assert.equal(clientError.name, 'SocketProtocolError');
1268+
done();
1269+
}, 1000);
1270+
});
1271+
1272+
it('should not disconnect if ping is not received before timeout when pingTimeoutDisabled is true', function (done) {
1273+
clientOptions.ackTimeout = 500;
1274+
clientOptions.pingTimeoutDisabled = true;
1275+
client = socketClusterClient.create(clientOptions);
1276+
1277+
assert.equal(client.pingTimeout, 500);
1278+
1279+
var clientError = null;
1280+
client.on('error', function (err) {
1281+
clientError = err;
1282+
});
1283+
1284+
setTimeout(function () {
1285+
assert.equal(clientError, null);
1286+
done();
1287+
}, 1000);
1288+
});
1289+
});
12401290
});

0 commit comments

Comments
 (0)