Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor flaky net-error-twice #4062

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions test/parallel/test-net-error-twice.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
'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) {
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the delay necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis Concise-but-naive answer: Without the delay, the test is flaky on Windows 2012 r2 on the CI infrastructure.

Less concise and marginally less naive: In this test, conn.write(buf) is supposed to get an error (either EPIPE or ECONNRESET depending on timing). Unfortunately, on Windows 2012 r2 only, it appears that the call to client.destroy() often returns before the socket is set to emit the error. UPDATE: Actually, I think the problem is that the client connection and server connection callbacks fire in an unpredictable order. On Windows 2012 r2 only, the conn.write() would often not trigger an error and the result was assert.equal(errs.length, 1) firing (because errs.length is 0). I tried setImmediate() and that may have improved things but it did not resolve the flakiness. Using the timeout resolved the flakiness. (I did not experiment with a timeout of less than 100 milliseconds.)

As for the cause of this on Windows 2012 r2, I don't know and would certainly be receptive to some guidance on figuring it out (or someone simply doing the work and identifying the source of the issue). Operating system limitation? Bug in libuv? Bug in socket.destroy? I don't know. I don't have easy access to a Windows 2012 r2 machine. (I could try to run one via VirtualBox, but my laptop resources are kind of limited right now and my weak excuse is that it would probably fill up my disk.)

} else {
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);
});