From 81671e9fe33349ca000b7dcb2867b92830d19eff Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 8 Feb 2017 22:15:19 -0500 Subject: [PATCH] test: refactor test-dgram-setBroadcast.js This test wasn't actually working, as sockets were being closed, allowing the test to exit before any assertions were actually run. This commit refactors the test to maintain the same intended semantics. --- test/parallel/test-dgram-setBroadcast.js | 44 ++++++++---------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/test/parallel/test-dgram-setBroadcast.js b/test/parallel/test-dgram-setBroadcast.js index 1232faa76c81fe..f4ce7ff06adbe4 100644 --- a/test/parallel/test-dgram-setBroadcast.js +++ b/test/parallel/test-dgram-setBroadcast.js @@ -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(); + })); +}