node/test/parallel/test-net-settimeout.js
Brian White 2bc7841d0f
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

32 lines
762 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(0, function() {
const socket = net.createConnection(this.address().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);
});