node/test/parallel/test-http-keep-alive.js
Brian White 2bc7841d0f
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

52 lines
1.4 KiB
JavaScript

'use strict';
require('../common');
var assert = require('assert');
var http = require('http');
var body = 'hello world\n';
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Length': body.length});
res.write(body);
res.end();
});
var agent = new http.Agent({maxSockets: 1});
var headers = {'connection': 'keep-alive'};
var name;
server.listen(0, function() {
name = agent.getName({ port: this.address().port });
http.get({
path: '/', headers: headers, port: this.address().port, agent: agent
}, function(response) {
assert.equal(agent.sockets[name].length, 1);
assert.equal(agent.requests[name].length, 2);
response.resume();
});
http.get({
path: '/', headers: headers, port: this.address().port, agent: agent
}, function(response) {
assert.equal(agent.sockets[name].length, 1);
assert.equal(agent.requests[name].length, 1);
response.resume();
});
http.get({
path: '/', headers: headers, port: this.address().port, agent: agent
}, function(response) {
response.on('end', function() {
assert.equal(agent.sockets[name].length, 1);
assert(!agent.requests.hasOwnProperty(name));
server.close();
});
response.resume();
});
});
process.on('exit', function() {
assert(!agent.sockets.hasOwnProperty(name));
assert(!agent.requests.hasOwnProperty(name));
});