-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test,dgram: add tests for setBroadcast()
The only tests for `setBroadcast()` (from the `dgram` module) were in `test/internet` which means they almost never get run. This adds a minimal test that can check JS-land functionality in `test/parallel`. I also expanded a comment and did some minor formatting on the existing `test/internet` test. If there were an easy and reliable way to check for the BROADCAST flag on an interface, it's possible that a version of the test could be moved to `test/sequential` or `test/parallel` once it was modified to only use internal networks.
- Loading branch information
Showing
2 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'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(common.PORT, common.localhostIPv4, () => { | ||
socket.setBroadcast(false); | ||
}); | ||
}); | ||
|
||
// Should not throw if broadcast set to true after binding. | ||
runTest((socket) => { | ||
socket.bind(common.PORT, common.localhostIPv4, () => { | ||
socket.setBroadcast(true); | ||
}); | ||
}); |