Skip to content

Commit fe2b5f0

Browse files
cjihrigjasnell
authored andcommittedFeb 11, 2017
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. PR-URL: nodejs#11252 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5cd9d76 commit fe2b5f0

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed
 

‎test/parallel/test-dgram-setBroadcast.js

+15-29
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,22 @@ const common = require('../common');
44
const assert = require('assert');
55
const dgram = require('dgram');
66

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

11-
const teardown = (socket) => {
12-
if (socket.close)
13-
socket.close();
14-
};
15-
16-
const runTest = (testCode, expectError) => {
17-
const socket = setup();
18-
const assertion = expectError ? assert.throws : assert.doesNotThrow;
19-
const wrapped = () => { testCode(socket); };
20-
assertion(wrapped, expectError);
21-
teardown(socket);
22-
};
11+
assert.throws(() => {
12+
socket.setBroadcast(true);
13+
}, /^Error: setBroadcast EBADF$/);
14+
}
2315

24-
// Should throw EBADF if socket is never bound.
25-
runTest((socket) => { socket.setBroadcast(true); }, /EBADF/);
16+
{
17+
// Can call setBroadcast() after binding the socket.
18+
const socket = dgram.createSocket('udp4');
2619

27-
// Should not throw if broadcast set to false after binding.
28-
runTest((socket) => {
29-
socket.bind(0, common.localhostIPv4, () => {
30-
socket.setBroadcast(false);
31-
});
32-
});
33-
34-
// Should not throw if broadcast set to true after binding.
35-
runTest((socket) => {
36-
socket.bind(0, common.localhostIPv4, () => {
20+
socket.bind(0, common.mustCall(() => {
3721
socket.setBroadcast(true);
38-
});
39-
});
22+
socket.setBroadcast(false);
23+
socket.close();
24+
}));
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.