2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-05-29 09:06:56 +02:00
|
|
|
require('../common');
|
2016-12-31 00:38:06 +01:00
|
|
|
const assert = require('assert');
|
2013-08-05 05:54:52 +02:00
|
|
|
|
2016-12-31 00:38:06 +01:00
|
|
|
const http = require('http');
|
2013-08-05 05:54:52 +02:00
|
|
|
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const serverSockets = [];
|
|
|
|
const server = http.createServer(function(req, res) {
|
2017-06-21 20:09:37 +02:00
|
|
|
if (!serverSockets.includes(req.socket)) {
|
2013-08-05 05:54:52 +02:00
|
|
|
serverSockets.push(req.socket);
|
|
|
|
}
|
|
|
|
res.end(req.url);
|
|
|
|
});
|
2016-05-29 09:06:56 +02:00
|
|
|
server.listen(0, function() {
|
2017-01-08 14:19:00 +01:00
|
|
|
const agent = http.Agent({
|
2016-05-29 09:06:56 +02:00
|
|
|
keepAlive: true,
|
|
|
|
maxSockets: 5,
|
|
|
|
maxFreeSockets: 2
|
|
|
|
});
|
2013-08-05 05:54:52 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let closed = false;
|
2013-08-05 05:54:52 +02:00
|
|
|
makeReqs(10, function(er) {
|
|
|
|
assert.ifError(er);
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(count(agent.freeSockets), 2);
|
|
|
|
assert.strictEqual(count(agent.sockets), 0);
|
|
|
|
assert.strictEqual(serverSockets.length, 5);
|
2013-08-05 05:54:52 +02:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Now make 10 more reqs.
|
2016-05-29 09:06:56 +02:00
|
|
|
// should use the 2 free reqs from the pool first.
|
|
|
|
makeReqs(10, function(er) {
|
|
|
|
assert.ifError(er);
|
2017-01-08 16:36:25 +01:00
|
|
|
assert.strictEqual(count(agent.freeSockets), 2);
|
|
|
|
assert.strictEqual(count(agent.sockets), 0);
|
|
|
|
assert.strictEqual(serverSockets.length, 8);
|
2016-05-29 09:06:56 +02:00
|
|
|
|
|
|
|
agent.destroy();
|
|
|
|
server.close(function() {
|
|
|
|
closed = true;
|
|
|
|
});
|
2013-08-05 05:54:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
process.on('exit', function() {
|
|
|
|
assert(closed);
|
|
|
|
console.log('ok');
|
|
|
|
});
|
|
|
|
|
|
|
|
// make 10 requests in parallel,
|
|
|
|
// then 10 more when they all finish.
|
|
|
|
function makeReqs(n, cb) {
|
2017-01-08 14:19:00 +01:00
|
|
|
for (let i = 0; i < n; i++)
|
2016-05-29 09:06:56 +02:00
|
|
|
makeReq(i, then);
|
|
|
|
|
|
|
|
function then(er) {
|
|
|
|
if (er)
|
|
|
|
return cb(er);
|
|
|
|
else if (--n === 0)
|
|
|
|
setTimeout(cb, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeReq(i, cb) {
|
|
|
|
http.request({
|
|
|
|
port: server.address().port,
|
2017-04-28 03:06:42 +02:00
|
|
|
path: `/${i}`,
|
2016-05-29 09:06:56 +02:00
|
|
|
agent: agent
|
|
|
|
}, function(res) {
|
2017-01-08 14:19:00 +01:00
|
|
|
let data = '';
|
2016-05-29 09:06:56 +02:00
|
|
|
res.setEncoding('ascii');
|
|
|
|
res.on('data', function(c) {
|
|
|
|
data += c;
|
|
|
|
});
|
|
|
|
res.on('end', function() {
|
2017-04-28 03:06:42 +02:00
|
|
|
assert.strictEqual(data, `/${i}`);
|
2016-05-29 09:06:56 +02:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}).end();
|
|
|
|
}
|
2013-08-05 05:54:52 +02:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
function count(sockets) {
|
|
|
|
return Object.keys(sockets).reduce(function(n, name) {
|
|
|
|
return n + sockets[name].length;
|
|
|
|
}, 0);
|
|
|
|
}
|
2013-08-05 05:54:52 +02:00
|
|
|
});
|