mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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: https://github.com/nodejs/node/pull/30551 Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
1 parent
dc9c7709ff
commit
646b81c209
4 files changed
+83
No files matched your search
@@ -0,0 +1,8 @@
|
||||
{
|
||||
'targets': [
|
||||
{
|
||||
'target_name': 'binding',
|
||||
'sources': [ 'test_worker_buffer_callback.c' ]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
// Test that buffers allocated with a free callback through our APIs are
|
||||
// released when a Worker owning it exits.
|
||||
|
||||
const w = new Worker(`require(${JSON.stringify(binding)})`, { eval: true });
|
||||
|
||||
assert.strictEqual(getFreeCallCount(), 0);
|
||||
w.on('exit', common.mustCall(() => {
|
||||
assert.strictEqual(getFreeCallCount(), 1);
|
||||
}));
|
||||
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
const common = require('../../common');
|
||||
const assert = require('assert');
|
||||
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);
|
||||
@@ -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++;
|
||||
}
|
||||
|
||||
NAPI_MODULE_INIT() {
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_PROPERTY("getFreeCallCount", GetFreeCallCount)
|
||||
};
|
||||
|
||||
NAPI_CALL(env, napi_define_properties(
|
||||
env, exports, sizeof(properties) / sizeof(*properties), properties));
|
||||
|
||||
// This is a slight variation on the non-N-API test: We create an ArrayBuffer
|
||||
// rather than a Node.js Buffer, since testing the latter would only test
|
||||
// the same code paths and not the ones specific to N-API.
|
||||
napi_value buffer;
|
||||
NAPI_CALL(env, napi_create_external_arraybuffer(
|
||||
env,
|
||||
data,
|
||||
sizeof(data),
|
||||
finalize_cb,
|
||||
NULL,
|
||||
&buffer));
|
||||
|
||||
NAPI_CALL(env, napi_set_named_property(env, exports, "buffer", buffer));
|
||||
|
||||
return exports;
|
||||
}
|
||||
Reference in new issue
Block a user