From ffdc0c03a3e9095feb831de28d276ae59414ec99 Mon Sep 17 00:00:00 2001 From: Ryan Petrich Date: Sun, 24 Jun 2018 23:36:05 -0400 Subject: [PATCH] test: add test for loading native modules from multiple contexts Introduce a new addons/hello-world-from-worker test that loads and calls a native module from the main thread, then dispatches a worker to do the same. This ensures that a module is callable from multiple threads/v8 isolates. --- .../addons/hello-world-from-worker/binding.cc | 13 ++++++++ .../hello-world-from-worker/binding.gyp | 9 ++++++ test/addons/hello-world-from-worker/test.js | 32 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 test/addons/hello-world-from-worker/binding.cc create mode 100644 test/addons/hello-world-from-worker/binding.gyp create mode 100644 test/addons/hello-world-from-worker/test.js diff --git a/test/addons/hello-world-from-worker/binding.cc b/test/addons/hello-world-from-worker/binding.cc new file mode 100644 index 00000000000000..55d64b3d6c3513 --- /dev/null +++ b/test/addons/hello-world-from-worker/binding.cc @@ -0,0 +1,13 @@ +#include +#include + +void Method(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world")); +} + +void init(v8::Local exports, v8::Local module) { + NODE_SET_METHOD(module, "exports", Method); +} + +NODE_MODULE(NODE_GYP_MODULE_NAME, init) diff --git a/test/addons/hello-world-from-worker/binding.gyp b/test/addons/hello-world-from-worker/binding.gyp new file mode 100644 index 00000000000000..7ede63d94a0d77 --- /dev/null +++ b/test/addons/hello-world-from-worker/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons/hello-world-from-worker/test.js b/test/addons/hello-world-from-worker/test.js new file mode 100644 index 00000000000000..ff96a16ca012f8 --- /dev/null +++ b/test/addons/hello-world-from-worker/test.js @@ -0,0 +1,32 @@ +// Flags: --experimental-worker +'use strict'; +const assert = require('assert'); + +function testModule() { + const common = require('../../common'); + const binding = require(`./build/${common.buildType}/binding`); + assert.strictEqual(binding(), 'world'); + console.log('binding.hello() =', binding()); +} + +const { Worker, isMainThread, parentPort } = require('worker_threads'); +if (isMainThread) { + testModule(); + const worker = new Worker(__filename); + worker.on('message', (message) => { + assert.strictEqual(message, undefined); + worker.terminate(); + }); + worker.on('error', (error) => { + console.error(error); + worker.terminate(); + }); +} else { + let response; + try { + testModule(); + } catch (e) { + response = e + ''; + } + parentPort.postMessage(response); +}