2020-08-24 17:16:35 +09:00
|
|
|
import test from 'ava'
|
|
|
|
|
|
|
|
import { napiVersion } from '../napi-version'
|
2020-07-03 01:31:50 +09:00
|
|
|
|
|
|
|
const bindings = require('../../index.node')
|
2020-06-19 21:42:18 +09:00
|
|
|
|
|
|
|
test('should get js function called from a thread', async (t) => {
|
|
|
|
let called = 0
|
|
|
|
|
2020-07-03 01:31:50 +09:00
|
|
|
if (napiVersion < 4) {
|
|
|
|
t.is(bindings.testThreadsafeFunction, undefined)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-24 19:01:55 +09:00
|
|
|
await new Promise((resolve, reject) => {
|
2020-08-24 17:16:35 +09:00
|
|
|
bindings.testThreadsafeFunction((...args: any[]) => {
|
2020-06-19 21:42:18 +09:00
|
|
|
called += 1
|
|
|
|
try {
|
2020-11-11 13:16:19 +09:00
|
|
|
if (args[1] === 0) {
|
|
|
|
t.deepEqual(args, [null, 0, 1, 2, 3])
|
|
|
|
} else {
|
|
|
|
t.deepEqual(args, [null, 3, 2, 1, 0])
|
|
|
|
}
|
2020-06-19 21:42:18 +09:00
|
|
|
} catch (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (called === 2) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-11-11 13:16:19 +09:00
|
|
|
|
|
|
|
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(() => {}))
|
|
|
|
})
|