From c8bb871f9542e722518031538676490572f9fdef 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 025941cf..a81381d0 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 511bb0de..7aff4312 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -710,6 +710,34 @@ describe('Reconnection', function () { } }) + it('continuing attemptes 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)