summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-agent-scheduling.js
blob: b5ed2df4363a5500c2d768c92c23df4da7be63db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

function createServer(count) {
  return http.createServer(common.mustCallAtLeast((req, res) => {
    // Return the remote port number used for this connection.
    res.end(req.socket.remotePort.toString(10));
  }), count);
}

function makeRequest(url, agent, callback) {
  http
    .request(url, { agent }, (res) => {
      let data = '';
      res.setEncoding('ascii');
      res.on('data', (c) => {
        data += c;
      });
      res.on('end', () => {
        process.nextTick(callback, data);
      });
    })
    .end();
}

function bulkRequest(url, agent, done) {
  const ports = [];
  let count = agent.maxSockets;

  for (let i = 0; i < agent.maxSockets; i++) {
    makeRequest(url, agent, callback);
  }

  function callback(port) {
    count -= 1;
    ports.push(port);
    if (count === 0) {
      done(ports);
    }
  }
}

function defaultTest() {
  const server = createServer(8);
  server.listen(0, onListen);

  function onListen() {
    const url = `http://localhost:${server.address().port}`;
    const agent = new http.Agent({
      keepAlive: true,
      maxSockets: 5
    });

    bulkRequest(url, agent, (ports) => {
      makeRequest(url, agent, (port) => {
        assert.strictEqual(ports[0], port);
        makeRequest(url, agent, (port) => {
          assert.strictEqual(ports[1], port);
          makeRequest(url, agent, (port) => {
            assert.strictEqual(ports[2], port);
            server.close();
            agent.destroy();
          });
        });
      });
    });
  }
}

function fifoTest() {
  const server = createServer(8);
  server.listen(0, onListen);

  function onListen() {
    const url = `http://localhost:${server.address().port}`;
    const agent = new http.Agent({
      keepAlive: true,
      maxSockets: 5,
      scheduling: 'fifo'
    });

    bulkRequest(url, agent, (ports) => {
      makeRequest(url, agent, (port) => {
        assert.strictEqual(ports[0], port);
        makeRequest(url, agent, (port) => {
          assert.strictEqual(ports[1], port);
          makeRequest(url, agent, (port) => {
            assert.strictEqual(ports[2], port);
            server.close();
            agent.destroy();
          });
        });
      });
    });
  }
}

function lifoTest() {
  const server = createServer(8);
  server.listen(0, onListen);

  function onListen() {
    const url = `http://localhost:${server.address().port}`;
    const agent = new http.Agent({
      keepAlive: true,
      maxSockets: 5,
      scheduling: 'lifo'
    });

    bulkRequest(url, agent, (ports) => {
      makeRequest(url, agent, (port) => {
        assert.strictEqual(ports[ports.length - 1], port);
        makeRequest(url, agent, (port) => {
          assert.strictEqual(ports[ports.length - 1], port);
          makeRequest(url, agent, (port) => {
            assert.strictEqual(ports[ports.length - 1], port);
            server.close();
            agent.destroy();
          });
        });
      });
    });
  }
}

function badSchedulingOptionTest() {
  try {
    new http.Agent({
      keepAlive: true,
      maxSockets: 5,
      scheduling: 'filo'
    });
  } catch (err) {
    assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE');
    assert.strictEqual(
      err.message,
      "The argument 'scheduling' must be one of: 'fifo', 'lifo'. " +
        "Received 'filo'"
    );
  }
}

defaultTest();
fifoTest();
lifoTest();
badSchedulingOptionTest();