-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26ebae1
commit 331151c
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default function run() { | ||
process.stdout.write('Worker message') | ||
process.stderr.write('Worker error') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { stripVTControlCharacters } from 'node:util' | ||
import { Tinypool } from 'tinypool' | ||
|
||
const runtimes = ['worker_threads', 'child_process'] as const | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
|
||
test.each(runtimes)( | ||
"worker's stdout and stderr are piped to main thread when { runtime: '%s' }", | ||
async (runtime) => { | ||
const pool = createPool({ | ||
runtime, | ||
minThreads: 1, | ||
maxThreads: 1, | ||
}) | ||
|
||
const getStdout = captureStandardStream('stdout') | ||
const getStderr = captureStandardStream('stderr') | ||
|
||
await pool.run({}) | ||
|
||
const stdout = getStdout() | ||
const stderr = getStderr() | ||
|
||
expect(stdout).toMatch('Worker message') | ||
|
||
expect(stderr).toMatch('Worker error') | ||
} | ||
) | ||
|
||
function createPool(options: Partial<Tinypool['options']>) { | ||
const pool = new Tinypool({ | ||
filename: path.resolve(__dirname, 'fixtures/stdio.mjs'), | ||
minThreads: 1, | ||
maxThreads: 1, | ||
...options, | ||
}) | ||
|
||
return pool | ||
} | ||
|
||
function captureStandardStream(type: 'stdout' | 'stderr') { | ||
const spy = vi.fn() | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const original = process[type].write | ||
process[type].write = spy | ||
|
||
return function collect() { | ||
process[type].write = original | ||
return stripVTControlCharacters( | ||
spy.mock.calls.map((call) => call[0]).join('') | ||
) | ||
} | ||
} |