2020-08-24 17:16:35 +09:00
|
|
|
import { readFileSync } from 'fs'
|
|
|
|
import { join, resolve } from 'path'
|
|
|
|
|
|
|
|
import test from 'ava'
|
|
|
|
|
|
|
|
import { napiVersion } from '../napi-version'
|
2020-07-03 01:31:50 +09:00
|
|
|
|
|
|
|
const bindings = require('../../index.node')
|
|
|
|
|
2020-08-24 17:16:35 +09:00
|
|
|
let threadMod: any
|
2020-08-18 14:55:35 +09:00
|
|
|
|
|
|
|
try {
|
|
|
|
threadMod = require('worker_threads')
|
|
|
|
} catch (err) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2020-07-03 01:31:50 +09:00
|
|
|
const filepath = join(__dirname, './example.txt')
|
|
|
|
|
2020-07-08 01:59:09 +09:00
|
|
|
test('should execute future on libuv thread pool', async (t) => {
|
2020-07-03 01:31:50 +09:00
|
|
|
if (napiVersion < 4) {
|
|
|
|
t.is(bindings.uvReadFile, undefined)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const fileContent = await bindings.uvReadFile(filepath)
|
|
|
|
t.true(Buffer.isBuffer(fileContent))
|
|
|
|
t.deepEqual(readFileSync(filepath), fileContent)
|
|
|
|
})
|
2020-08-18 14:55:35 +09:00
|
|
|
|
2020-08-18 15:11:19 +09:00
|
|
|
if (threadMod && napiVersion >= 4) {
|
|
|
|
test('should execute future on libuv thread pool of "Worker"', async (t) => {
|
|
|
|
// Test in threads if current Node.js supports "worker_threads".`
|
|
|
|
|
|
|
|
const { Worker } = threadMod
|
|
|
|
const script = resolve(__dirname, './uv_worker.js')
|
|
|
|
const worker = new Worker(script)
|
|
|
|
const success = await new Promise((resolve) => {
|
2020-08-24 17:16:35 +09:00
|
|
|
worker.on('message', (success: boolean) => {
|
2020-08-18 15:11:19 +09:00
|
|
|
resolve(success)
|
|
|
|
})
|
2020-08-18 14:55:35 +09:00
|
|
|
})
|
|
|
|
|
2020-08-18 15:11:19 +09:00
|
|
|
t.is(success, true)
|
|
|
|
})
|
|
|
|
}
|