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
|
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const server = http.createServer((req, res) => {
res.end('ok');
}).listen(0, common.mustCall(() => {
const agent = http.Agent({
keepAlive: true,
maxSockets: 5,
maxFreeSockets: 2
});
const keepSocketAlive = agent.keepSocketAlive;
const reuseSocket = agent.reuseSocket;
let called = 0;
let expectedSocket;
agent.keepSocketAlive = common.mustCall((socket) => {
assert(socket);
called++;
if (called === 1) {
return false;
} else if (called === 2) {
expectedSocket = socket;
return keepSocketAlive.call(agent, socket);
}
assert.strictEqual(socket, expectedSocket);
return false;
}, 3);
agent.reuseSocket = common.mustCall((socket, req) => {
assert.strictEqual(socket, expectedSocket);
assert(req);
return reuseSocket.call(agent, socket, req);
}, 1);
function req(callback) {
http.request({
method: 'GET',
path: '/',
agent,
port: server.address().port
}, common.mustCall((res) => {
res.resume();
res.once('end', common.mustCall(() => {
setImmediate(callback);
}));
})).end();
}
// Should destroy socket instead of keeping it alive
req(common.mustCall(() => {
// Should keep socket alive
req(common.mustCall(() => {
// Should reuse the socket
req(common.mustCall(() => {
server.close();
}));
}));
}));
}));
|