node/test/parallel/test-dgram-membership.js
Michael Dawson f2e3a670af dgram: convert to using internal/errors
Covert lib/dgram.js over to using lib/internal/errors.js
for generating Errors. See
[using-internal-errors.md](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md)
for more details.

I have not addressed the cases that use errnoException() and
exceptionWithHostPort() helper methods as changing these would require
fixing the tests across all of the different files that use them. In
addition, these helpers already add a `code` to the Error and we'll
have to discuss how that interacts with the `code` used by
lib/internal/errors.js.  I believe we should convert all users
of errnoException and exceptionWithHostPort in a PR dedicated to
that conversion.

PR-URL: https://github.com/nodejs/node/pull/12926
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben.bridgewater@fintura.de>
2017-05-28 07:00:40 -07:00

107 lines
2.6 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const multicastAddress = '224.0.0.114';
const setup = dgram.createSocket.bind(dgram, {type: 'udp4', reuseAddr: true});
// addMembership() on closed socket should throw
{
const socket = setup();
socket.close(common.mustCall(() => {
assert.throws(() => {
socket.addMembership(multicastAddress);
}, common.expectsError({
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
type: Error,
message: /^Not running$/
}));
}));
}
// dropMembership() on closed socket should throw
{
const socket = setup();
socket.close(common.mustCall(() => {
assert.throws(() => {
socket.dropMembership(multicastAddress);
}, common.expectsError({
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
type: Error,
message: /^Not running$/
}));
}));
}
// addMembership() with no argument should throw
{
const socket = setup();
assert.throws(() => {
socket.addMembership();
}, common.expectsError({
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: /^The "multicastAddress" argument must be specified$/
}));
socket.close();
}
// dropMembership() with no argument should throw
{
const socket = setup();
assert.throws(() => {
socket.dropMembership();
}, common.expectsError({
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: /^The "multicastAddress" argument must be specified$/
}));
socket.close();
}
// addMembership() with invalid multicast address should throw
{
const socket = setup();
assert.throws(() => { socket.addMembership('256.256.256.256'); },
/^Error: addMembership EINVAL$/);
socket.close();
}
// dropMembership() with invalid multicast address should throw
{
const socket = setup();
assert.throws(() => { socket.dropMembership('256.256.256.256'); },
/^Error: dropMembership EINVAL$/);
socket.close();
}
// addMembership() with valid socket and multicast address should not throw
{
const socket = setup();
assert.doesNotThrow(() => { socket.addMembership(multicastAddress); });
socket.close();
}
// dropMembership() without previous addMembership should throw
{
const socket = setup();
assert.throws(
() => { socket.dropMembership(multicastAddress); },
/^Error: dropMembership EADDRNOTAVAIL$/
);
socket.close();
}
// dropMembership() after addMembership() should not throw
{
const socket = setup();
assert.doesNotThrow(
() => {
socket.addMembership(multicastAddress);
socket.dropMembership(multicastAddress);
}
);
socket.close();
}