-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
test_runner: support timeout for tests #43505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
928179a
5698169
391d302
79c33fa
d9acf5e
554d2b2
88ab08e
d02fc1f
0e0a64c
402fd56
d327bff
6c58a50
586572a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const { | |
PromiseResolve, | ||
ReflectApply, | ||
SafeMap, | ||
PromiseRace, | ||
} = primordials; | ||
const { AsyncResource } = require('async_hooks'); | ||
const { | ||
|
@@ -26,20 +27,39 @@ const { | |
} = require('internal/util'); | ||
const { isPromise } = require('internal/util/types'); | ||
const { isUint32 } = require('internal/validators'); | ||
const { setTimeout } = require('timers/promises'); | ||
const { cpus } = require('os'); | ||
const { bigint: hrtime } = process.hrtime; | ||
const kCallbackAndPromisePresent = 'callbackAndPromisePresent'; | ||
const kCancelledByParent = 'cancelledByParent'; | ||
const kParentAlreadyFinished = 'parentAlreadyFinished'; | ||
const kSubtestsFailed = 'subtestsFailed'; | ||
const kTestCodeFailure = 'testCodeFailure'; | ||
const kTestTimeoutFailure = 'testTimeoutFailure'; | ||
const kDefaultIndent = ' '; | ||
const kDefaultTimeout = null; | ||
const noop = FunctionPrototype; | ||
const isTestRunner = getOptionValue('--test'); | ||
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only'); | ||
// TODO(cjihrig): Use uv_available_parallelism() once it lands. | ||
const rootConcurrency = isTestRunner ? cpus().length : 1; | ||
|
||
|
||
function testTimeout(promise, timeout) { | ||
if (timeout === kDefaultTimeout) { | ||
return promise; | ||
} | ||
return PromiseRace([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really a fan at all of using
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah see #43554 |
||
promise, | ||
setTimeout(timeout, null, { ref: false }).then(() => { | ||
throw new ERR_TEST_FAILURE( | ||
`test timed out after ${timeout}ms`, | ||
kTestTimeoutFailure | ||
); | ||
}), | ||
]); | ||
} | ||
|
||
class TestContext { | ||
#test; | ||
|
||
|
@@ -76,7 +96,7 @@ class Test extends AsyncResource { | |
super('Test'); | ||
|
||
let { fn, name, parent, skip } = options; | ||
const { concurrency, only, todo } = options; | ||
const { concurrency, only, timeout, todo } = options; | ||
|
||
if (typeof fn !== 'function') { | ||
fn = noop; | ||
|
@@ -98,6 +118,7 @@ class Test extends AsyncResource { | |
this.reporter = new TapStream(); | ||
this.runOnlySubtests = this.only; | ||
this.testNumber = 0; | ||
this.timeout = kDefaultTimeout; | ||
} else { | ||
const indent = parent.parent === null ? parent.indent : | ||
parent.indent + parent.indentString; | ||
|
@@ -109,12 +130,17 @@ class Test extends AsyncResource { | |
this.reporter = parent.reporter; | ||
this.runOnlySubtests = !this.only; | ||
this.testNumber = parent.subtests.length + 1; | ||
this.timeout = parent.timeout; | ||
} | ||
|
||
if (isUint32(concurrency) && concurrency !== 0) { | ||
this.concurrency = concurrency; | ||
} | ||
|
||
if (isUint32(timeout)) { | ||
this.timeout = timeout; | ||
} | ||
|
||
if (testOnlyFlag && !this.only) { | ||
skip = '\'only\' option not set'; | ||
} | ||
|
@@ -329,13 +355,13 @@ class Test extends AsyncResource { | |
'passed a callback but also returned a Promise', | ||
kCallbackAndPromisePresent | ||
)); | ||
await ret; | ||
await testTimeout(ret, this.timeout); | ||
} else { | ||
await promise; | ||
await testTimeout(promise, this.timeout); | ||
} | ||
} else { | ||
// This test is synchronous or using Promises. | ||
await ReflectApply(this.runInAsyncScope, this, args); | ||
await testTimeout(ReflectApply(this.runInAsyncScope, this, args), this.timeout); | ||
} | ||
|
||
this.pass(); | ||
|
@@ -480,10 +506,10 @@ class Suite extends Test { | |
this.parent.activeSubtests++; | ||
this.startTime = hrtime(); | ||
const subtests = this.skipped || this.error ? [] : this.subtests; | ||
await ArrayPrototypeReduce(subtests, async (prev, subtest) => { | ||
await testTimeout(ArrayPrototypeReduce(subtests, async (prev, subtest) => { | ||
await prev; | ||
await subtest.run(); | ||
}, PromiseResolve()); | ||
}, PromiseResolve()), this.timeout); | ||
this.pass(); | ||
this.postRun(); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.