test(napi): update test case for checking electron renderer crash (#1547)

(cherry picked from commit d22598dbb1082de8ac712de954cd5616c838a48d)
This commit is contained in:
Bo 2023-03-29 12:53:57 +08:00 committed by GitHub
parent d8cfcfdfda
commit 0a0aa36c28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 3 deletions

View file

@ -207,6 +207,7 @@ Generated by [AVA](https://avajs.dev).
export function withoutAbortController(a: number, b: number): Promise<number>
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>
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␊

View file

@ -1,7 +1,7 @@
const { ipcRenderer } = require('electron')
const { callThreadsafeFunction } = require('../index')
const { callLongThreadsafeFunction } = require('../index')
callThreadsafeFunction(() => {})
callLongThreadsafeFunction(() => {})
ipcRenderer.on('ping', () => ipcRenderer.send('pong'))

View file

@ -197,6 +197,7 @@ export function createSymbol(): symbol
export function withoutAbortController(a: number, b: number): Promise<number>
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>
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

View file

@ -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<u32, ErrorStrategy::CalleeHandled> =
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<bool, ErrorStrategy::CalleeHandled> =