From a30a9f8193ddd61dfadc2d5d79784b682027b319 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Mon, 18 Nov 2019 12:41:02 +0200 Subject: [PATCH] test: clean up http-set-trailers * remove shared state of request counting from each listener by using callbacks to report test finish. This also fixes slight race condition where one of the request could finish before the other was taken into account resulting in ECONNREFUSED due to premature server.close() * slightly move code for better cohesion * fix error comment in testHttp10 'Trailer ...' -> 'No trailer ...' PR-URL: https://github.com/nodejs/node/pull/30522 Reviewed-By: Luigi Pinca Reviewed-By: Yongsheng Zhang --- test/parallel/test-http-set-trailers.js | 72 ++++++++++--------------- 1 file changed, 29 insertions(+), 43 deletions(-) diff --git a/test/parallel/test-http-set-trailers.js b/test/parallel/test-http-set-trailers.js index 921c456bdaddfd..2197de9b0b229a 100644 --- a/test/parallel/test-http-set-trailers.js +++ b/test/parallel/test-http-set-trailers.js @@ -26,29 +26,18 @@ const http = require('http'); const net = require('net'); const util = require('util'); -let outstanding_reqs = 0; - -const server = http.createServer(function(req, res) { - res.writeHead(200, [['content-type', 'text/plain']]); - res.addTrailers({ 'x-foo': 'bar' }); - res.end('stuff\n'); -}); -server.listen(0); - - // First, we test an HTTP/1.0 request. -server.on('listening', function() { - const c = net.createConnection(this.address().port); - let res_buffer = ''; +function testHttp10(port, callback) { + const c = net.createConnection(port); c.setEncoding('utf8'); - c.on('connect', function() { - outstanding_reqs++; + c.on('connect', () => { c.write('GET / HTTP/1.0\r\n\r\n'); }); - c.on('data', function(chunk) { + let res_buffer = ''; + c.on('data', (chunk) => { res_buffer += chunk; }); @@ -56,61 +45,58 @@ server.on('listening', function() { c.end(); assert.ok( !/x-foo/.test(res_buffer), - `Trailer in HTTP/1.0 response. Response buffer: ${res_buffer}` + `No trailer in HTTP/1.0 response. Response buffer: ${res_buffer}` ); - outstanding_reqs--; - if (outstanding_reqs === 0) { - server.close(); - } + callback(); }); -}); +} // Now, we test an HTTP/1.1 request. -server.on('listening', function() { - const c = net.createConnection(this.address().port); - let res_buffer = ''; - let tid; +function testHttp11(port, callback) { + const c = net.createConnection(port); c.setEncoding('utf8'); + let tid; c.on('connect', function() { - outstanding_reqs++; c.write('GET / HTTP/1.1\r\n\r\n'); tid = setTimeout(common.mustNotCall(), 2000, 'Couldn\'t find last chunk.'); }); + let res_buffer = ''; c.on('data', function(chunk) { res_buffer += chunk; if (/0\r\n/.test(res_buffer)) { // got the end. - outstanding_reqs--; clearTimeout(tid); assert.ok( /0\r\nx-foo: bar\r\n\r\n$/.test(res_buffer), `No trailer in HTTP/1.1 response. Response buffer: ${res_buffer}` ); - if (outstanding_reqs === 0) { - server.close(); - } + callback(); } }); -}); +} // Now, see if the client sees the trailers. -server.on('listening', function() { - http.get({ - port: this.address().port, - path: '/hello', - headers: {} - }, function(res) { +function testClientTrailers(port, callback) { + http.get({ port, path: '/hello', headers: {} }, (res) => { res.on('end', function() { assert.ok('x-foo' in res.trailers, `${util.inspect(res.trailers)} misses the 'x-foo' property`); - outstanding_reqs--; - if (outstanding_reqs === 0) { - server.close(); - } + callback(); }); res.resume(); }); - outstanding_reqs++; +} + +const server = http.createServer((req, res) => { + res.writeHead(200, [['content-type', 'text/plain']]); + res.addTrailers({ 'x-foo': 'bar' }); + res.end('stuff\n'); +}); +server.listen(0, () => { + Promise.all([testHttp10, testHttp11, testClientTrailers] + .map(util.promisify) + .map((f) => f(server.address().port))) + .then(() => server.close()); });