mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 20:29:23 +01:00
e038d6a1cd
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>
149 lines
4.3 KiB
JavaScript
149 lines
4.3 KiB
JavaScript
// Flags: --expose-internals
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const EventEmitter = require('events');
|
|
const SocketListSend = require('internal/socket_list').SocketListSend;
|
|
|
|
const key = 'test-key';
|
|
|
|
// Verify that an error will be received in callback when child is not
|
|
// connected.
|
|
{
|
|
const child = Object.assign(new EventEmitter(), { connected: false });
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
|
|
const list = new SocketListSend(child, 'test');
|
|
|
|
list._request('msg', 'cmd', false, common.mustCall((err) => {
|
|
common.expectsError({
|
|
code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
|
|
name: 'Error',
|
|
message: 'Child closed before reply received'
|
|
})(err);
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
}));
|
|
}
|
|
|
|
// Verify that the given message will be received in callback.
|
|
{
|
|
const child = Object.assign(new EventEmitter(), {
|
|
connected: true,
|
|
_send: function(msg) {
|
|
process.nextTick(() =>
|
|
this.emit('internalMessage', { key, cmd: 'cmd' })
|
|
);
|
|
}
|
|
});
|
|
|
|
const list = new SocketListSend(child, key);
|
|
|
|
list._request('msg', 'cmd', false, common.mustCall((err, msg) => {
|
|
assert.strictEqual(err, null);
|
|
assert.strictEqual(msg.cmd, 'cmd');
|
|
assert.strictEqual(msg.key, key);
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
assert.strictEqual(child.listenerCount('disconnect'), 0);
|
|
}));
|
|
}
|
|
|
|
// Verify that an error will be received in callback when child was
|
|
// disconnected.
|
|
{
|
|
const child = Object.assign(new EventEmitter(), {
|
|
connected: true,
|
|
_send: function(msg) { process.nextTick(() => this.emit('disconnect')); }
|
|
});
|
|
|
|
const list = new SocketListSend(child, key);
|
|
|
|
list._request('msg', 'cmd', false, common.mustCall((err) => {
|
|
common.expectsError({
|
|
code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
|
|
name: 'Error',
|
|
message: 'Child closed before reply received'
|
|
})(err);
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
}));
|
|
}
|
|
|
|
// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be received
|
|
// in callback.
|
|
{
|
|
const child = Object.assign(new EventEmitter(), {
|
|
connected: true,
|
|
_send: function(msg) {
|
|
assert.strictEqual(msg.cmd, 'NODE_SOCKET_NOTIFY_CLOSE');
|
|
assert.strictEqual(msg.key, key);
|
|
process.nextTick(() =>
|
|
this.emit('internalMessage', { key, cmd: 'NODE_SOCKET_ALL_CLOSED' })
|
|
);
|
|
}
|
|
});
|
|
|
|
const list = new SocketListSend(child, key);
|
|
|
|
list.close(common.mustCall((err, msg) => {
|
|
assert.strictEqual(err, null);
|
|
assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED');
|
|
assert.strictEqual(msg.key, key);
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
assert.strictEqual(child.listenerCount('disconnect'), 0);
|
|
}));
|
|
}
|
|
|
|
// Verify that the count of connections will be received in callback.
|
|
{
|
|
const count = 1;
|
|
const child = Object.assign(new EventEmitter(), {
|
|
connected: true,
|
|
_send: function(msg) {
|
|
assert.strictEqual(msg.cmd, 'NODE_SOCKET_GET_COUNT');
|
|
assert.strictEqual(msg.key, key);
|
|
process.nextTick(() =>
|
|
this.emit('internalMessage', {
|
|
key,
|
|
count,
|
|
cmd: 'NODE_SOCKET_COUNT'
|
|
})
|
|
);
|
|
}
|
|
});
|
|
|
|
const list = new SocketListSend(child, key);
|
|
|
|
list.getConnections(common.mustCall((err, msg) => {
|
|
assert.strictEqual(err, null);
|
|
assert.strictEqual(msg, count);
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
assert.strictEqual(child.listenerCount('disconnect'), 0);
|
|
}));
|
|
}
|
|
|
|
// Verify that an error will be received in callback when child is
|
|
// disconnected after sending a message and before getting the reply.
|
|
{
|
|
const count = 1;
|
|
const child = Object.assign(new EventEmitter(), {
|
|
connected: true,
|
|
_send: function() {
|
|
process.nextTick(() => {
|
|
this.emit('disconnect');
|
|
this.emit('internalMessage', { key, count, cmd: 'NODE_SOCKET_COUNT' });
|
|
});
|
|
}
|
|
});
|
|
|
|
const list = new SocketListSend(child, key);
|
|
|
|
list.getConnections(common.mustCall((err) => {
|
|
common.expectsError({
|
|
code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
|
|
name: 'Error',
|
|
message: 'Child closed before reply received'
|
|
})(err);
|
|
assert.strictEqual(child.listenerCount('internalMessage'), 0);
|
|
}));
|
|
}
|