From 04053c4124b095bddd28e7b968761b51896a7d83 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 31 May 2019 16:50:00 +0200 Subject: [PATCH 1/2] test: add coverage for unserializable worker thread error Check that the main thread handles unserializable errors in a worker thread as expected. PR-URL: https://github.com/nodejs/node/pull/27995 Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca --- .../test-worker-message-not-serializable.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/parallel/test-worker-message-not-serializable.js diff --git a/test/parallel/test-worker-message-not-serializable.js b/test/parallel/test-worker-message-not-serializable.js new file mode 100644 index 00000000000000..875cc1d58a148a --- /dev/null +++ b/test/parallel/test-worker-message-not-serializable.js @@ -0,0 +1,25 @@ +'use strict'; + +// Flags: --expose-internals + +// Check that main thread handles unserializable errors in a worker thread as +// 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 { 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 }); +} From e8f31191902a8304f77f7ed4377f10de91aca103 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 31 May 2019 17:46:14 +0200 Subject: [PATCH 2/2] test: handle unknown message type in worker threads Check that main thread handles an unknown message type from a worker thread as expected. PR-URL: https://github.com/nodejs/node/pull/27995 Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca --- .../test-worker-message-not-serializable.js | 17 ++++++------- .../test-worker-message-type-unknown.js | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-worker-message-type-unknown.js diff --git a/test/parallel/test-worker-message-not-serializable.js b/test/parallel/test-worker-message-not-serializable.js index 875cc1d58a148a..3753c7de6cbdf9 100644 --- a/test/parallel/test-worker-message-not-serializable.js +++ b/test/parallel/test-worker-message-not-serializable.js @@ -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'); +})); diff --git a/test/parallel/test-worker-message-type-unknown.js b/test/parallel/test-worker-message-type-unknown.js new file mode 100644 index 00000000000000..32f6c2a32bd6b4 --- /dev/null +++ b/test/parallel/test-worker-message-type-unknown.js @@ -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 }); +}