-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
54 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,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) |
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,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], | ||
'sources': [ 'binding.cc' ] | ||
} | ||
] | ||
} |
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,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); | ||
} |