mirror of
https://github.com/nodejs/node.git
synced 2025-05-20 05:01:48 +00:00

This completely refactors the `expectsError` behavior: so far it's almost identical to `assert.throws(fn, object)` in case it was used with a function as first argument. It had a magical property check that allowed to verify a functions `type` in case `type` was passed used in the validation object. This pattern is now completely removed and `assert.throws()` should be used instead. The main intent for `common.expectsError()` is to verify error cases for callback based APIs. This is now more flexible by accepting all validation possibilites that `assert.throws()` accepts as well. No magical properties exist anymore. This reduces surprising behavior for developers who are not used to the Node.js core code base. This has the side effect that `common` is used significantly less frequent. PR-URL: https://github.com/nodejs/node/pull/31092 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const dgram = require('dgram');
|
|
const invalidTypes = [
|
|
'test',
|
|
['udp4'],
|
|
new String('udp4'),
|
|
1,
|
|
{},
|
|
true,
|
|
false,
|
|
null,
|
|
undefined
|
|
];
|
|
const validTypes = [
|
|
'udp4',
|
|
'udp6',
|
|
{ type: 'udp4' },
|
|
{ type: 'udp6' }
|
|
];
|
|
const errMessage = /^Bad socket type specified\. Valid types are: udp4, udp6$/;
|
|
|
|
// Error must be thrown with invalid types
|
|
invalidTypes.forEach((invalidType) => {
|
|
assert.throws(() => {
|
|
dgram.createSocket(invalidType);
|
|
}, {
|
|
code: 'ERR_SOCKET_BAD_TYPE',
|
|
name: 'TypeError',
|
|
message: errMessage
|
|
});
|
|
});
|
|
|
|
// Error must not be thrown with valid types
|
|
validTypes.forEach((validType) => {
|
|
const socket = dgram.createSocket(validType);
|
|
socket.close();
|
|
});
|
|
|
|
// Ensure buffer sizes can be set
|
|
{
|
|
const socket = dgram.createSocket({
|
|
type: 'udp4',
|
|
recvBufferSize: 10000,
|
|
sendBufferSize: 15000
|
|
});
|
|
|
|
socket.bind(common.mustCall(() => {
|
|
// note: linux will double the buffer size
|
|
assert.ok(socket.getRecvBufferSize() === 10000 ||
|
|
socket.getRecvBufferSize() === 20000,
|
|
'SO_RCVBUF not 10000 or 20000, ' +
|
|
`was ${socket.getRecvBufferSize()}`);
|
|
assert.ok(socket.getSendBufferSize() === 15000 ||
|
|
socket.getSendBufferSize() === 30000,
|
|
'SO_SNDBUF not 15000 or 30000, ' +
|
|
`was ${socket.getRecvBufferSize()}`);
|
|
socket.close();
|
|
}));
|
|
}
|