11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst agent = new http.Agent({
81cb0ef41Sopenharmony_ci  keepAlive: true,
91cb0ef41Sopenharmony_ci  keepAliveMsecs: 1000,
101cb0ef41Sopenharmony_ci  maxSockets: 2,
111cb0ef41Sopenharmony_ci  maxFreeSockets: 2
121cb0ef41Sopenharmony_ci});
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst server = http.createServer(common.mustCall((req, res) => {
151cb0ef41Sopenharmony_ci  res.end('hello world');
161cb0ef41Sopenharmony_ci}, 2));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciserver.keepAliveTimeout = 0;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction get(path, callback) {
211cb0ef41Sopenharmony_ci  return http.get({
221cb0ef41Sopenharmony_ci    host: 'localhost',
231cb0ef41Sopenharmony_ci    port: server.address().port,
241cb0ef41Sopenharmony_ci    agent: agent,
251cb0ef41Sopenharmony_ci    path: path
261cb0ef41Sopenharmony_ci  }, callback);
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst countdown = new Countdown(2, () => {
301cb0ef41Sopenharmony_ci  const freepool = agent.freeSockets[Object.keys(agent.freeSockets)[0]];
311cb0ef41Sopenharmony_ci  assert.strictEqual(freepool.length, 2,
321cb0ef41Sopenharmony_ci                     `expect keep 2 free sockets, but got ${freepool.length}`);
331cb0ef41Sopenharmony_ci  agent.destroy();
341cb0ef41Sopenharmony_ci  server.close();
351cb0ef41Sopenharmony_ci});
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cifunction dec() {
381cb0ef41Sopenharmony_ci  process.nextTick(() => countdown.dec());
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cifunction onGet(res) {
421cb0ef41Sopenharmony_ci  assert.strictEqual(res.statusCode, 200);
431cb0ef41Sopenharmony_ci  res.resume();
441cb0ef41Sopenharmony_ci  res.on('end', common.mustCall(dec));
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
481cb0ef41Sopenharmony_ci  get('/1', common.mustCall(onGet));
491cb0ef41Sopenharmony_ci  get('/2', common.mustCall(onGet));
501cb0ef41Sopenharmony_ci}));
51