node/test/parallel/test-net-settimeout.js
Santiago Gimeno bf22c71a7a test: fix test-net-settimeout flakiness
Wait for the data to be received by the socket before creating the
clean-up timer. This way, a possible (though unlikely) `ECONNRESET`
error can be avoided.

PR-URL: https://github.com/nodejs/node/pull/6166
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2016-04-19 09:08:02 +02:00

32 lines
725 B
JavaScript

'use strict';
// This example sets a timeout then immediately attempts to disable the timeout
// https://github.com/joyent/node/pull/2245
const common = require('../common');
const net = require('net');
const assert = require('assert');
const T = 100;
const server = net.createServer(common.mustCall((c) => {
c.write('hello');
}));
server.listen(common.PORT);
const socket = net.createConnection(common.PORT, 'localhost');
const s = socket.setTimeout(T, () => {
common.fail('Socket timeout event is not expected to fire');
});
assert.ok(s instanceof net.Socket);
socket.on('data', common.mustCall(() => {
setTimeout(function() {
socket.destroy();
server.close();
}, T * 2);
}));
socket.setTimeout(0);