Skip to content

Commit

Permalink
test: handle unknown message type in worker threads
Browse files Browse the repository at this point in the history
Check that main thread handles an unknown message type from a worker
thread as expected.

PR-URL: #27995
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
Trott authored and targos committed Jun 3, 2019
1 parent 1827256 commit 6a5ce36
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
17 changes: 8 additions & 9 deletions test/parallel/test-worker-message-not-serializable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
// expected.

const common = require('../common');
common.skipIfWorker();

const assert = require('assert');

const { Worker, isMainThread } = require('worker_threads');
if (isMainThread) {
const worker = new Worker(__filename);
worker.on('error', common.mustCall((e) => {
assert.strictEqual(e.code, 'ERR_WORKER_UNSERIALIZABLE_ERROR');
}));
} else {
const { Worker } = require('worker_threads');

const worker = new Worker(`
const { internalBinding } = require('internal/test/binding');
const { getEnvMessagePort } = internalBinding('worker');
const { messageTypes } = require('internal/worker/io');
const messagePort = getEnvMessagePort();
messagePort.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
}
`, { eval: true });

worker.on('error', common.mustCall((e) => {
assert.strictEqual(e.code, 'ERR_WORKER_UNSERIALIZABLE_ERROR');
}));
25 changes: 25 additions & 0 deletions test/parallel/test-worker-message-type-unknown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

// Check that main thread handles an unknown message type from a worker thread
// as expected.

require('../common');

const assert = require('assert');
const { spawnSync } = require('child_process');

const { Worker } = require('worker_threads');
if (process.argv[2] !== 'spawned') {
const result = spawnSync(process.execPath,
[ '--expose-internals', __filename, 'spawned'],
{ encoding: 'utf8' });
assert.ok(result.stderr.includes('Unknown worker message type FHQWHGADS'),
`Expected error not found in: ${result.stderr}`);
} else {
new Worker(`
const { internalBinding } = require('internal/test/binding');
const { getEnvMessagePort } = internalBinding('worker');
const messagePort = getEnvMessagePort();
messagePort.postMessage({ type: 'FHQWHGADS' });
`, { eval: true });
}

0 comments on commit 6a5ce36

Please sign in to comment.