-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
dc9c770
commit 646b81c
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
test/node-api/test_worker_buffer_callback/test-free-called.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
43 changes: 43 additions & 0 deletions
43
test/node-api/test_worker_buffer_callback/test_worker_buffer_callback.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |