From 6753610b05bf36e03eabe97fb3d819faa55412c3 Mon Sep 17 00:00:00 2001 From: Tom Connell Date: Fri, 8 Jul 2022 15:41:47 -0600 Subject: [PATCH 1/3] =?UTF-8?q?Use=20the=20provided=20timeout=20option,=20?= =?UTF-8?q?if=20set,=20for=20the=20InfoReceiver.=20=E2=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/info-receiver.js | 11 +++++++---- lib/main.js | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/info-receiver.js b/lib/info-receiver.js index afefea5c..d5de7df8 100644 --- a/lib/info-receiver.js +++ b/lib/info-receiver.js @@ -16,13 +16,13 @@ if (process.env.NODE_ENV !== 'production') { debug = require('debug')('sockjs-client:info-receiver'); } -function InfoReceiver(baseUrl, urlInfo) { +function InfoReceiver(baseUrl, urlInfo, timeout) { debug(baseUrl); var self = this; EventEmitter.call(this); setTimeout(function() { - self.doXhr(baseUrl, urlInfo); + self.doXhr(baseUrl, urlInfo, timeout); }, 0); } @@ -47,7 +47,7 @@ InfoReceiver._getReceiver = function(baseUrl, url, urlInfo) { return new InfoAjax(url, XHRFake); }; -InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) { +InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo, timeout) { var self = this , url = urlUtils.addPath(baseUrl, '/info') ; @@ -55,11 +55,14 @@ InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) { this.xo = InfoReceiver._getReceiver(baseUrl, url, urlInfo); + if (timeout === undefined) { + timeout = InfoReceiver.timeout + } this.timeoutRef = setTimeout(function() { debug('timeout'); self._cleanup(false); self.emit('finish'); - }, InfoReceiver.timeout); + }, timeout); this.xo.once('finish', function(info, rtt) { debug('finish', info, rtt); diff --git a/lib/main.js b/lib/main.js index 90dc0428..48fe2847 100644 --- a/lib/main.js +++ b/lib/main.js @@ -121,7 +121,7 @@ function SockJS(url, protocols, options) { , sameScheme: urlUtils.isSchemeEqual(this.url, loc.href) }; - this._ir = new InfoReceiver(this.url, this._urlInfo); + this._ir = new InfoReceiver(this.url, this._urlInfo, options.timeout); this._ir.once('finish', this._receiveInfo.bind(this)); } From 8b1035484fdaac3c3fba1beed617126498836b07 Mon Sep 17 00:00:00 2001 From: Tom Connell Date: Tue, 12 Jul 2022 13:38:02 -0600 Subject: [PATCH 2/3] Add test of configurable timeout. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is only a bit awkward. 😳 --- tests/lib/receivers.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/lib/receivers.js b/tests/lib/receivers.js index 823c9698..ed564bd6 100644 --- a/tests/lib/receivers.js +++ b/tests/lib/receivers.js @@ -5,7 +5,8 @@ var expect = require('expect.js') , XhrReceiver = require('../../lib/transport/receiver/xhr') , XhrFake = require('../../lib/transport/sender/xhr-fake') , utils = require('../../lib/utils/iframe') - ; + , InfoAjax = require('../../lib/info-ajax') + , InfoReceiver = require('../../lib/info-receiver'); describe('Receivers', function () { describe('jsonp', function () { @@ -288,4 +289,43 @@ describe('Receivers', function () { xhr.abort(); }); }); + describe('info', function () { + it('will timeout - default timeout', function (done) { + this.timeout(10000); + InfoReceiver.prototype._getReceiver = function(baseUrl, url) { + return new InfoAjax(url, XhrFake); + }; + + var expectedWasClean = true; + InfoReceiver.prototype._cleanup = function(wasClean) { + expect(wasClean).to.equal(expectedWasClean); + if (expectedWasClean === false) { + // Cleanup was called because of a timeout + done(); + } + expectedWasClean = false; + }; + + new InfoReceiver('test', {}, 4000); + }); + + it('will timeout - configured timeout', function (done) { + this.timeout(5000); + InfoReceiver.prototype._getReceiver = function(baseUrl, url) { + return new InfoAjax(url, XhrFake); + }; + + var expectedWasClean = true; + InfoReceiver.prototype._cleanup = function(wasClean) { + expect(wasClean).to.equal(expectedWasClean); + if (expectedWasClean === false) { + // Cleanup was called because of a timeout + done(); + } + expectedWasClean = false; + }; + + new InfoReceiver('test', {}, 4000); + }); + }); }); From 90dd919c8cd0ee093486cf7ec3edd7b8bb933251 Mon Sep 17 00:00:00 2001 From: Tom Connell Date: Tue, 12 Jul 2022 13:40:05 -0600 Subject: [PATCH 3/3] =?UTF-8?q?Make=20the=20configured=20test=20faster,=20?= =?UTF-8?q?and=20remove=20my=20accidental=20configuration=20in=20the=20def?= =?UTF-8?q?ault=20case.=20=20=F0=9F=A5=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/lib/receivers.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/lib/receivers.js b/tests/lib/receivers.js index ed564bd6..eef0ffa4 100644 --- a/tests/lib/receivers.js +++ b/tests/lib/receivers.js @@ -306,11 +306,11 @@ describe('Receivers', function () { expectedWasClean = false; }; - new InfoReceiver('test', {}, 4000); + new InfoReceiver('test', {}); }); it('will timeout - configured timeout', function (done) { - this.timeout(5000); + this.timeout(2000); InfoReceiver.prototype._getReceiver = function(baseUrl, url) { return new InfoAjax(url, XhrFake); }; @@ -325,7 +325,7 @@ describe('Receivers', function () { expectedWasClean = false; }; - new InfoReceiver('test', {}, 4000); + new InfoReceiver('test', {}, 1000); }); }); });