0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-worker-message-port-receive-message.js
Anna Henningsen d699fdd2c6
worker: mention argument name in type check message
Refs: https://github.com/nodejs/node/pull/32745#discussion_r406564208

PR-URL: https://github.com/nodejs/node/pull/32815
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
2020-04-15 16:36:27 +02:00

34 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel, receiveMessageOnPort } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
const message1 = { hello: 'world' };
const message2 = { foo: 'bar' };
// Make sure receiveMessageOnPort() works in a FIFO way, the same way it does
// when were using events.
assert.deepStrictEqual(receiveMessageOnPort(port2), undefined);
port1.postMessage(message1);
port1.postMessage(message2);
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message2 });
assert.deepStrictEqual(receiveMessageOnPort(port2), undefined);
assert.deepStrictEqual(receiveMessageOnPort(port2), undefined);
// Make sure message handlers arent called.
port2.on('message', common.mustNotCall());
port1.postMessage(message1);
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
port1.close();
for (const value of [null, 0, -1, {}, []]) {
assert.throws(() => receiveMessageOnPort(value), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "port" argument must be a MessagePort instance'
});
}