11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst http = require('http');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifunction createServer(count) {
81cb0ef41Sopenharmony_ci  return http.createServer(common.mustCallAtLeast((req, res) => {
91cb0ef41Sopenharmony_ci    // Return the remote port number used for this connection.
101cb0ef41Sopenharmony_ci    res.end(req.socket.remotePort.toString(10));
111cb0ef41Sopenharmony_ci  }), count);
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction makeRequest(url, agent, callback) {
151cb0ef41Sopenharmony_ci  http
161cb0ef41Sopenharmony_ci    .request(url, { agent }, (res) => {
171cb0ef41Sopenharmony_ci      let data = '';
181cb0ef41Sopenharmony_ci      res.setEncoding('ascii');
191cb0ef41Sopenharmony_ci      res.on('data', (c) => {
201cb0ef41Sopenharmony_ci        data += c;
211cb0ef41Sopenharmony_ci      });
221cb0ef41Sopenharmony_ci      res.on('end', () => {
231cb0ef41Sopenharmony_ci        process.nextTick(callback, data);
241cb0ef41Sopenharmony_ci      });
251cb0ef41Sopenharmony_ci    })
261cb0ef41Sopenharmony_ci    .end();
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cifunction bulkRequest(url, agent, done) {
301cb0ef41Sopenharmony_ci  const ports = [];
311cb0ef41Sopenharmony_ci  let count = agent.maxSockets;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  for (let i = 0; i < agent.maxSockets; i++) {
341cb0ef41Sopenharmony_ci    makeRequest(url, agent, callback);
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  function callback(port) {
381cb0ef41Sopenharmony_ci    count -= 1;
391cb0ef41Sopenharmony_ci    ports.push(port);
401cb0ef41Sopenharmony_ci    if (count === 0) {
411cb0ef41Sopenharmony_ci      done(ports);
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cifunction defaultTest() {
471cb0ef41Sopenharmony_ci  const server = createServer(8);
481cb0ef41Sopenharmony_ci  server.listen(0, onListen);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  function onListen() {
511cb0ef41Sopenharmony_ci    const url = `http://localhost:${server.address().port}`;
521cb0ef41Sopenharmony_ci    const agent = new http.Agent({
531cb0ef41Sopenharmony_ci      keepAlive: true,
541cb0ef41Sopenharmony_ci      maxSockets: 5
551cb0ef41Sopenharmony_ci    });
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    bulkRequest(url, agent, (ports) => {
581cb0ef41Sopenharmony_ci      makeRequest(url, agent, (port) => {
591cb0ef41Sopenharmony_ci        assert.strictEqual(ports[ports.length - 1], port);
601cb0ef41Sopenharmony_ci        makeRequest(url, agent, (port) => {
611cb0ef41Sopenharmony_ci          assert.strictEqual(ports[ports.length - 1], port);
621cb0ef41Sopenharmony_ci          makeRequest(url, agent, (port) => {
631cb0ef41Sopenharmony_ci            assert.strictEqual(ports[ports.length - 1], port);
641cb0ef41Sopenharmony_ci            server.close();
651cb0ef41Sopenharmony_ci            agent.destroy();
661cb0ef41Sopenharmony_ci          });
671cb0ef41Sopenharmony_ci        });
681cb0ef41Sopenharmony_ci      });
691cb0ef41Sopenharmony_ci    });
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_cifunction fifoTest() {
741cb0ef41Sopenharmony_ci  const server = createServer(8);
751cb0ef41Sopenharmony_ci  server.listen(0, onListen);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  function onListen() {
781cb0ef41Sopenharmony_ci    const url = `http://localhost:${server.address().port}`;
791cb0ef41Sopenharmony_ci    const agent = new http.Agent({
801cb0ef41Sopenharmony_ci      keepAlive: true,
811cb0ef41Sopenharmony_ci      maxSockets: 5,
821cb0ef41Sopenharmony_ci      scheduling: 'fifo'
831cb0ef41Sopenharmony_ci    });
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    bulkRequest(url, agent, (ports) => {
861cb0ef41Sopenharmony_ci      makeRequest(url, agent, (port) => {
871cb0ef41Sopenharmony_ci        assert.strictEqual(ports[0], port);
881cb0ef41Sopenharmony_ci        makeRequest(url, agent, (port) => {
891cb0ef41Sopenharmony_ci          assert.strictEqual(ports[1], port);
901cb0ef41Sopenharmony_ci          makeRequest(url, agent, (port) => {
911cb0ef41Sopenharmony_ci            assert.strictEqual(ports[2], port);
921cb0ef41Sopenharmony_ci            server.close();
931cb0ef41Sopenharmony_ci            agent.destroy();
941cb0ef41Sopenharmony_ci          });
951cb0ef41Sopenharmony_ci        });
961cb0ef41Sopenharmony_ci      });
971cb0ef41Sopenharmony_ci    });
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_cifunction lifoTest() {
1021cb0ef41Sopenharmony_ci  const server = createServer(8);
1031cb0ef41Sopenharmony_ci  server.listen(0, onListen);
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  function onListen() {
1061cb0ef41Sopenharmony_ci    const url = `http://localhost:${server.address().port}`;
1071cb0ef41Sopenharmony_ci    const agent = new http.Agent({
1081cb0ef41Sopenharmony_ci      keepAlive: true,
1091cb0ef41Sopenharmony_ci      maxSockets: 5,
1101cb0ef41Sopenharmony_ci      scheduling: 'lifo'
1111cb0ef41Sopenharmony_ci    });
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci    bulkRequest(url, agent, (ports) => {
1141cb0ef41Sopenharmony_ci      makeRequest(url, agent, (port) => {
1151cb0ef41Sopenharmony_ci        assert.strictEqual(ports[ports.length - 1], port);
1161cb0ef41Sopenharmony_ci        makeRequest(url, agent, (port) => {
1171cb0ef41Sopenharmony_ci          assert.strictEqual(ports[ports.length - 1], port);
1181cb0ef41Sopenharmony_ci          makeRequest(url, agent, (port) => {
1191cb0ef41Sopenharmony_ci            assert.strictEqual(ports[ports.length - 1], port);
1201cb0ef41Sopenharmony_ci            server.close();
1211cb0ef41Sopenharmony_ci            agent.destroy();
1221cb0ef41Sopenharmony_ci          });
1231cb0ef41Sopenharmony_ci        });
1241cb0ef41Sopenharmony_ci      });
1251cb0ef41Sopenharmony_ci    });
1261cb0ef41Sopenharmony_ci  }
1271cb0ef41Sopenharmony_ci}
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_cifunction badSchedulingOptionTest() {
1301cb0ef41Sopenharmony_ci  try {
1311cb0ef41Sopenharmony_ci    new http.Agent({
1321cb0ef41Sopenharmony_ci      keepAlive: true,
1331cb0ef41Sopenharmony_ci      maxSockets: 5,
1341cb0ef41Sopenharmony_ci      scheduling: 'filo'
1351cb0ef41Sopenharmony_ci    });
1361cb0ef41Sopenharmony_ci  } catch (err) {
1371cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE');
1381cb0ef41Sopenharmony_ci    assert.strictEqual(
1391cb0ef41Sopenharmony_ci      err.message,
1401cb0ef41Sopenharmony_ci      "The argument 'scheduling' must be one of: 'fifo', 'lifo'. " +
1411cb0ef41Sopenharmony_ci        "Received 'filo'"
1421cb0ef41Sopenharmony_ci    );
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci}
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_cidefaultTest();
1471cb0ef41Sopenharmony_cififoTest();
1481cb0ef41Sopenharmony_cilifoTest();
1491cb0ef41Sopenharmony_cibadSchedulingOptionTest();
150