1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const http = require('http'); 6const Agent = http.Agent; 7 8const server = http.createServer(common.mustCall((req, res) => { 9 res.end('hello world'); 10}, 2)); 11 12server.listen(0, () => { 13 const agent = new Agent({ keepAlive: true }); 14 15 const requestParams = { 16 host: 'localhost', 17 port: server.address().port, 18 agent: agent, 19 path: '/' 20 }; 21 22 const socketKey = agent.getName(requestParams); 23 24 http.get(requestParams, common.mustCall((res) => { 25 assert.strictEqual(res.statusCode, 200); 26 res.resume(); 27 res.on('end', common.mustCall(() => { 28 process.nextTick(common.mustCall(() => { 29 const freeSockets = agent.freeSockets[socketKey]; 30 // Expect a free socket on socketKey 31 assert.strictEqual(freeSockets.length, 1); 32 33 // Generate a random error on the free socket 34 const freeSocket = freeSockets[0]; 35 freeSocket.emit('error', new Error('ECONNRESET: test')); 36 37 http.get(requestParams, done); 38 })); 39 })); 40 })); 41 42 function done() { 43 // Expect the freeSockets pool to be empty 44 assert.strictEqual(Object.keys(agent.freeSockets).length, 0); 45 46 agent.destroy(); 47 server.close(); 48 } 49}); 50