Skip to content

Commit 826048c

Browse files
MoLowaduh95
authored andcommitted
feat: cancel on termination
PR-URL: nodejs/node#43549 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Nitzan Uziely <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent bee4a6a commit 826048c

7 files changed

+49
-12
lines changed

Diff for: lib/internal/test_runner/harness.js

+19-6
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,
@@ -77,15 +77,14 @@ function setup (root) {
7777
const rejectionHandler =
7878
createProcessEventHandler('unhandledRejection', root)
7979

80-
process.on('uncaughtException', exceptionHandler)
81-
process.on('unhandledRejection', rejectionHandler)
82-
process.on('beforeExit', () => {
80+
const exitHandler = () => {
8381
root.postRun()
8482

8583
let passCount = 0
8684
let failCount = 0
8785
let skipCount = 0
8886
let todoCount = 0
87+
let cancelledCount = 0
8988

9089
for (let i = 0; i < root.subtests.length; i++) {
9190
const test = root.subtests[i]
@@ -96,6 +95,8 @@ function setup (root) {
9695
skipCount++
9796
} else if (test.isTodo) {
9897
todoCount++
98+
} else if (test.cancelled) {
99+
cancelledCount++
99100
} else if (!test.passed) {
100101
failCount++
101102
} else {
@@ -112,6 +113,7 @@ function setup (root) {
112113
root.reporter.diagnostic(root.indent, `tests ${root.subtests.length}`)
113114
root.reporter.diagnostic(root.indent, `pass ${passCount}`)
114115
root.reporter.diagnostic(root.indent, `fail ${failCount}`)
116+
root.reporter.diagnostic(root.indent, `cancelled ${cancelledCount}`)
115117
root.reporter.diagnostic(root.indent, `skipped ${skipCount}`)
116118
root.reporter.diagnostic(root.indent, `todo ${todoCount}`)
117119
root.reporter.diagnostic(root.indent, `duration_ms ${process.uptime()}`)
@@ -121,10 +123,21 @@ function setup (root) {
121123
process.removeListener('unhandledRejection', rejectionHandler)
122124
process.removeListener('uncaughtException', exceptionHandler)
123125

124-
if (failCount > 0) {
126+
if (failCount > 0 || cancelledCount > 0) {
125127
process.exitCode = 1
126128
}
127-
})
129+
}
130+
131+
const terminationHandler = () => {
132+
exitHandler()
133+
process.exit()
134+
}
135+
136+
process.on('uncaughtException', exceptionHandler)
137+
process.on('unhandledRejection', rejectionHandler)
138+
process.on('beforeExit', exitHandler)
139+
process.on('SIGINT', terminationHandler)
140+
process.on('SIGTERM', terminationHandler)
128141

129142
root.reporter.pipe(process.stdout)
130143
root.reporter.version()

Diff for: test/message/test_runner_desctibe_it.out

+1
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 *

Diff for: test/message/test_runner_no_refs.out

+2-1
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 *

Diff for: test/message/test_runner_only_tests.out

+1
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 *

Diff for: test/message/test_runner_output.out

+1
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 *

Diff for: test/message/test_runner_unresolved_promise.out

+2-1
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 *

Diff for: test/parallel/test-runner-exit-code.js

+23-4
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)