2015-12-31 15:51:46 +01:00
|
|
|
'use strict';
|
2016-05-29 09:06:56 +02:00
|
|
|
require('../common');
|
2016-12-31 00:38:06 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
2017-01-08 14:19:00 +01:00
|
|
|
const Agent = http.Agent;
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const server = http.createServer(function(req, res) {
|
2016-05-29 09:06:56 +02:00
|
|
|
res.end('hello world');
|
2015-12-31 15:51:46 +01:00
|
|
|
});
|
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
server.listen(0, function() {
|
2017-01-08 14:19:00 +01:00
|
|
|
const agent = new Agent({
|
2016-05-29 09:06:56 +02:00
|
|
|
keepAlive: true,
|
|
|
|
});
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const requestParams = {
|
2016-05-29 09:06:56 +02:00
|
|
|
host: 'localhost',
|
|
|
|
port: this.address().port,
|
|
|
|
agent: agent,
|
|
|
|
path: '/'
|
|
|
|
};
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const socketKey = agent.getName(requestParams);
|
2015-12-31 15:51:46 +01:00
|
|
|
|
|
|
|
get(function(res) {
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
res.resume();
|
|
|
|
res.on('end', function() {
|
|
|
|
process.nextTick(function() {
|
2017-01-08 14:19:00 +01:00
|
|
|
const freeSockets = agent.freeSockets[socketKey];
|
2015-12-31 15:51:46 +01:00
|
|
|
assert.equal(freeSockets.length, 1,
|
|
|
|
'expect a free socket on ' + socketKey);
|
|
|
|
|
|
|
|
//generate a random error on the free socket
|
2017-01-08 14:19:00 +01:00
|
|
|
const freeSocket = freeSockets[0];
|
2015-12-31 15:51:46 +01:00
|
|
|
freeSocket.emit('error', new Error('ECONNRESET: test'));
|
|
|
|
|
|
|
|
get(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
function get(callback) {
|
|
|
|
return http.get(requestParams, callback);
|
|
|
|
}
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
function done() {
|
|
|
|
assert.equal(Object.keys(agent.freeSockets).length, 0,
|
|
|
|
'expect the freeSockets pool to be empty');
|
|
|
|
|
|
|
|
agent.destroy();
|
|
|
|
server.close();
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
});
|