2015-12-31 15:51:46 +01:00
|
|
|
'use strict';
|
2017-07-15 00:05:24 +02:00
|
|
|
|
|
|
|
const common = 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-07-15 00:05:24 +02:00
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
2016-05-29 09:06:56 +02:00
|
|
|
res.end('hello world');
|
2017-07-15 00:05:24 +02:00
|
|
|
}, 2));
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2017-07-15 00:05:24 +02:00
|
|
|
server.listen(0, () => {
|
|
|
|
const agent = new Agent({ 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',
|
2017-07-15 00:05:24 +02:00
|
|
|
port: server.address().port,
|
2016-05-29 09:06:56 +02:00
|
|
|
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
|
|
|
|
2017-07-15 00:05:24 +02:00
|
|
|
http.get(requestParams, common.mustCall((res) => {
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2015-12-31 15:51:46 +01:00
|
|
|
res.resume();
|
2017-07-15 00:05:24 +02:00
|
|
|
res.on('end', common.mustCall(() => {
|
|
|
|
process.nextTick(common.mustCall(() => {
|
2017-01-08 14:19:00 +01:00
|
|
|
const freeSockets = agent.freeSockets[socketKey];
|
2018-03-25 16:27:38 +02:00
|
|
|
// Expect a free socket on socketKey
|
2018-03-22 02:22:44 +01:00
|
|
|
assert.strictEqual(freeSockets.length, 1);
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2018-03-25 16:27:38 +02:00
|
|
|
// 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'));
|
|
|
|
|
2017-07-15 00:05:24 +02:00
|
|
|
http.get(requestParams, done);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
2015-12-31 15:51:46 +01:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
function done() {
|
2018-03-25 16:27:38 +02:00
|
|
|
// Expect the freeSockets pool to be empty
|
2018-03-22 02:22:44 +01:00
|
|
|
assert.strictEqual(Object.keys(agent.freeSockets).length, 0);
|
2016-05-29 09:06:56 +02:00
|
|
|
|
|
|
|
agent.destroy();
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
});
|