Skip to content

Commit

Permalink
test: port worker + buffer test to N-API
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
addaleax committed Nov 30, 2019
1 parent be9788b commit 099d3fd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/node-api/test_worker_buffer_callback/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': [ 'test_worker_buffer_callback.c' ]
}
]
}
17 changes: 17 additions & 0 deletions test/node-api/test_worker_buffer_callback/test-free-called.js
Original file line number Diff line number Diff line change
@@ -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);
}));
15 changes: 15 additions & 0 deletions test/node-api/test_worker_buffer_callback/test.js
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 099d3fd

Please sign in to comment.