8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent be9788b commit 099d3fdCopy full SHA for 099d3fd
test/node-api/test_worker_buffer_callback/binding.gyp
@@ -0,0 +1,8 @@
1
+{
2
+ 'targets': [
3
+ {
4
+ 'target_name': 'binding',
5
+ 'sources': [ 'test_worker_buffer_callback.c' ]
6
+ }
7
+ ]
8
+}
test/node-api/test_worker_buffer_callback/test-free-called.js
@@ -0,0 +1,17 @@
+'use strict';
+const common = require('../../common');
+const path = require('path');
+const assert = require('assert');
+const { Worker } = require('worker_threads');
+const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
+const { getFreeCallCount } = require(binding);
+
9
+// Test that buffers allocated with a free callback through our APIs are
10
+// released when a Worker owning it exits.
11
12
+const w = new Worker(`require(${JSON.stringify(binding)})`, { eval: true });
13
14
+assert.strictEqual(getFreeCallCount(), 0);
15
+w.on('exit', common.mustCall(() => {
16
+ assert.strictEqual(getFreeCallCount(), 1);
17
+}));
test/node-api/test_worker_buffer_callback/test.js
@@ -0,0 +1,15 @@
+const { MessageChannel } = require('worker_threads');
+const { buffer } = require(`./build/${common.buildType}/binding`);
+// Test that buffers allocated with a free callback through our APIs are not
+// transferred.
+const { port1 } = new MessageChannel();
+const origByteLength = buffer.byteLength;
+port1.postMessage(buffer, [buffer]);
+assert.strictEqual(buffer.byteLength, origByteLength);
+assert.notStrictEqual(buffer.byteLength, 0);
test/node-api/test_worker_buffer_callback/test_worker_buffer_callback.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <node_api.h>
+#include <assert.h>
+#include "../../js-native-api/common.h"
+uint32_t free_call_count = 0;
+char data[] = "hello";
+napi_value GetFreeCallCount(napi_env env, napi_callback_info info) {
+ napi_value value;
+ NAPI_CALL(env, napi_create_uint32(env, free_call_count, &value));
+ return value;
+static void finalize_cb(napi_env env, void* finalize_data, void* hint) {
+ assert(finalize_data == data);
+ free_call_count++;
18
19
20
+NAPI_MODULE_INIT() {
21
+ napi_property_descriptor properties[] = {
22
+ DECLARE_NAPI_PROPERTY("getFreeCallCount", GetFreeCallCount)
23
+ };
24
25
+ NAPI_CALL(env, napi_define_properties(
26
+ env, exports, sizeof(properties) / sizeof(*properties), properties));
27
28
+ // This is a slight variation on the non-N-API test: We create an ArrayBuffer
29
+ // rather than a Node.js Buffer, since testing the latter would only test
30
+ // the same code paths and not the ones specific to N-API.
31
+ napi_value buffer;
32
+ NAPI_CALL(env, napi_create_external_arraybuffer(
33
+ env,
34
+ data,
35
+ sizeof(data),
36
+ finalize_cb,
37
+ NULL,
38
+ &buffer));
39
40
+ NAPI_CALL(env, napi_set_named_property(env, exports, "buffer", buffer));
41
42
+ return exports;
43