2021-01-25 12:46:58 +09:00
|
|
|
import { execSync } from 'child_process'
|
|
|
|
import { join } from 'path'
|
|
|
|
|
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-12-18 00:18:07 +09:00
|
|
|
await new Promise<void>((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(() => {}))
|
|
|
|
})
|
2020-12-18 00:18:07 +09:00
|
|
|
|
|
|
|
test('should work with napi ref', (t) => {
|
|
|
|
if (napiVersion < 4) {
|
|
|
|
t.is(bindings.testTsfnWithRef, undefined)
|
|
|
|
} else {
|
|
|
|
const obj = {
|
|
|
|
foo: Symbol(),
|
|
|
|
}
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
bindings.testTsfnWithRef((err: Error | null, returnObj: any) => {
|
|
|
|
t.is(err, null)
|
|
|
|
t.is(obj, returnObj)
|
|
|
|
resolve()
|
|
|
|
}, obj)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-01-25 12:46:58 +09:00
|
|
|
|
|
|
|
test('should be able to throw error in tsfn', (t) => {
|
|
|
|
if (napiVersion < 4) {
|
|
|
|
t.is(bindings.testThreadsafeFunction, undefined)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
execSync(`node ${join(__dirname, 'tsfn-throw.js')}`)
|
|
|
|
})
|
|
|
|
})
|
2021-03-31 22:31:37 +09:00
|
|
|
|
|
|
|
test('tsfn dua instance', (t) => {
|
|
|
|
if (napiVersion < 4) {
|
|
|
|
t.is(bindings.A, undefined)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.notThrows(() => {
|
|
|
|
execSync(`node ${join(__dirname, 'tsfn-dua-instance.js')}`)
|
|
|
|
})
|
|
|
|
})
|