1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5const http = require('http'); 6 7const server = http.createServer((req, res) => { 8 res.end('ok'); 9}).listen(0, common.mustCall(() => { 10 const agent = http.Agent({ 11 keepAlive: true, 12 maxSockets: 5, 13 maxFreeSockets: 2 14 }); 15 16 const keepSocketAlive = agent.keepSocketAlive; 17 const reuseSocket = agent.reuseSocket; 18 19 let called = 0; 20 let expectedSocket; 21 agent.keepSocketAlive = common.mustCall((socket) => { 22 assert(socket); 23 24 called++; 25 if (called === 1) { 26 return false; 27 } else if (called === 2) { 28 expectedSocket = socket; 29 return keepSocketAlive.call(agent, socket); 30 } 31 32 assert.strictEqual(socket, expectedSocket); 33 return false; 34 }, 3); 35 36 agent.reuseSocket = common.mustCall((socket, req) => { 37 assert.strictEqual(socket, expectedSocket); 38 assert(req); 39 40 return reuseSocket.call(agent, socket, req); 41 }, 1); 42 43 function req(callback) { 44 http.request({ 45 method: 'GET', 46 path: '/', 47 agent, 48 port: server.address().port 49 }, common.mustCall((res) => { 50 res.resume(); 51 res.once('end', common.mustCall(() => { 52 setImmediate(callback); 53 })); 54 })).end(); 55 } 56 57 // Should destroy socket instead of keeping it alive 58 req(common.mustCall(() => { 59 // Should keep socket alive 60 req(common.mustCall(() => { 61 // Should reuse the socket 62 req(common.mustCall(() => { 63 server.close(); 64 })); 65 })); 66 })); 67})); 68