summaryrefslogtreecommitdiff
path: root/test_ffi/tests/test.js
diff options
context:
space:
mode:
authorAapo Alasuutari <aapo.alasuutari@gmail.com>2022-06-28 12:23:36 +0300
committerGitHub <noreply@github.com>2022-06-28 14:53:36 +0530
commit00f4521b205bf25c79f0fa7c9a6840941342bda4 (patch)
treeb988db360143384ae216022846b518ceb262c80a /test_ffi/tests/test.js
parente1c90963fbbf6571ae1b66971b83159681928ec3 (diff)
feat(ext/ffi): Thread safe callbacks (#14942)
Diffstat (limited to 'test_ffi/tests/test.js')
-rw-r--r--test_ffi/tests/test.js41
1 files changed, 36 insertions, 5 deletions
diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js
index ab31dcb83..03c166a7c 100644
--- a/test_ffi/tests/test.js
+++ b/test_ffi/tests/test.js
@@ -130,6 +130,12 @@ const dylib = Deno.dlopen(libPath, {
parameters: ["function"],
result: "void",
},
+ call_fn_ptr_thread_safe: {
+ name: "call_fn_ptr",
+ parameters: ["function"],
+ result: "void",
+ nonblocking: true,
+ },
call_fn_ptr_many_parameters: {
parameters: ["function"],
result: "void",
@@ -138,6 +144,11 @@ const dylib = Deno.dlopen(libPath, {
parameters: ["function"],
result: "void",
},
+ call_fn_ptr_return_u8_thread_safe: {
+ name: "call_fn_ptr_return_u8",
+ parameters: ["function"],
+ result: "void",
+ },
call_fn_ptr_return_buffer: {
parameters: ["function"],
result: "void",
@@ -292,15 +303,16 @@ console.log("After sleep_blocking");
console.log(performance.now() - start >= 100);
start = performance.now();
-dylib.symbols.sleep_nonblocking(100).then(() => {
+const promise_2 = dylib.symbols.sleep_nonblocking(100).then(() => {
console.log("After");
console.log(performance.now() - start >= 100);
- // Close after task is complete.
- cleanup();
});
console.log("Before");
console.log(performance.now() - start < 100);
+// Await to make sure `sleep_nonblocking` calls and logs before we proceed
+await promise_2;
+
// Test calls with callback parameters
const logCallback = new Deno.UnsafeCallback(
{ parameters: [], result: "void" },
@@ -376,6 +388,24 @@ dylib.symbols.store_function(ptr(nestedCallback));
dylib.symbols.store_function(null);
dylib.symbols.store_function_2(null);
+let counter = 0;
+const addToFooCallback = new Deno.UnsafeCallback({
+ parameters: [],
+ result: "void",
+}, () => counter++);
+
+// Test thread safe callbacks
+console.log("Thread safe call counter:", counter);
+addToFooCallback.ref();
+await dylib.symbols.call_fn_ptr_thread_safe(ptr(addToFooCallback));
+addToFooCallback.unref();
+logCallback.ref();
+await dylib.symbols.call_fn_ptr_thread_safe(ptr(logCallback));
+logCallback.unref();
+console.log("Thread safe call counter:", counter);
+returnU8Callback.ref();
+await dylib.symbols.call_fn_ptr_return_u8_thread_safe(ptr(returnU8Callback));
+
// Test statics
console.log("Static u32:", dylib.symbols.static_u32);
console.log("Static i64:", dylib.symbols.static_i64);
@@ -386,7 +416,7 @@ console.log(
const view = new Deno.UnsafePointerView(dylib.symbols.static_ptr);
console.log("Static ptr value:", view.getUint32());
-function cleanup() {
+(function cleanup() {
dylib.close();
throwCallback.close();
logCallback.close();
@@ -395,6 +425,7 @@ function cleanup() {
returnBufferCallback.close();
add10Callback.close();
nestedCallback.close();
+ addToFooCallback.close();
const resourcesPost = Deno.resources();
@@ -409,4 +440,4 @@ After: ${postStr}`,
}
console.log("Correct number of resources");
-}
+})();