Skip to content

Commit

Permalink
feat: cancel on termination
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MoLow authored and aduh95 committed Jul 9, 2022
1 parent bee4a6a commit 826048c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
25 changes: 19 additions & 6 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/e2225ba8e1c00995c0f8bd56e607ea7c5b463ab9/lib/internal/test_runner/harness.js
// https://github.com/nodejs/node/blob/1523a1817ed9b06fb51c0149451f9ea31bd2756e/lib/internal/test_runner/harness.js
'use strict'
const {
ArrayPrototypeForEach,
Expand Down Expand Up @@ -77,15 +77,14 @@ function setup (root) {
const rejectionHandler =
createProcessEventHandler('unhandledRejection', root)

process.on('uncaughtException', exceptionHandler)
process.on('unhandledRejection', rejectionHandler)
process.on('beforeExit', () => {
const exitHandler = () => {
root.postRun()

let passCount = 0
let failCount = 0
let skipCount = 0
let todoCount = 0
let cancelledCount = 0

for (let i = 0; i < root.subtests.length; i++) {
const test = root.subtests[i]
Expand All @@ -96,6 +95,8 @@ function setup (root) {
skipCount++
} else if (test.isTodo) {
todoCount++
} else if (test.cancelled) {
cancelledCount++
} else if (!test.passed) {
failCount++
} else {
Expand All @@ -112,6 +113,7 @@ function setup (root) {
root.reporter.diagnostic(root.indent, `tests ${root.subtests.length}`)
root.reporter.diagnostic(root.indent, `pass ${passCount}`)
root.reporter.diagnostic(root.indent, `fail ${failCount}`)
root.reporter.diagnostic(root.indent, `cancelled ${cancelledCount}`)
root.reporter.diagnostic(root.indent, `skipped ${skipCount}`)
root.reporter.diagnostic(root.indent, `todo ${todoCount}`)
root.reporter.diagnostic(root.indent, `duration_ms ${process.uptime()}`)
Expand All @@ -121,10 +123,21 @@ function setup (root) {
process.removeListener('unhandledRejection', rejectionHandler)
process.removeListener('uncaughtException', exceptionHandler)

if (failCount > 0) {
if (failCount > 0 || cancelledCount > 0) {
process.exitCode = 1
}
})
}

const terminationHandler = () => {
exitHandler()
process.exit()
}

process.on('uncaughtException', exceptionHandler)
process.on('unhandledRejection', rejectionHandler)
process.on('beforeExit', exitHandler)
process.on('SIGINT', terminationHandler)
process.on('SIGTERM', terminationHandler)

root.reporter.pipe(process.stdout)
root.reporter.version()
Expand Down
1 change: 1 addition & 0 deletions test/message/test_runner_desctibe_it.out
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ not ok 54 - invalid subtest fail
# tests 54
# pass 23
# fail 17
# cancelled 0
# skipped 9
# todo 5
# duration_ms *
3 changes: 2 additions & 1 deletion test/message/test_runner_no_refs.out
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ not ok 1 - does not keep event loop alive
1..1
# tests 1
# pass 0
# fail 1
# fail 0
# cancelled 1
# skipped 0
# todo 0
# duration_ms *
1 change: 1 addition & 0 deletions test/message/test_runner_only_tests.out
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ ok 11 - only = true, with subtests
# tests 11
# pass 1
# fail 0
# cancelled 0
# skipped 10
# todo 0
# duration_ms *
1 change: 1 addition & 0 deletions test/message/test_runner_output.out
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ not ok 57 - invalid subtest fail
# tests 57
# pass 24
# fail 18
# cancelled 0
# skipped 10
# todo 5
# duration_ms *
3 changes: 2 additions & 1 deletion test/message/test_runner_unresolved_promise.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ not ok 3 - fail
1..3
# tests 3
# pass 1
# fail 2
# fail 0
# cancelled 2
# skipped 0
# todo 0
# duration_ms *
27 changes: 23 additions & 4 deletions test/parallel/test-runner-exit-code.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// https://github.com/nodejs/node/blob/1aab13cad9c800f4121c1d35b554b78c1b17bdbd/test/parallel/test-runner-exit-code.js
// https://github.com/nodejs/node/blob/1523a1817ed9b06fb51c0149451f9ea31bd2756e/test/parallel/test-runner-exit-code.js

'use strict'

require('../common')
const common = require('../common')
const assert = require('assert')
const { spawnSync } = require('child_process')
const { promisify } = require('util')
const setTimeout = promisify(require('timers').setTimeout)

if (process.argv[2] === 'child') {
const test = require('#node:test')
Expand All @@ -13,12 +15,18 @@ if (process.argv[2] === 'child') {
test('passing test', () => {
assert.strictEqual(true, true)
})
} else {
} else if (process.argv[3] === 'fail') {
assert.strictEqual(process.argv[3], 'fail')
test('failing test', () => {
assert.strictEqual(true, false)
})
}
} else if (process.argv[3] === 'never_ends') {
assert.strictEqual(process.argv[3], 'never_ends')
test('never ending test', () => {
return setTimeout(100_000_000)
})
process.kill(process.pid, 'SIGINT')
} else assert.fail('unreachable')
} else {
let child = spawnSync(process.execPath, [__filename, 'child', 'pass'])
assert.strictEqual(child.status, 0)
Expand All @@ -27,4 +35,15 @@ if (process.argv[2] === 'child') {
child = spawnSync(process.execPath, [__filename, 'child', 'fail'])
assert.strictEqual(child.status, 1)
assert.strictEqual(child.signal, null)

child = spawnSync(process.execPath, [__filename, 'child', 'never_ends'])
assert.strictEqual(child.status, 1)
assert.strictEqual(child.signal, null)
if (common.isWindows) {
common.printSkipMessage('signals are not supported in windows')
} else {
const stdout = child.stdout.toString()
assert.match(stdout, /not ok 1 - never ending test/)
assert.match(stdout, /# cancelled 1/)
}
}

0 comments on commit 826048c

Please sign in to comment.