Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cleanup test-http-agent-destroyed-socket #8583

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions test/parallel/test-http-agent-destroyed-socket.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
'use strict';
require('../common');
var assert = require('assert');
var http = require('http');
const assert = require('assert');
const http = require('http');

var server = http.createServer(function(req, res) {
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(0, function() {
var agent = new http.Agent({maxSockets: 1});
const agent = new http.Agent({maxSockets: 1});

agent.on('free', function(socket, host, port) {
agent.on('free', (socket, host, port) => {
console.log('freeing socket. destroyed? ', socket.destroyed);
});

var requestOptions = {
const requestOptions = {
agent: agent,
host: 'localhost',
port: this.address().port,
path: '/'
};

var request1 = http.get(requestOptions, function(response) {
const request1 = http.get(requestOptions, (response) => {
// assert request2 is queued in the agent
var key = agent.getName(requestOptions);
assert(agent.requests[key].length === 1);
const key = agent.getName(requestOptions);
assert.strictEqual(agent.requests[key].length, 1);
console.log('got response1');
request1.socket.on('close', function() {
request1.socket.on('close', () => {
console.log('request1 socket closed');
});
response.pipe(process.stdout);
response.on('end', function() {
response.on('end', () => {
console.log('response1 done');
/////////////////////////////////
//
Expand All @@ -44,35 +44,35 @@ var server = http.createServer(function(req, res) {
// is triggered.
request1.socket.destroy();

response.once('close', function() {
response.once('close', () => {
// assert request2 was removed from the queue
assert(!agent.requests[key]);
console.log("waiting for request2.onSocket's nextTick");
process.nextTick(function() {
process.nextTick(() => {
// assert that the same socket was not assigned to request2,
// since it was destroyed.
assert(request1.socket !== request2.socket);
assert.notStrictEqual(request1.socket, request2.socket);
assert(!request2.socket.destroyed, 'the socket is destroyed');
});
});
});
});

var request2 = http.get(requestOptions, function(response) {
const request2 = http.get(requestOptions, (response) => {
assert(!request2.socket.destroyed);
assert(request1.socket.destroyed);
// assert not reusing the same socket, since it was destroyed.
assert(request1.socket !== request2.socket);
assert.notStrictEqual(request1.socket, request2.socket);
console.log('got response2');
var gotClose = false;
var gotResponseEnd = false;
request2.socket.on('close', function() {
let gotClose = false;
let gotResponseEnd = false;
request2.socket.on('close', () => {
console.log('request2 socket closed');
gotClose = true;
done();
});
response.pipe(process.stdout);
response.on('end', function() {
response.on('end', () => {
console.log('response2 done');
gotResponseEnd = true;
done();
Expand Down