Skip to content

Commit

Permalink
[Echo API WS] clear timers if WS socket close the connection unexpect…
Browse files Browse the repository at this point in the history
…edly
Maksim Terekhin committed Jun 2, 2014
1 parent a7ccdbc commit fedeb06
Showing 2 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -190,6 +190,7 @@ Echo.API.Transports.WebSockets.prototype._getTransportObject = function() {
self.config.get(topic)(data);
if (topic === "onClose") {
clearTimeout(self.timers.close);
self._clearTimers();
}
},
// when we receive data - send it to the appropriate
58 changes: 53 additions & 5 deletions tests/unit/api.js
Original file line number Diff line number Diff line change
@@ -129,23 +129,71 @@ if (Echo.API.Transports.WebSockets.available()) {
QUnit.ok($.isEmptyObject(Echo.API.Transports.WebSockets.socketByURI[req.transport.config.get("uri")].subscribers), "Check that all subscription removed in case of all requests abortion");
$.when.apply($, closeDef).done(function() {
QUnit.strictEqual(closed, 4, "Check that all subscribed connections are closed (\"onClose\" event fired)");
// wait for socket closing
setTimeout(function() {
QUnit.ok($.isEmptyObject(Echo.API.Transports.WebSockets.socketByURI), "Check that WS static instance container is cleared");
QUnit.strictEqual(
$.grep(requests, function(req) {
return req.transport.closed();
}).length,
4, "Check that each statuses of the requests are \"closed\""
);
QUnit.strictEqual(
$.grep(requests, function(req) {
return $.isEmptyObject(req.transport.timers);
}).length,
4, "Check that all timers are cleared after closing a connection"
);
QUnit.start();
}, 0);
});
});
}, {"timeout": 10000});

Echo.Tests.asyncTest("WebSocket: immediate close socket", function() {
var closeDef = [];
var requests = [];
for(var i = 0; i < 2; i++) {
closeDef.push($.Deferred());
/* jshint loopfunc: true */
(function(i) {
requests.push(
new Echo.API.Request({
"endpoint": "ws",
"apiBaseURL": "live.echoenabled.com/v1/",
"transport": "websockets",
"onOpen": function() {
if (i === 1) {
requests[0].transport.transportObject.close();
}
},
"onClose": function() {
closeDef[i].resolve();
}
})
);
})(i);
/* jshint loopfunc: false */
}
$.when.apply($, closeDef).done(function() {
setTimeout(function() {
QUnit.ok($.isEmptyObject(Echo.API.Transports.WebSockets.socketByURI), "Check that WS static instance container is cleared");
QUnit.strictEqual(
$.grep(requests, function(req) {
return req.transport.closed();
}).length,
4, "Check that each statuses of the requests are \"closed\""
2, "Check that each statuses of the requests are \"closed\""
);
QUnit.strictEqual(
$.grep(requests, function(req) {
return $.isEmptyObject(req.timers);
return $.isEmptyObject(req.transport.timers);
}).length,
4, "Check that all timers are cleared after closing a connection"
2, "Check that all timers are cleared after closing a connection"
);
QUnit.start();
});
}, 0);
});
}, {"timeout": 10000});
});
}

Echo.Tests.test("Transports JSONP method POST", function() {

0 comments on commit fedeb06

Please sign in to comment.