Skip to content

Commit d6c211d

Browse files
addaleaxtargos
authored andcommitted
test: port worker + buffer test to N-API
This ports `test/addons/worker-buffer-callback` to N-API, with the small exception of using external `ArrayBuffer`s rather than external Node.js `Buffer`s. PR-URL: #30551 Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 499fb42 commit d6c211d

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': [ 'test_worker_buffer_callback.c' ]
6+
}
7+
]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const path = require('path');
4+
const assert = require('assert');
5+
const { Worker } = require('worker_threads');
6+
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
7+
const { getFreeCallCount } = require(binding);
8+
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+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
const { MessageChannel } = require('worker_threads');
5+
const { buffer } = require(`./build/${common.buildType}/binding`);
6+
7+
// Test that buffers allocated with a free callback through our APIs are not
8+
// transferred.
9+
10+
const { port1 } = new MessageChannel();
11+
const origByteLength = buffer.byteLength;
12+
port1.postMessage(buffer, [buffer]);
13+
14+
assert.strictEqual(buffer.byteLength, origByteLength);
15+
assert.notStrictEqual(buffer.byteLength, 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <node_api.h>
3+
#include <assert.h>
4+
#include "../../js-native-api/common.h"
5+
6+
uint32_t free_call_count = 0;
7+
char data[] = "hello";
8+
9+
napi_value GetFreeCallCount(napi_env env, napi_callback_info info) {
10+
napi_value value;
11+
NAPI_CALL(env, napi_create_uint32(env, free_call_count, &value));
12+
return value;
13+
}
14+
15+
static void finalize_cb(napi_env env, void* finalize_data, void* hint) {
16+
assert(finalize_data == data);
17+
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+
}

0 commit comments

Comments
 (0)