Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test-dgram-setBroadcast.js #11252

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 15 additions & 29 deletions test/parallel/test-dgram-setBroadcast.js
Expand Up @@ -4,36 +4,22 @@ const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');

const setup = () => {
return dgram.createSocket({type: 'udp4', reuseAddr: true});
};
{
// Should throw EBADF if the socket is never bound.
const socket = dgram.createSocket('udp4');

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);
};
assert.throws(() => {
socket.setBroadcast(true);
}, /^Error: setBroadcast EBADF$/);
}

// Should throw EBADF if socket is never bound.
runTest((socket) => { socket.setBroadcast(true); }, /EBADF/);
{
// Can call setBroadcast() after binding the socket.
const socket = dgram.createSocket('udp4');

// 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.bind(0, common.mustCall(() => {
socket.setBroadcast(true);
});
});
socket.setBroadcast(false);
socket.close();
}));
}