-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test,doc: add tests and docs for addon unloading
Originally from portions of #23319. Backport-PR-URL: #25258 PR-URL: #24861 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
- Loading branch information
Showing
5 changed files
with
108 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
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,14 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
|
||
const w = new Worker(` | ||
require('worker_threads').parentPort.postMessage( | ||
require(${JSON.stringify(binding)}).hello());`, { eval: true }); | ||
w.on('message', common.mustCall((message) => { | ||
assert.strictEqual(message, 'world'); | ||
})); |
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,46 @@ | ||
#include <assert.h> | ||
#include <node.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <v8.h> | ||
|
||
using v8::Context; | ||
using v8::HandleScope; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::Object; | ||
using v8::Value; | ||
|
||
size_t count = 0; | ||
|
||
struct statically_allocated { | ||
statically_allocated() { | ||
assert(count == 0); | ||
printf("ctor "); | ||
} | ||
~statically_allocated() { | ||
assert(count == 0); | ||
printf("dtor"); | ||
} | ||
} var; | ||
|
||
void Dummy(void*) { | ||
assert(0); | ||
} | ||
|
||
void Cleanup(void* str) { | ||
printf("%s ", static_cast<const char*>(str)); | ||
} | ||
|
||
void Initialize(Local<Object> exports, | ||
Local<Value> module, | ||
Local<Context> context) { | ||
node::AddEnvironmentCleanupHook( | ||
context->GetIsolate(), | ||
Cleanup, | ||
const_cast<void*>(static_cast<const void*>("cleanup"))); | ||
node::AddEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr); | ||
node::RemoveEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr); | ||
} | ||
|
||
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize) |
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,21 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
|
||
if (process.argv[2] === 'child') { | ||
new Worker(`require(${JSON.stringify(binding)});`, { eval: true }); | ||
} else { | ||
const proc = child_process.spawnSync(process.execPath, [ | ||
'--experimental-worker', | ||
__filename, | ||
'child' | ||
]); | ||
assert.strictEqual(proc.stderr.toString(), ''); | ||
assert.strictEqual(proc.stdout.toString(), 'ctor cleanup dtor'); | ||
assert.strictEqual(proc.status, 0); | ||
} |