2023-11-02 13:57:11 +09:00
|
|
|
import { join } from 'node:path'
|
|
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { Worker } from 'node:worker_threads'
|
2021-12-22 00:24:07 +09:00
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
import test from 'ava'
|
2021-12-22 00:24:07 +09:00
|
|
|
|
2024-02-26 16:46:07 +09:00
|
|
|
import { Animal, Kind, DEFAULT_COST } from '../index.cjs'
|
2023-11-02 13:57:11 +09:00
|
|
|
|
|
|
|
const __dirname = join(fileURLToPath(import.meta.url), '..')
|
2021-12-22 00:24:07 +09:00
|
|
|
|
2023-04-17 00:59:15 +09:00
|
|
|
const t =
|
2023-11-03 13:09:06 +09:00
|
|
|
// aarch64-unknown-linux-gnu is extremely slow in CI, skip it or it will timeout
|
2023-11-07 02:46:43 +09:00
|
|
|
process.arch === 'arm64' && process.platform === 'linux' ? test.skip : test
|
2023-04-17 00:59:15 +09:00
|
|
|
|
2023-11-07 02:46:43 +09:00
|
|
|
const concurrency = process.env.WASI_TEST
|
|
|
|
? 1
|
|
|
|
: process.platform === 'win32' ||
|
2023-11-21 00:07:43 +09:00
|
|
|
process.platform === 'darwin' ||
|
|
|
|
(process.platform === 'linux' &&
|
|
|
|
process.arch === 'x64' &&
|
|
|
|
// @ts-expect-error
|
|
|
|
process?.report?.getReport()?.header?.glibcVersionRuntime)
|
|
|
|
? 50
|
2024-04-10 16:43:05 +09:00
|
|
|
: 3
|
2023-11-02 01:44:08 +09:00
|
|
|
|
2023-04-17 00:59:15 +09:00
|
|
|
t('should be able to require in worker thread', async (t) => {
|
2022-03-05 15:14:32 +09:00
|
|
|
await Promise.all(
|
2023-11-02 01:44:08 +09:00
|
|
|
Array.from({ length: concurrency }).map(() => {
|
2023-09-20 17:18:01 +09:00
|
|
|
const w = new Worker(join(__dirname, 'worker.cjs'), {
|
2023-11-07 02:46:43 +09:00
|
|
|
execArgv: ['--experimental-wasi-unstable-preview1'],
|
|
|
|
env: process.env,
|
2023-09-20 17:18:01 +09:00
|
|
|
})
|
2023-11-02 13:57:11 +09:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2023-03-05 17:51:06 +09:00
|
|
|
w.postMessage({ type: 'require' })
|
2022-03-05 15:14:32 +09:00
|
|
|
w.on('message', (msg) => {
|
|
|
|
t.is(msg, Animal.withKind(Kind.Cat).whoami() + DEFAULT_COST)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
w.on('error', (err) => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(() => w.terminate())
|
|
|
|
.then(() => {
|
|
|
|
t.pass()
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
)
|
2021-12-22 00:24:07 +09:00
|
|
|
})
|
2023-03-05 17:51:06 +09:00
|
|
|
|
2023-04-17 00:59:15 +09:00
|
|
|
t('custom GC works on worker_threads', async (t) => {
|
2023-03-05 17:51:06 +09:00
|
|
|
await Promise.all(
|
2023-11-02 01:44:08 +09:00
|
|
|
Array.from({ length: concurrency }).map(() =>
|
2023-03-05 17:51:06 +09:00
|
|
|
Promise.all([
|
2023-11-02 13:57:11 +09:00
|
|
|
new Promise<Worker>((resolve, reject) => {
|
2023-09-20 17:18:01 +09:00
|
|
|
const w = new Worker(join(__dirname, 'worker.cjs'), {
|
2023-11-07 02:46:43 +09:00
|
|
|
execArgv: ['--experimental-wasi-unstable-preview1'],
|
|
|
|
env: process.env,
|
2023-09-20 17:18:01 +09:00
|
|
|
})
|
2023-03-05 17:51:06 +09:00
|
|
|
w.postMessage({
|
|
|
|
type: 'async:buffer',
|
|
|
|
})
|
|
|
|
w.on('message', (msg) => {
|
|
|
|
t.is(msg, 'done')
|
|
|
|
resolve(w)
|
|
|
|
})
|
|
|
|
w.on('error', (err) => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
}).then((w) => {
|
|
|
|
return w.terminate()
|
|
|
|
}),
|
2023-11-02 13:57:11 +09:00
|
|
|
new Promise<Worker>((resolve, reject) => {
|
2023-09-20 17:18:01 +09:00
|
|
|
const w = new Worker(join(__dirname, 'worker.cjs'), {
|
2023-11-02 13:57:11 +09:00
|
|
|
execArgv: [],
|
2023-09-20 17:18:01 +09:00
|
|
|
})
|
2023-03-05 17:51:06 +09:00
|
|
|
w.postMessage({
|
|
|
|
type: 'async:arraybuffer',
|
|
|
|
})
|
|
|
|
w.on('message', (msg) => {
|
|
|
|
t.is(msg, 'done')
|
|
|
|
resolve(w)
|
|
|
|
})
|
|
|
|
w.on('error', (err) => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
}).then((w) => {
|
|
|
|
return w.terminate()
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})
|
2023-04-15 16:37:01 +09:00
|
|
|
|
2023-04-17 00:59:15 +09:00
|
|
|
t('should be able to new Class in worker thread concurrently', async (t) => {
|
2023-04-15 16:37:01 +09:00
|
|
|
await Promise.all(
|
2023-11-02 01:44:08 +09:00
|
|
|
Array.from({ length: concurrency }).map(() => {
|
2023-09-20 17:18:01 +09:00
|
|
|
const w = new Worker(join(__dirname, 'worker.cjs'), {
|
2023-11-07 02:46:43 +09:00
|
|
|
execArgv: ['--experimental-wasi-unstable-preview1'],
|
|
|
|
env: process.env,
|
2023-09-20 17:18:01 +09:00
|
|
|
})
|
2023-11-02 13:57:11 +09:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2023-04-15 16:37:01 +09:00
|
|
|
w.postMessage({ type: 'constructor' })
|
|
|
|
w.on('message', (msg) => {
|
|
|
|
t.is(msg, 'Ellie')
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
w.on('error', (err) => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(() => w.terminate())
|
|
|
|
.then(() => {
|
|
|
|
t.pass()
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|