Skip to content

Commit

Permalink
test: add test for loading native modules from multiple contexts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rpetrich committed Jun 25, 2018
1 parent 78159c3 commit ffdc0c0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/addons/hello-world-from-worker/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <node.h>
#include <v8.h>

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
}

void init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
NODE_SET_METHOD(module, "exports", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, init)
9 changes: 9 additions & 0 deletions test/addons/hello-world-from-worker/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
'targets': [
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.cc' ]
}
]
}
32 changes: 32 additions & 0 deletions test/addons/hello-world-from-worker/test.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit ffdc0c0

Please sign in to comment.