|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const http = require('http'); |
| 6 | + |
| 7 | +function 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 | + |
| 14 | +function 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 | + |
| 29 | +function 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 | + |
| 46 | +function 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[0], port); |
| 60 | + makeRequest(url, agent, (port) => { |
| 61 | + assert.strictEqual(ports[1], port); |
| 62 | + makeRequest(url, agent, (port) => { |
| 63 | + assert.strictEqual(ports[2], port); |
| 64 | + server.close(); |
| 65 | + agent.destroy(); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function 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 | + |
| 101 | +function 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 | + |
| 129 | +function 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_OPT_VALUE'); |
| 138 | + assert.strictEqual( |
| 139 | + err.message, |
| 140 | + 'The value "filo" is invalid for option "scheduling"' |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +defaultTest(); |
| 146 | +fifoTest(); |
| 147 | +lifoTest(); |
| 148 | +badSchedulingOptionTest(); |
0 commit comments