Skip to content

Commit

Permalink
Assert: Fix assert.timeout() bug causing a non-async test to fail
Browse files Browse the repository at this point in the history
For an async test, we normally remember the assigned timeout amount,
and the actual scheduling and enforcement of the timeout doesn't happen
until after the test callback returns.

If the test doesn't happen to be asynchronous, then the amount is
discarded and we simply move on to the next test.

Sometimes, the timeout ID stored in `config.timeout` for a previous
async test would remain and thus caused `assert.timeout()` to wrongly
think it was called twice in the same test, and tries clear and
reschedule it, when actually it is clearing nothing and scheduling
the first timeout and doing so before the test callback has returned.

Thus when the test is over and we "ignore" the timeout, QUnit then
got confused thinking it's an async test and thus waits until it
times out.

Fix this by making sure `config.timeout` is always explicitly emptied
(or re-assigned) when a timeout is cancelled or has completed.

Fixes #1539.
  • Loading branch information
Krinkle authored Jan 12, 2021
1 parent 9ab52be commit 4a2e830
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Assert {
// If a timeout has been set, clear it and reset with the new duration
if ( config.timeout ) {
clearTimeout( config.timeout );
config.timeout = null;

if ( config.timeoutHandler && this.test.timeout > 0 ) {
resetTestTimeout( this.test.timeout );
Expand Down
12 changes: 5 additions & 7 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,9 @@ export function internalStop( test ) {
}

if ( typeof timeoutDuration === "number" && timeoutDuration > 0 ) {
clearTimeout( config.timeout );
config.timeoutHandler = function( timeout ) {
return function() {
config.timeout = null;
pushFailure(
`Test took longer than ${timeout}ms; test timed out.`,
sourceFromStacktrace( 2 )
Expand All @@ -771,6 +771,7 @@ export function internalStop( test ) {
internalRecover( test );
};
};
clearTimeout( config.timeout );
config.timeout = setTimeout(
config.timeoutHandler( timeoutDuration ),
timeoutDuration
Expand Down Expand Up @@ -827,17 +828,14 @@ function internalStart( test ) {

// Add a slight delay to allow more assertions etc.
if ( setTimeout ) {
if ( config.timeout ) {
clearTimeout( config.timeout );
}
clearTimeout( config.timeout );
config.timeout = setTimeout( function() {
if ( test.semaphore > 0 ) {
return;
}

if ( config.timeout ) {
clearTimeout( config.timeout );
}
clearTimeout( config.timeout );
config.timeout = null;

begin();
} );
Expand Down

0 comments on commit 4a2e830

Please sign in to comment.