Skip to content

Commit 0951281

Browse files
MoLowaduh95
authored andcommitted
feat: cancel on termination
PR-URL: nodejs/node#43549 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Nitzan Uziely <linkgoron@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 2839aaf commit 0951281

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

lib/internal/test_runner/harness.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/e2225ba8e1c00995c0f8bd56e607ea7c5b463ab9/lib/internal/test_runner/harness.js
1+
// https://github.com/nodejs/node/blob/1523a1817ed9b06fb51c0149451f9ea31bd2756e/lib/internal/test_runner/harness.js
22
'use strict'
33
const {
44
ArrayPrototypeForEach,
@@ -75,15 +75,14 @@ function setup (root) {
7575
const rejectionHandler =
7676
createProcessEventHandler('unhandledRejection', root)
7777

78-
process.on('uncaughtException', exceptionHandler)
79-
process.on('unhandledRejection', rejectionHandler)
80-
process.on('beforeExit', () => {
78+
const exitHandler = () => {
8179
root.postRun()
8280

8381
let passCount = 0
8482
let failCount = 0
8583
let skipCount = 0
8684
let todoCount = 0
85+
let cancelledCount = 0
8786

8887
for (let i = 0; i < root.subtests.length; i++) {
8988
const test = root.subtests[i]
@@ -94,6 +93,8 @@ function setup (root) {
9493
skipCount++
9594
} else if (test.isTodo) {
9695
todoCount++
96+
} else if (test.cancelled) {
97+
cancelledCount++
9798
} else if (!test.passed) {
9899
failCount++
99100
} else {
@@ -110,6 +111,7 @@ function setup (root) {
110111
root.reporter.diagnostic(root.indent, `tests ${root.subtests.length}`)
111112
root.reporter.diagnostic(root.indent, `pass ${passCount}`)
112113
root.reporter.diagnostic(root.indent, `fail ${failCount}`)
114+
root.reporter.diagnostic(root.indent, `cancelled ${cancelledCount}`)
113115
root.reporter.diagnostic(root.indent, `skipped ${skipCount}`)
114116
root.reporter.diagnostic(root.indent, `todo ${todoCount}`)
115117
root.reporter.diagnostic(root.indent, `duration_ms ${process.uptime()}`)
@@ -119,10 +121,21 @@ function setup (root) {
119121
process.removeListener('unhandledRejection', rejectionHandler)
120122
process.removeListener('uncaughtException', exceptionHandler)
121123

122-
if (failCount > 0) {
124+
if (failCount > 0 || cancelledCount > 0) {
123125
process.exitCode = 1
124126
}
125-
})
127+
}
128+
129+
const terminationHandler = () => {
130+
exitHandler()
131+
process.exit()
132+
}
133+
134+
process.on('uncaughtException', exceptionHandler)
135+
process.on('unhandledRejection', rejectionHandler)
136+
process.on('beforeExit', exitHandler)
137+
process.on('SIGINT', terminationHandler)
138+
process.on('SIGTERM', terminationHandler)
126139

127140
root.reporter.pipe(process.stdout)
128141
root.reporter.version()

test/message/test_runner_desctibe_it.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ not ok 54 - invalid subtest fail
515515
# tests 54
516516
# pass 23
517517
# fail 17
518+
# cancelled 0
518519
# skipped 9
519520
# todo 5
520521
# duration_ms *

test/message/test_runner_no_refs.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ not ok 1 - does not keep event loop alive
2323
1..1
2424
# tests 1
2525
# pass 0
26-
# fail 1
26+
# fail 0
27+
# cancelled 1
2728
# skipped 0
2829
# todo 0
2930
# duration_ms *

test/message/test_runner_only_tests.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ ok 11 - only = true, with subtests
120120
# tests 11
121121
# pass 1
122122
# fail 0
123+
# cancelled 0
123124
# skipped 10
124125
# todo 0
125126
# duration_ms *

test/message/test_runner_output.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ not ok 57 - invalid subtest fail
582582
# tests 57
583583
# pass 24
584584
# fail 18
585+
# cancelled 0
585586
# skipped 10
586587
# todo 5
587588
# duration_ms *

test/message/test_runner_unresolved_promise.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ not ok 3 - fail
2727
1..3
2828
# tests 3
2929
# pass 1
30-
# fail 2
30+
# fail 0
31+
# cancelled 2
3132
# skipped 0
3233
# todo 0
3334
# duration_ms *
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// https://github.com/nodejs/node/blob/1aab13cad9c800f4121c1d35b554b78c1b17bdbd/test/parallel/test-runner-exit-code.js
1+
// https://github.com/nodejs/node/blob/1523a1817ed9b06fb51c0149451f9ea31bd2756e/test/parallel/test-runner-exit-code.js
22

33
'use strict'
44

5-
require('../common')
5+
const common = require('../common')
66
const assert = require('assert')
77
const { spawnSync } = require('child_process')
8+
const { promisify } = require('util')
9+
const setTimeout = promisify(require('timers').setTimeout)
810

911
if (process.argv[2] === 'child') {
1012
const test = require('#node:test')
@@ -13,12 +15,18 @@ if (process.argv[2] === 'child') {
1315
test('passing test', () => {
1416
assert.strictEqual(true, true)
1517
})
16-
} else {
18+
} else if (process.argv[3] === 'fail') {
1719
assert.strictEqual(process.argv[3], 'fail')
1820
test('failing test', () => {
1921
assert.strictEqual(true, false)
2022
})
21-
}
23+
} else if (process.argv[3] === 'never_ends') {
24+
assert.strictEqual(process.argv[3], 'never_ends')
25+
test('never ending test', () => {
26+
return setTimeout(100_000_000)
27+
})
28+
process.kill(process.pid, 'SIGINT')
29+
} else assert.fail('unreachable')
2230
} else {
2331
let child = spawnSync(process.execPath, [__filename, 'child', 'pass'])
2432
assert.strictEqual(child.status, 0)
@@ -27,4 +35,15 @@ if (process.argv[2] === 'child') {
2735
child = spawnSync(process.execPath, [__filename, 'child', 'fail'])
2836
assert.strictEqual(child.status, 1)
2937
assert.strictEqual(child.signal, null)
38+
39+
child = spawnSync(process.execPath, [__filename, 'child', 'never_ends'])
40+
assert.strictEqual(child.status, 1)
41+
assert.strictEqual(child.signal, null)
42+
if (common.isWindows) {
43+
common.printSkipMessage('signals are not supported in windows')
44+
} else {
45+
const stdout = child.stdout.toString()
46+
assert.match(stdout, /not ok 1 - never ending test/)
47+
assert.match(stdout, /# cancelled 1/)
48+
}
3049
}

0 commit comments

Comments
 (0)