Skip to content

Commit

Permalink
test: improve test-http-agent-destroyed-socket.js
Browse files Browse the repository at this point in the history
* wrap callbacks in mustCall()
* Wrap the callbacks which make assertions in common.mustcall()
  to ensure they are called

PR-URL: #11201
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
shubheksha authored and italoacasas committed Feb 14, 2017
1 parent 3074c6d commit b405699
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions test/parallel/test-http-agent-destroyed-socket.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const http = require('http');

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

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

Expand All @@ -20,7 +20,7 @@ const server = http.createServer(function(req, res) {
path: '/'
};

const request1 = http.get(requestOptions, function(response) {
const request1 = http.get(requestOptions, common.mustCall(function(response) {
// assert request2 is queued in the agent
const key = agent.getName(requestOptions);
assert.strictEqual(agent.requests[key].length, 1);
Expand All @@ -29,7 +29,7 @@ const server = http.createServer(function(req, res) {
console.log('request1 socket closed');
});
response.pipe(process.stdout);
response.on('end', function() {
response.on('end', common.mustCall(function() {
console.log('response1 done');
/////////////////////////////////
//
Expand All @@ -48,17 +48,17 @@ const server = http.createServer(function(req, res) {
// assert request2 was removed from the queue
assert(!agent.requests[key]);
console.log("waiting for request2.onSocket's nextTick");
process.nextTick(function() {
process.nextTick(common.mustCall(function() {
// assert that the same socket was not assigned to request2,
// since it was destroyed.
assert.notStrictEqual(request1.socket, request2.socket);
assert(!request2.socket.destroyed, 'the socket is destroyed');
});
}));
});
});
});
}));
}));

const request2 = http.get(requestOptions, function(response) {
const request2 = http.get(requestOptions, common.mustCall(function(response) {
assert(!request2.socket.destroyed);
assert(request1.socket.destroyed);
// assert not reusing the same socket, since it was destroyed.
Expand All @@ -82,5 +82,5 @@ const server = http.createServer(function(req, res) {
if (gotResponseEnd && gotClose)
server.close();
}
});
});
}));
}));

0 comments on commit b405699

Please sign in to comment.