2
2
const common = require ( '../common' ) ;
3
3
const fixtures = require ( '../common/fixtures' ) ;
4
4
const assert = require ( 'assert' ) ;
5
- const { spawnSync } = require ( 'child_process' ) ;
6
- const { setTimeout } = require ( 'timers/promises' ) ;
5
+ const { spawnSync, spawn } = require ( 'child_process' ) ;
6
+ const { once } = require ( 'events' ) ;
7
+ const { finished } = require ( 'stream/promises' ) ;
8
+
9
+ async function runAndKill ( file ) {
10
+ if ( common . isWindows ) {
11
+ common . printSkipMessage ( `signals are not supported in windows, skipping ${ file } ` ) ;
12
+ return ;
13
+ }
14
+ let stdout = '' ;
15
+ const child = spawn ( process . execPath , [ '--test' , file ] ) ;
16
+ child . stdout . setEncoding ( 'utf8' ) ;
17
+ child . stdout . on ( 'data' , ( chunk ) => {
18
+ if ( ! stdout . length ) child . kill ( 'SIGINT' ) ;
19
+ stdout += chunk ;
20
+ } ) ;
21
+ const [ code , signal ] = await once ( child , 'exit' ) ;
22
+ await finished ( child . stdout ) ;
23
+ assert . match ( stdout , / n o t o k 1 / ) ;
24
+ assert . match ( stdout , / # c a n c e l l e d 1 \n / ) ;
25
+ assert . strictEqual ( signal , null ) ;
26
+ assert . strictEqual ( code , 1 ) ;
27
+ }
7
28
8
29
if ( process . argv [ 2 ] === 'child' ) {
9
30
const test = require ( 'node:test' ) ;
@@ -17,12 +38,6 @@ if (process.argv[2] === 'child') {
17
38
test ( 'failing test' , ( ) => {
18
39
assert . strictEqual ( true , false ) ;
19
40
} ) ;
20
- } else if ( process . argv [ 3 ] === 'never_ends' ) {
21
- assert . strictEqual ( process . argv [ 3 ] , 'never_ends' ) ;
22
- test ( 'never ending test' , ( ) => {
23
- return setTimeout ( 100_000_000 ) ;
24
- } ) ;
25
- process . kill ( process . pid , 'SIGINT' ) ;
26
41
} else assert . fail ( 'unreachable' ) ;
27
42
} else {
28
43
let child = spawnSync ( process . execPath , [ __filename , 'child' , 'pass' ] ) ;
@@ -37,14 +52,6 @@ if (process.argv[2] === 'child') {
37
52
assert . strictEqual ( child . status , 1 ) ;
38
53
assert . strictEqual ( child . signal , null ) ;
39
54
40
- child = spawnSync ( process . execPath , [ __filename , 'child' , 'never_ends' ] ) ;
41
- assert . strictEqual ( child . status , 1 ) ;
42
- assert . strictEqual ( child . signal , null ) ;
43
- if ( common . isWindows ) {
44
- common . printSkipMessage ( 'signals are not supported in windows' ) ;
45
- } else {
46
- const stdout = child . stdout . toString ( ) ;
47
- assert . match ( stdout , / n o t o k 1 - n e v e r e n d i n g t e s t / ) ;
48
- assert . match ( stdout , / # c a n c e l l e d 1 / ) ;
49
- }
55
+ runAndKill ( fixtures . path ( 'test-runner' , 'never_ending_sync.js' ) ) . then ( common . mustCall ( ) ) ;
56
+ runAndKill ( fixtures . path ( 'test-runner' , 'never_ending_async.js' ) ) . then ( common . mustCall ( ) ) ;
50
57
}
0 commit comments