forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-http-agent-maxtotalsockets.js
More file actions
65 lines (56 loc) · 1.38 KB
/
test-http-agent-maxtotalsockets.js
File metadata and controls
65 lines (56 loc) · 1.38 KB
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
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');
const maxTotalSockets = 3;
const agent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
maxTotalSockets,
maxSockets: 100,
maxFreeSockets: 3
});
const server = http.createServer(common.mustCall((req, res) => {
res.end('hello world');
}, 6));
server.keepAliveTimeout = 0;
const countdown = new Countdown(6, () => {
agent.destroy();
server.close();
});
function handler() {
for (let i = 0; i < 6; i++) {
http.get({
host: 'localhost',
port: server.address().port,
agent: agent,
path: `/${i}`,
// Setting different origins
family: i < 3 ? 4 : 6
}, common.mustCall(res => {
assert.strictEqual(res.statusCode, 200);
res.resume();
res.on('end', common.mustCall(() => {
const count = getTotalSocketsCount();
assert(count <= maxTotalSockets);
countdown.dec();
}));
}));
}
}
function getTotalSocketsCount() {
let num = 0;
for (const key of Object.keys(agent.sockets)) {
num += agent.sockets[key].length;
}
return num;
}
function getRequestCount() {
let num = 0;
for (const key of Object.keys(agent.requests)) {
num += agent.requests[key].length;
}
return num;
}
server.listen(0, common.mustCall(handler));