From 6b38a67dc50485228f14fb830adbf4a988a6f8ab Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 28 Nov 2015 14:38:15 -0800 Subject: [PATCH 1/2] test: refactor flaky net-error-twice The test was not reliably creating the error event on Windows 2012. This makes the test more robust by using setImmediate() to delay the server writing to the socket to give time for socket.destroy() to take effect. Fixes: https://github.com/nodejs/node/issues/4057 --- test/parallel/test-net-error-twice.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/test/parallel/test-net-error-twice.js b/test/parallel/test-net-error-twice.js index af92ca93206f18..9203be9340498f 100644 --- a/test/parallel/test-net-error-twice.js +++ b/test/parallel/test-net-error-twice.js @@ -1,33 +1,32 @@ 'use strict'; -var common = require('../common'); -var assert = require('assert'); -var net = require('net'); +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); -var buf = new Buffer(10 * 1024 * 1024); +const buf = new Buffer(10 * 1024 * 1024); buf.fill(0x62); var errs = []; -var srv = net.createServer(function onConnection(conn) { - conn.write(buf); +const srv = net.createServer(function onConnection(conn) { + setImmediate(() => { + conn.write(buf); + }); + conn.on('error', function(err) { errs.push(err); - if (errs.length > 1 && errs[0] === errs[1]) - assert(false, 'We should not be emitting the same error twice'); + if (errs.length > 1) + assert(errs[0] !== errs[1], 'Should not get the same error twice'); }); conn.on('close', function() { srv.unref(); }); }).listen(common.PORT, function() { - var client = net.connect({ port: common.PORT }); - - client.on('connect', function() { - client.destroy(); - }); + const client = net.connect({ port: common.PORT }); + client.on('connect', client.destroy); }); process.on('exit', function() { - console.log(errs); assert.equal(errs.length, 1); }); From c30dc10d18976de7e9d25cdd04bc701db768d86f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 29 Nov 2015 08:02:21 -0800 Subject: [PATCH 2/2] fixup: windows-specific handling --- test/parallel/test-net-error-twice.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-net-error-twice.js b/test/parallel/test-net-error-twice.js index 9203be9340498f..2dedb32271ff60 100644 --- a/test/parallel/test-net-error-twice.js +++ b/test/parallel/test-net-error-twice.js @@ -10,9 +10,14 @@ buf.fill(0x62); var errs = []; const srv = net.createServer(function onConnection(conn) { - setImmediate(() => { + if (common.isWindows) { + // Windows-specific handling. See: + // * https://github.com/nodejs/node/pull/4062 + // * https://github.com/nodejs/node/issues/4057 + setTimeout(() => { conn.write(buf); }, 100); + } else { conn.write(buf); - }); + } conn.on('error', function(err) { errs.push(err);