From 5a8fcd0d94f654af89d722760c00c03f2bbb972f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 11 Apr 2018 08:06:50 +0200 Subject: [PATCH] test: fixed flaky test-http-readable-data-event Fixes: https://github.com/nodejs/node/issues/19905 PR-URL: https://github.com/nodejs/node/pull/19931 Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott Reviewed-By: Trivikram Kamat --- test/parallel/test-http-readable-data-event.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-http-readable-data-event.js b/test/parallel/test-http-readable-data-event.js index 74c9a59b268083..1c48a451ef9714 100644 --- a/test/parallel/test-http-readable-data-event.js +++ b/test/parallel/test-http-readable-data-event.js @@ -6,16 +6,21 @@ const http = require('http'); const helloWorld = 'Hello World!'; const helloAgainLater = 'Hello again later!'; +let next = null; + const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Length': '' + (helloWorld.length + helloAgainLater.length) }); - res.write(helloWorld); // we need to make sure the data is flushed - setTimeout(() => { + // before writing again + next = () => { res.end(helloAgainLater); - }, common.platformTimeout(10)); + next = () => {}; + }; + + res.write(helloWorld); }).listen(0, function() { const opts = { hostname: 'localhost', @@ -27,7 +32,7 @@ const server = http.createServer((req, res) => { const expectedRead = [helloWorld, null, helloAgainLater, null]; const req = http.request(opts, (res) => { - res.on('error', common.mustNotCall); + res.on('error', common.mustNotCall()); res.on('readable', common.mustCall(() => { let data; @@ -35,6 +40,7 @@ const server = http.createServer((req, res) => { do { data = res.read(); assert.strictEqual(data, expectedRead.shift()); + next(); } while (data !== null); }, 2));