diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-12-05 17:23:56 -0800 |
---|---|---|
committer | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-12-19 18:34:28 -0800 |
commit | 622e348d8f70a4ec006ee1ce9207a6a5bc3fc325 (patch) | |
tree | 83bdaaabb0acad67cb1c538f367ecace121a522a /test | |
parent | bf36f0755c0a41702fe506bcfc90d156030d0497 (diff) | |
download | android-node-v8-622e348d8f70a4ec006ee1ce9207a6a5bc3fc325.tar.gz android-node-v8-622e348d8f70a4ec006ee1ce9207a6a5bc3fc325.tar.bz2 android-node-v8-622e348d8f70a4ec006ee1ce9207a6a5bc3fc325.zip |
test,doc: add tests and docs for addon unloading
Originally from portions of https://github.com/nodejs/node/pull/23319/.
PR-URL: https://github.com/nodejs/node/pull/24861
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/addons/hello-world/test-worker.js | 14 | ||||
-rw-r--r-- | test/addons/worker-addon/binding.cc | 46 | ||||
-rw-r--r-- | test/addons/worker-addon/binding.gyp | 9 | ||||
-rw-r--r-- | test/addons/worker-addon/test.js | 21 |
4 files changed, 90 insertions, 0 deletions
diff --git a/test/addons/hello-world/test-worker.js b/test/addons/hello-world/test-worker.js new file mode 100644 index 0000000000..f989c738c8 --- /dev/null +++ b/test/addons/hello-world/test-worker.js @@ -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'); +})); diff --git a/test/addons/worker-addon/binding.cc b/test/addons/worker-addon/binding.cc new file mode 100644 index 0000000000..1fb85ae230 --- /dev/null +++ b/test/addons/worker-addon/binding.cc @@ -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) diff --git a/test/addons/worker-addon/binding.gyp b/test/addons/worker-addon/binding.gyp new file mode 100644 index 0000000000..7ede63d94a --- /dev/null +++ b/test/addons/worker-addon/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons/worker-addon/test.js b/test/addons/worker-addon/test.js new file mode 100644 index 0000000000..2f4bb512fc --- /dev/null +++ b/test/addons/worker-addon/test.js @@ -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); +} |