Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
test: add basic tests for MessageChannel
Browse files Browse the repository at this point in the history
PR-URL: #116
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
addaleax committed Nov 2, 2017
1 parent 3ddde2c commit 8e049c1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/parallel/test-message-channel-sharedarraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*global SharedArrayBuffer*/
'use strict';
// Flags: --harmony-sharedarraybuffer --expose-gc

const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker');

{
const sharedArrayBuffer = new SharedArrayBuffer(12);
const local = Buffer.from(sharedArrayBuffer);

const w = new Worker(`
require('worker').on('workerMessage', ({ sharedArrayBuffer }) => {
const local = Buffer.from(sharedArrayBuffer);
local.write('world!', 6);
require('worker').postMessage();
});
`, { eval: true });
w.on('message', common.mustCall(() => {
assert.strictEqual(local.toString(), 'Hello world!');
global.gc();
w.terminate();
}));
w.postMessage({ sharedArrayBuffer });
// This would be a race condition if the memory regions were overlapping
local.write('Hello ');
}
45 changes: 45 additions & 0 deletions test/parallel/test-message-channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker, MessagePort, MessageChannel } = require('worker');

{
const channel = new MessageChannel();

const w = new Worker(`
const { MessagePort } = require('worker');
const assert = require('assert');
require('worker').on('workerMessage', ({ port }) => {
assert(port instanceof MessagePort);
port.postMessage('works');
});
`, { eval: true });
w.postMessage({ port: channel.port2 }, [ channel.port2 ]);
assert(channel.port1 instanceof MessagePort);
assert(channel.port2 instanceof MessagePort);
channel.port1.on('message', common.mustCall((message) => {
assert.strictEqual(message, 'works');
w.terminate();
}));
}

{
const channel = new MessageChannel();

channel.port1.on('message', common.mustCall(({ typedArray }) => {
assert.deepStrictEqual(typedArray, new Uint8Array([0, 1, 2, 3, 4]));
}));

const typedArray = new Uint8Array([0, 1, 2, 3, 4]);
channel.port2.postMessage({ typedArray }, [ typedArray.buffer ]);
assert.strictEqual(typedArray.buffer.byteLength, 0);
channel.port2.close();
}

{
const channel = new MessageChannel();

channel.port1.on('close', common.mustCall());
channel.port2.on('close', common.mustCall());
channel.port2.close();
}

0 comments on commit 8e049c1

Please sign in to comment.