Skip to content

Commit 9b22acc

Browse files
TrottFishrock123
authored andcommitted
test: fix flaky test-net-write-after-close
Replace 250ms timer with event-based logic to make test robust. PR-URL: #14361 Fixes: #13597 Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 11ae8c3 commit 9b22acc

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

test/parallel/test-net-write-after-close.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,31 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
2425
const net = require('net');
2526

27+
let serverSocket;
28+
2629
const server = net.createServer(common.mustCall(function(socket) {
30+
serverSocket = socket;
31+
2732
socket.resume();
2833

2934
socket.on('error', common.mustCall(function(error) {
30-
console.error('got error, closing server', error);
35+
console.error('received error as expected, closing server', error);
3136
server.close();
3237
}));
33-
34-
setTimeout(common.mustCall(function() {
35-
console.error('about to try to write');
36-
socket.write('test', common.mustCall());
37-
}), 250);
3838
}));
3939

4040
server.listen(0, function() {
4141
const client = net.connect(this.address().port, function() {
42+
// cliend.end() will close both the readable and writable side
43+
// of the duplex because allowHalfOpen defaults to false.
44+
// Then 'end' will be emitted when it receives a FIN packet from
45+
// the other side.
46+
client.on('end', common.mustCall(() => {
47+
serverSocket.write('test', common.mustCall());
48+
}));
4249
client.end();
4350
});
4451
});

0 commit comments

Comments
 (0)