This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add basic tests for MessageChannel
PR-URL: #116 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |