11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst http = require('http'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst server = http.createServer((req, res) => { 81cb0ef41Sopenharmony_ci res.end('ok'); 91cb0ef41Sopenharmony_ci}).listen(0, common.mustCall(() => { 101cb0ef41Sopenharmony_ci const agent = http.Agent({ 111cb0ef41Sopenharmony_ci keepAlive: true, 121cb0ef41Sopenharmony_ci maxSockets: 5, 131cb0ef41Sopenharmony_ci maxFreeSockets: 2 141cb0ef41Sopenharmony_ci }); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci const keepSocketAlive = agent.keepSocketAlive; 171cb0ef41Sopenharmony_ci const reuseSocket = agent.reuseSocket; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci let called = 0; 201cb0ef41Sopenharmony_ci let expectedSocket; 211cb0ef41Sopenharmony_ci agent.keepSocketAlive = common.mustCall((socket) => { 221cb0ef41Sopenharmony_ci assert(socket); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci called++; 251cb0ef41Sopenharmony_ci if (called === 1) { 261cb0ef41Sopenharmony_ci return false; 271cb0ef41Sopenharmony_ci } else if (called === 2) { 281cb0ef41Sopenharmony_ci expectedSocket = socket; 291cb0ef41Sopenharmony_ci return keepSocketAlive.call(agent, socket); 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci assert.strictEqual(socket, expectedSocket); 331cb0ef41Sopenharmony_ci return false; 341cb0ef41Sopenharmony_ci }, 3); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci agent.reuseSocket = common.mustCall((socket, req) => { 371cb0ef41Sopenharmony_ci assert.strictEqual(socket, expectedSocket); 381cb0ef41Sopenharmony_ci assert(req); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci return reuseSocket.call(agent, socket, req); 411cb0ef41Sopenharmony_ci }, 1); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci function req(callback) { 441cb0ef41Sopenharmony_ci http.request({ 451cb0ef41Sopenharmony_ci method: 'GET', 461cb0ef41Sopenharmony_ci path: '/', 471cb0ef41Sopenharmony_ci agent, 481cb0ef41Sopenharmony_ci port: server.address().port 491cb0ef41Sopenharmony_ci }, common.mustCall((res) => { 501cb0ef41Sopenharmony_ci res.resume(); 511cb0ef41Sopenharmony_ci res.once('end', common.mustCall(() => { 521cb0ef41Sopenharmony_ci setImmediate(callback); 531cb0ef41Sopenharmony_ci })); 541cb0ef41Sopenharmony_ci })).end(); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci // Should destroy socket instead of keeping it alive 581cb0ef41Sopenharmony_ci req(common.mustCall(() => { 591cb0ef41Sopenharmony_ci // Should keep socket alive 601cb0ef41Sopenharmony_ci req(common.mustCall(() => { 611cb0ef41Sopenharmony_ci // Should reuse the socket 621cb0ef41Sopenharmony_ci req(common.mustCall(() => { 631cb0ef41Sopenharmony_ci server.close(); 641cb0ef41Sopenharmony_ci })); 651cb0ef41Sopenharmony_ci })); 661cb0ef41Sopenharmony_ci })); 671cb0ef41Sopenharmony_ci})); 68