Skip to content

Commit 9808521

Browse files
mscdexMyles Borins
authored and
Myles Borins
committed
test: fix flaky test-net-error-twice
On Windows there can exist some race condition where the notification of the client's `socket.destroy()` isn't received before the server writes to the socket. This race condition was more evident/reproducible on a single core system. This commit fixes the flakiness by waiting until the server's connection event handler has been called to destroy the client socket and perform the server socket write. Fixes: #4057 PR-URL: #4342 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: João Reis <[email protected]>
1 parent 1bc44e7 commit 9808521

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

test/parallel/test-net-error-twice.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var net = require('net');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
55

6-
var buf = new Buffer(10 * 1024 * 1024);
6+
const buf = new Buffer(10 * 1024 * 1024);
77

88
buf.fill(0x62);
99

10-
var errs = [];
10+
const errs = [];
11+
var clientSocket;
12+
var serverSocket;
13+
14+
function ready() {
15+
if (clientSocket && serverSocket) {
16+
clientSocket.destroy();
17+
serverSocket.write(buf);
18+
}
19+
}
1120

1221
var srv = net.createServer(function onConnection(conn) {
13-
conn.write(buf);
1422
conn.on('error', function(err) {
1523
errs.push(err);
1624
if (errs.length > 1 && errs[0] === errs[1])
@@ -19,11 +27,14 @@ var srv = net.createServer(function onConnection(conn) {
1927
conn.on('close', function() {
2028
srv.unref();
2129
});
30+
serverSocket = conn;
31+
ready();
2232
}).listen(common.PORT, function() {
2333
var client = net.connect({ port: common.PORT });
2434

2535
client.on('connect', function() {
26-
client.destroy();
36+
clientSocket = client;
37+
ready();
2738
});
2839
});
2940

0 commit comments

Comments
 (0)