From eb5d89d86430f8bacbdf559e52c08c5f6a1701c4 Mon Sep 17 00:00:00 2001 From: Nic Jansma Date: Thu, 9 Mar 2017 09:30:52 -0500 Subject: [PATCH 1/2] Adds test-timing keepAlive test --- tests/test-timing.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/test-timing.js b/tests/test-timing.js index bc68774a6..a7da6d6a2 100644 --- a/tests/test-timing.js +++ b/tests/test-timing.js @@ -88,6 +88,53 @@ tape('redirected request is timed with rollup', function(t) { }) }) +tape('keepAlive is timed', function(t) { + var agent = new require('http').Agent({ keepAlive: true }) + var options = { time: true, agent: agent } + var start1 = new Date().getTime() + request('http://localhost:' + plain_server.port + '/', options, function(err1, res1, body1) { + var end1 = new Date().getTime() + + // ensure the first request's timestamps look ok + t.equal((res1.timingStart >= start1), true) + t.equal((start1 <= end1), true) + + t.equal((res1.timings.socket >= 0), true) + t.equal((res1.timings.lookup >= res1.timings.socket), true) + t.equal((res1.timings.connect >= res1.timings.lookup), true) + t.equal((res1.timings.response >= res1.timings.connect), true) + + var start2 = new Date().getTime() + request('http://localhost:' + plain_server.port + '/', options, function(err2, res2, body2) { + var end2 = new Date().getTime() + + // ensure the second request's timestamps look ok + t.equal((res2.timingStart >= start2), true) + t.equal((start2 <= end2), true) + + // ensure socket==lookup==connect for the second request + t.equal((res2.timings.socket >= 0), true) + t.equal((res2.timings.lookup == res2.timings.socket), true) + t.equal((res2.timings.connect == res2.timings.lookup), true) + t.equal((res2.timings.response >= res2.timings.connect), true) + + // explicitly shut down the agent + if (typeof agent.destroy === 'function') { + agent.destroy() + } else { + // node < 0.12 + Object.keys(agent.sockets).forEach(function (name) { + agent.sockets[name].forEach(function (socket) { + socket.end() + }) + }) + } + + t.end() + }) + }) +}) + tape('cleanup', function(t) { plain_server.close(function() { t.end() From dd5c02c2d6861e6d44b27ea013cdb09d5f820717 Mon Sep 17 00:00:00 2001 From: Nic Jansma Date: Thu, 16 Mar 2017 19:22:33 -0400 Subject: [PATCH 2/2] Updated comment --- tests/test-timing.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test-timing.js b/tests/test-timing.js index a7da6d6a2..4e87afcee 100644 --- a/tests/test-timing.js +++ b/tests/test-timing.js @@ -104,6 +104,7 @@ tape('keepAlive is timed', function(t) { t.equal((res1.timings.connect >= res1.timings.lookup), true) t.equal((res1.timings.response >= res1.timings.connect), true) + // open a second request with the same agent so we re-use the same connection var start2 = new Date().getTime() request('http://localhost:' + plain_server.port + '/', options, function(err2, res2, body2) { var end2 = new Date().getTime()