Skip to content

Commit

Permalink
Merge pull request hapijs#903 from wpreul/master
Browse files Browse the repository at this point in the history
Fixing issue where timeout occurs after socket close in client
  • Loading branch information
Eran Hammer committed May 30, 2013
2 parents 606b6ce + c15deee commit 3ae657e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
2 changes: 0 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ exports.request = function (method, url, options, callback, _trace) {
if (!isFinished) {
isFinished = true;

req.removeAllListeners();

return callback(err, res);
}
};
Expand Down
58 changes: 58 additions & 0 deletions test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,63 @@ describe('Client', function () {
done();
});
});

it('handles a timeout during a socket close', function (done) {

var server = Http.createServer(function (req, res) {

req.once('error', function () { });
res.once('error', function () { });

setTimeout(function () {

req.destroy();
}, 5);
});

server.once('error', function () { });

server.once('listening', function () {

Client.request('get', 'http://127.0.0.1:' + server.address().port, { payload: '', timeout: 5 }, function (err) {

expect(err).to.exist;
server.close();

setTimeout(done, 5);
});
});

server.listen(0);
});

it('handles an error after a timeout', function (done) {

var server = Http.createServer(function (req, res) {

req.once('error', function () { });
res.once('error', function () { });

setTimeout(function () {

res.socket.write('ERROR');
}, 5);
});

server.once('error', function () { });

server.once('listening', function () {

Client.request('get', 'http://127.0.0.1:' + server.address().port, { payload: '', timeout: 5 }, function (err) {

expect(err).to.exist;
server.close();

setTimeout(done, 5);
});
});

server.listen(0);
});
});
});

0 comments on commit 3ae657e

Please sign in to comment.