11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This test ensures that the `maxSockets` value for `http.Agent` is respected.
61cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/4050
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst http = require('http');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst MAX_SOCKETS = 2;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst agent = new http.Agent({
141cb0ef41Sopenharmony_ci  keepAlive: true,
151cb0ef41Sopenharmony_ci  keepAliveMsecs: 1000,
161cb0ef41Sopenharmony_ci  maxSockets: MAX_SOCKETS,
171cb0ef41Sopenharmony_ci  maxFreeSockets: 2
181cb0ef41Sopenharmony_ci});
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst server = http.createServer(
211cb0ef41Sopenharmony_ci  common.mustCall((req, res) => {
221cb0ef41Sopenharmony_ci    res.end('hello world');
231cb0ef41Sopenharmony_ci  }, 6)
241cb0ef41Sopenharmony_ci);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst countdown = new Countdown(6, () => server.close());
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cifunction get(path, callback) {
291cb0ef41Sopenharmony_ci  return http.get(
301cb0ef41Sopenharmony_ci    {
311cb0ef41Sopenharmony_ci      host: 'localhost',
321cb0ef41Sopenharmony_ci      port: server.address().port,
331cb0ef41Sopenharmony_ci      agent: agent,
341cb0ef41Sopenharmony_ci      path: path
351cb0ef41Sopenharmony_ci    },
361cb0ef41Sopenharmony_ci    callback
371cb0ef41Sopenharmony_ci  );
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciserver.listen(
411cb0ef41Sopenharmony_ci  0,
421cb0ef41Sopenharmony_ci  common.mustCall(() => {
431cb0ef41Sopenharmony_ci    for (let i = 0; i < 6; i++) {
441cb0ef41Sopenharmony_ci      const request = get('/1', common.mustCall());
451cb0ef41Sopenharmony_ci      request.on(
461cb0ef41Sopenharmony_ci        'response',
471cb0ef41Sopenharmony_ci        common.mustCall(() => {
481cb0ef41Sopenharmony_ci          request.abort();
491cb0ef41Sopenharmony_ci          const sockets = agent.sockets[Object.keys(agent.sockets)[0]];
501cb0ef41Sopenharmony_ci          assert(sockets.length <= MAX_SOCKETS);
511cb0ef41Sopenharmony_ci          countdown.dec();
521cb0ef41Sopenharmony_ci        })
531cb0ef41Sopenharmony_ci      );
541cb0ef41Sopenharmony_ci    }
551cb0ef41Sopenharmony_ci  })
561cb0ef41Sopenharmony_ci);
57