Skip to content

Commit

Permalink
Merge pull request #61 from badunk/bugfix/reconnect-multiply-tests
Browse files Browse the repository at this point in the history
Double Reconnect Test
  • Loading branch information
rexxars authored Apr 17, 2017
2 parents b59da74 + 9ed0e75 commit 4b8338b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,17 @@ function EventSource(url, eventSourceInitDict) {
}

readyState = EventSource.OPEN;
res.on('close', onConnectionClosed);
res.on('end', onConnectionClosed);
res.on('close', function() {
res.removeAllListeners('close');
res.removeAllListeners('end');
onConnectionClosed();
});

res.on('end', function() {
res.removeAllListeners('close');
res.removeAllListeners('end');
onConnectionClosed();
});
_emit('open', new Event('open'));

// text/event-stream parser adapted from webkit's
Expand Down
22 changes: 22 additions & 0 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,28 @@ describe('Events', function () {
});
});

it('does not double reconnect when connection is closed by server', function (done) {
createServer(function (err, server) {
if(err) return done(err);

var numConnections = 0;
server.on('request', function (req, res) {
numConnections++;
writeEvents([])(req, res)

if (numConnections > 2) done(new Error('reopening too many connections'))
// destroy only the first connection - expected only 1 other reconnect
if (numConnections == 1){
process.nextTick(function (){
req.destroy()
})
}
});
var es = new EventSource(server.url);
});
setTimeout(done, 1500);
})

it('does not emit error when connection is closed by client', function (done) {
createServer(function (err, server) {
if(err) return done(err);
Expand Down

0 comments on commit 4b8338b

Please sign in to comment.