Skip to content

Commit 6b38a67

Browse files
committed
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: #4057
1 parent d1000b4 commit 6b38a67

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
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

1010
var errs = [];
1111

12-
var srv = net.createServer(function onConnection(conn) {
13-
conn.write(buf);
12+
const srv = net.createServer(function onConnection(conn) {
13+
setImmediate(() => {
14+
conn.write(buf);
15+
});
16+
1417
conn.on('error', function(err) {
1518
errs.push(err);
16-
if (errs.length > 1 && errs[0] === errs[1])
17-
assert(false, 'We should not be emitting the same error twice');
19+
if (errs.length > 1)
20+
assert(errs[0] !== errs[1], 'Should not get the same error twice');
1821
});
1922
conn.on('close', function() {
2023
srv.unref();
2124
});
2225
}).listen(common.PORT, function() {
23-
var client = net.connect({ port: common.PORT });
24-
25-
client.on('connect', function() {
26-
client.destroy();
27-
});
26+
const client = net.connect({ port: common.PORT });
27+
client.on('connect', client.destroy);
2828
});
2929

3030
process.on('exit', function() {
31-
console.log(errs);
3231
assert.equal(errs.length, 1);
3332
});

0 commit comments

Comments
 (0)