• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const http = require('http');
6
7function createServer(count) {
8  return http.createServer(common.mustCallAtLeast((req, res) => {
9    // Return the remote port number used for this connection.
10    res.end(req.socket.remotePort.toString(10));
11  }), count);
12}
13
14function makeRequest(url, agent, callback) {
15  http
16    .request(url, { agent }, (res) => {
17      let data = '';
18      res.setEncoding('ascii');
19      res.on('data', (c) => {
20        data += c;
21      });
22      res.on('end', () => {
23        process.nextTick(callback, data);
24      });
25    })
26    .end();
27}
28
29function bulkRequest(url, agent, done) {
30  const ports = [];
31  let count = agent.maxSockets;
32
33  for (let i = 0; i < agent.maxSockets; i++) {
34    makeRequest(url, agent, callback);
35  }
36
37  function callback(port) {
38    count -= 1;
39    ports.push(port);
40    if (count === 0) {
41      done(ports);
42    }
43  }
44}
45
46function defaultTest() {
47  const server = createServer(8);
48  server.listen(0, onListen);
49
50  function onListen() {
51    const url = `http://localhost:${server.address().port}`;
52    const agent = new http.Agent({
53      keepAlive: true,
54      maxSockets: 5
55    });
56
57    bulkRequest(url, agent, (ports) => {
58      makeRequest(url, agent, (port) => {
59        assert.strictEqual(ports[ports.length - 1], port);
60        makeRequest(url, agent, (port) => {
61          assert.strictEqual(ports[ports.length - 1], port);
62          makeRequest(url, agent, (port) => {
63            assert.strictEqual(ports[ports.length - 1], port);
64            server.close();
65            agent.destroy();
66          });
67        });
68      });
69    });
70  }
71}
72
73function fifoTest() {
74  const server = createServer(8);
75  server.listen(0, onListen);
76
77  function onListen() {
78    const url = `http://localhost:${server.address().port}`;
79    const agent = new http.Agent({
80      keepAlive: true,
81      maxSockets: 5,
82      scheduling: 'fifo'
83    });
84
85    bulkRequest(url, agent, (ports) => {
86      makeRequest(url, agent, (port) => {
87        assert.strictEqual(ports[0], port);
88        makeRequest(url, agent, (port) => {
89          assert.strictEqual(ports[1], port);
90          makeRequest(url, agent, (port) => {
91            assert.strictEqual(ports[2], port);
92            server.close();
93            agent.destroy();
94          });
95        });
96      });
97    });
98  }
99}
100
101function lifoTest() {
102  const server = createServer(8);
103  server.listen(0, onListen);
104
105  function onListen() {
106    const url = `http://localhost:${server.address().port}`;
107    const agent = new http.Agent({
108      keepAlive: true,
109      maxSockets: 5,
110      scheduling: 'lifo'
111    });
112
113    bulkRequest(url, agent, (ports) => {
114      makeRequest(url, agent, (port) => {
115        assert.strictEqual(ports[ports.length - 1], port);
116        makeRequest(url, agent, (port) => {
117          assert.strictEqual(ports[ports.length - 1], port);
118          makeRequest(url, agent, (port) => {
119            assert.strictEqual(ports[ports.length - 1], port);
120            server.close();
121            agent.destroy();
122          });
123        });
124      });
125    });
126  }
127}
128
129function badSchedulingOptionTest() {
130  try {
131    new http.Agent({
132      keepAlive: true,
133      maxSockets: 5,
134      scheduling: 'filo'
135    });
136  } catch (err) {
137    assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE');
138    assert.strictEqual(
139      err.message,
140      "The argument 'scheduling' must be one of: 'fifo', 'lifo'. " +
141        "Received 'filo'"
142    );
143  }
144}
145
146defaultTest();
147fifoTest();
148lifoTest();
149badSchedulingOptionTest();
150