napi-rs/test_module/__test__/napi4/threadsafe_function.spec.ts

57 lines
1.4 KiB
TypeScript

import test from 'ava'
import { napiVersion } from '../napi-version'
const bindings = require('../../index.node')
test('should get js function called from a thread', async (t) => {
let called = 0
if (napiVersion < 4) {
t.is(bindings.testThreadsafeFunction, undefined)
return
}
await new Promise((resolve, reject) => {
bindings.testThreadsafeFunction((...args: any[]) => {
called += 1
try {
if (args[1] === 0) {
t.deepEqual(args, [null, 0, 1, 2, 3])
} else {
t.deepEqual(args, [null, 3, 2, 1, 0])
}
} catch (err) {
reject(err)
}
if (called === 2) {
resolve()
}
})
})
})
test('should be able to abort tsfn', (t) => {
if (napiVersion < 4) {
t.is(bindings.testAbortThreadsafeFunction, undefined)
return
}
t.true(bindings.testAbortThreadsafeFunction(() => {}))
})
test('should be able to abort independent tsfn', (t) => {
if (napiVersion < 4) {
t.is(bindings.testAbortIndependentThreadsafeFunction, undefined)
return
}
t.false(bindings.testAbortIndependentThreadsafeFunction(() => {}))
})
test('should return Closing while calling aborted tsfn', (t) => {
if (napiVersion < 4) {
t.is(bindings.testCallAbortedThreadsafeFunction, undefined)
return
}
t.notThrows(() => bindings.testCallAbortedThreadsafeFunction(() => {}))
})