From 9a4190f65e761ee672d786a6d01c60392950064b Mon Sep 17 00:00:00 2001 From: icy_fish Date: Thu, 21 May 2020 23:17:31 +0800 Subject: [PATCH] Fix issue: reconnection only happends for 1 time after connection drops Since the PR #125 fixed duplicate connections after reconnection by using a `connectionInProgress` lock to avoid function `connect()` be called duplicately. But it forgot to release the `connectionInProgress` lock when request error happends, in that case, our client can only retry for 1 time and never get the lock again. So it's needed to release the `connectionInProgress` lock when error happends. Signed-off-by: icy_fish --- lib/eventsource.js | 1 + test/eventsource_test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/eventsource.js b/lib/eventsource.js index 025941c..a81381d 100644 --- a/lib/eventsource.js +++ b/lib/eventsource.js @@ -242,6 +242,7 @@ function EventSource (url, eventSourceInitDict) { }) req.on('error', function (err) { + self.connectionInProgress = false onConnectionClosed(err.message) }) diff --git a/test/eventsource_test.js b/test/eventsource_test.js index 511bb0d..b0be4ec 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -710,6 +710,34 @@ describe('Reconnection', function () { } }) + it('continuing attempts when server is down', function (done) { + // Seems set reconnectInterval=0 not work here, this makes total time spent for current case more than 3S + this.timeout(4000) + + var es = new EventSource('http://localhost:' + _port++) + es.reconnectInterval = 0 + var reconnectCount = 0 + + es.onerror = function () { + reconnectCount++ + // make sure client is keeping reconnecting + if (reconnectCount > 2) { + es.onerror = null + var port = u.parse(es.url).port + configureServer(http.createServer(), 'http', port, function (err, server) { + if (err) return done(err) + + server.on('request', writeEvents(['data: hello\n\n'])) + + es.onmessage = function (m) { + assert.equal('hello', m.data) + server.close(done) + } + }) + } + } + }) + it('is attempted when server goes down after connection', function (done) { createServer(function (err, server) { if (err) return done(err)