mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 15:32:15 +00:00

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>
40 lines
984 B
JavaScript
40 lines
984 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const dgram = require('dgram');
|
|
|
|
const setup = () => {
|
|
return dgram.createSocket({type: 'udp4', reuseAddr: true});
|
|
};
|
|
|
|
const teardown = (socket) => {
|
|
if (socket.close)
|
|
socket.close();
|
|
};
|
|
|
|
const runTest = (testCode, expectError) => {
|
|
const socket = setup();
|
|
const assertion = expectError ? assert.throws : assert.doesNotThrow;
|
|
const wrapped = () => { testCode(socket); };
|
|
assertion(wrapped, expectError);
|
|
teardown(socket);
|
|
};
|
|
|
|
// Should throw EBADF if socket is never bound.
|
|
runTest((socket) => { socket.setBroadcast(true); }, /EBADF/);
|
|
|
|
// Should not throw if broadcast set to false after binding.
|
|
runTest((socket) => {
|
|
socket.bind(0, common.localhostIPv4, () => {
|
|
socket.setBroadcast(false);
|
|
});
|
|
});
|
|
|
|
// Should not throw if broadcast set to true after binding.
|
|
runTest((socket) => {
|
|
socket.bind(0, common.localhostIPv4, () => {
|
|
socket.setBroadcast(true);
|
|
});
|
|
});
|