Skip to content

Semaphore imbalance when returned promise resolves when test timed out. #1375

Closed

Description

@rwjblue

Tell us about your runtime:

  • QUnit version:2.9.1
  • What environment are you running QUnit in? (e.g., browser, Node): Browser & Node
  • How are you running QUnit? (e.g., script, testem, Grunt): qunit Node CLI / testem

What are you trying to do?

A test returning a promise (or an async) that runs longer than the allowed timeout period, causes an internal test semaphore imbalance if the returned promise ever settles.

Code that reproduces the problem:

QUnit.module('group of tests', function(hooks) {
  QUnit.test('one', function(assert) {
	assert.timeout(10);

    return new Promise(resolve => setTimeout(resolve, 20));
  });

  QUnit.test('two', function(assert) {
   	return new Promise(resolve => setTimeout(resolve, 20))
      .then(() => {
        assert.ok(true, 'all is well');
      });
  });
});

CodePen Demo Here

If you have any relevant configuration information, please include that here:

What did you expect to happen?

Test one should fail (due to timeout), and test two should pass.

What actually happened?

Test one fails properly (due to timeout), however test two also fails with the following message:

Tried to restart test while already started (test's semaphore was 0 already)

Activity

  1. rwjblue commented on Feb 20, 2019

    @rwjblue
    ContributorAuthor

    I did some debugging with @scalvert for this issue on Friday and we believe we have a good understanding of what is going on and how to fix it (so hopefully we'll have a PR inbound soon), but I wanted to create the issue for discussion also.


    The basic issue is that Test.prototype.resolvePromise here always calls resume (in both reject and fulfilled scenarios) which attempts to decrements the individual tests semaphore:

    qunit/src/test.js

    Lines 523 to 539 in 766db37

    then.call(
    promise,
    function() { resume(); },
    function( error ) {
    message = "Promise rejected " +
    ( !phase ? "during" : phase.replace( /Each$/, "" ) ) +
    " \"" + test.testName + "\": " +
    ( ( error && error.message ) || error );
    test.pushFailure( message, extractStacktrace( error, 0 ) );
    // Else next test will carry the responsibility
    saveGlobal();
    // Unblock
    internalRecover( test );
    }
    );

    After a test timeout (see this for what that does) we reset the tests semaphore to 0, but we don't also prevent resume from running when the promise finally does resolve.

    When the promise finally resolves, the test is "long gone" but resume is still called which decrements the tests semaphore (now to -1) and calls internalStart. When internalStart is called and the passed in test.semaphore < 0 we call pushFailure to log the semaphore imbalance.

    Unfortunately, since pushFailure always logs failures to the QUnit.config.current test (or creates a new global failure) this pushFailure about semaphore imbalance causes a completely unrelated test to fail (the test that happens to be running) not the test that actually had the imbalance.

  2. trentmwillis commented on Feb 21, 2019

    @trentmwillis
    Member

    Thanks y'all for the detailed report and PR to fix. I'll cut a new release tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions