From 3e77536c6b7a67f6949857d2606b8b137aa2ab43 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sat, 14 Nov 2020 18:46:19 +0100 Subject: [PATCH] test: deflake test-http-destroyed-socket-write2 Ensure that the write occurs in the same tick where the socket is destroyed by the other peer. PR-URL: https://github.com/nodejs/node/pull/36120 Fixes: https://github.com/nodejs/node/issues/36081 Fixes: https://github.com/nodejs/node/issues/4066 Reviewed-By: Rich Trott Reviewed-By: Ricky Zhou <0x19951125@gmail.com> --- .../test-http-destroyed-socket-write2.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-http-destroyed-socket-write2.js b/test/parallel/test-http-destroyed-socket-write2.js index 49594bde6daf1d..8511c43dddf155 100644 --- a/test/parallel/test-http-destroyed-socket-write2.js +++ b/test/parallel/test-http-destroyed-socket-write2.js @@ -21,16 +21,20 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); // Verify that ECONNRESET is raised when writing to a http request // where the server has ended the socket. +const assert = require('assert'); const http = require('http'); + +const kResponseDestroyed = Symbol('kResponseDestroyed'); + const server = http.createServer(function(req, res) { - setImmediate(function() { + req.on('data', common.mustCall(function() { res.destroy(); - }); + server.emit(kResponseDestroyed); + })); }); server.listen(0, function() { @@ -40,11 +44,9 @@ server.listen(0, function() { method: 'POST' }); - function write() { - req.write('hello', function() { - setImmediate(write); - }); - } + server.once(kResponseDestroyed, common.mustCall(function() { + req.write('hello'); + })); req.on('error', common.mustCall(function(er) { assert.strictEqual(req.res, null); @@ -73,6 +75,5 @@ server.listen(0, function() { })); req.on('response', common.mustNotCall()); - - write(); + req.write('hello', common.mustSucceed()); });