From 0a0aa36c28c80faac9bd74a57bf6e38fd6d31d34 Mon Sep 17 00:00:00 2001 From: Bo Date: Wed, 29 Mar 2023 12:53:57 +0800 Subject: [PATCH] test(napi): update test case for checking electron renderer crash (#1547) (cherry picked from commit d22598dbb1082de8ac712de954cd5616c838a48d) --- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 3892 -> 3900 bytes examples/napi/electron-renderer/index.js | 4 ++-- examples/napi/index.d.ts | 1 + examples/napi/src/threadsafe_function.rs | 15 ++++++++++++++- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index a907c780..8c68405a 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -207,6 +207,7 @@ Generated by [AVA](https://avajs.dev). export function withoutAbortController(a: number, b: number): Promise␊ export function withAbortController(a: number, b: number, signal: AbortSignal): Promise␊ export function callThreadsafeFunction(callback: (...args: any[]) => any): void␊ + export function callLongThreadsafeFunction(callback: (...args: any[]) => any): void␊ export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊ export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 14120495aadf57303e28e877b473bfd953caf790..e249977761fba91f3025d912d8f4150993cd4b85 100644 GIT binary patch literal 3900 zcmV-C55w?5RzVkJr9C6locrqMqvD2Y3xBm4f66t-w z#;k&t*B^@r00000000BETkCEdw-ruX6b1aRZ@{`JGHb{e$%f%9e8FlZIkGLu(5~wQ zj?r*uxVyAwhm+xqrHvw>d4vA$pCWm}K0?poWrlO%?#glF*qY%Semp!pJU3FmOee7j z&*aa)vYh8JyZD=&iz1H5j3q3iIU}LSG8X0}U#5&iY)Ym)W?UvT1v9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vBHBTQo~@&D zJ+q9_oAi`J!tsdDAY%wz7A`%q+i_**E@%g@?AE+T=$7d>YafAJUi%;FzqTZ5IRF3* Co`A;y diff --git a/examples/napi/electron-renderer/index.js b/examples/napi/electron-renderer/index.js index e3cc9965..6e9f5c3e 100644 --- a/examples/napi/electron-renderer/index.js +++ b/examples/napi/electron-renderer/index.js @@ -1,7 +1,7 @@ const { ipcRenderer } = require('electron') -const { callThreadsafeFunction } = require('../index') +const { callLongThreadsafeFunction } = require('../index') -callThreadsafeFunction(() => {}) +callLongThreadsafeFunction(() => {}) ipcRenderer.on('ping', () => ipcRenderer.send('pong')) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 0abc1042..2ad678be 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -197,6 +197,7 @@ export function createSymbol(): symbol export function withoutAbortController(a: number, b: number): Promise export function withAbortController(a: number, b: number, signal: AbortSignal): Promise export function callThreadsafeFunction(callback: (...args: any[]) => any): void +export function callLongThreadsafeFunction(callback: (...args: any[]) => any): void export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void diff --git a/examples/napi/src/threadsafe_function.rs b/examples/napi/src/threadsafe_function.rs index 468c6e51..7e52c489 100644 --- a/examples/napi/src/threadsafe_function.rs +++ b/examples/napi/src/threadsafe_function.rs @@ -1,4 +1,4 @@ -use std::thread; +use std::{thread, time::Duration}; use napi::{ bindgen_prelude::*, @@ -19,6 +19,19 @@ pub fn call_threadsafe_function(callback: JsFunction) -> Result<()> { Ok(()) } +#[napi] +pub fn call_long_threadsafe_function(callback: JsFunction) -> Result<()> { + let tsfn: ThreadsafeFunction = + callback.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value + 1]))?; + thread::spawn(move || { + for n in 0..10 { + thread::sleep(Duration::from_millis(100)); + tsfn.call(Ok(n), ThreadsafeFunctionCallMode::NonBlocking); + } + }); + Ok(()) +} + #[napi] pub fn threadsafe_function_throw_error(cb: JsFunction) -> Result<()> { let tsfn: ThreadsafeFunction =