Skip to content

Commit

Permalink
Core: Better isolate async abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Jul 8, 2016
1 parent 1743a14 commit 434e8c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
9 changes: 3 additions & 6 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ QUnit.assert = Assert.prototype = {
}
},

// Increment this Test's semaphore counter, then return a function that
// decrements that counter a maximum of once.
// Put a hold on processing and return a function that will release it a maximum of once.
async: function( count ) {
var test = this.test,
popped = false,
Expand All @@ -26,9 +25,8 @@ QUnit.assert = Assert.prototype = {
acceptCallCount = 1;
}

test.semaphore += 1;
test.usedAsync = true;
pauseProcessing( test );
internalStop( test );

return function done() {

Expand All @@ -42,9 +40,8 @@ QUnit.assert = Assert.prototype = {
return;
}

test.semaphore -= 1;
popped = true;
resumeProcessing( test );
internalStart( test );
};
},

Expand Down
27 changes: 18 additions & 9 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ extend( QUnit, {
);
}

resumeProcessing();
scheduleBegin();
},

config: config,
Expand All @@ -138,7 +138,7 @@ extend( QUnit, {
config.blocking = false;

if ( config.autostart ) {
resumeProcessing();
scheduleBegin();
}
},

Expand All @@ -150,6 +150,20 @@ extend( QUnit, {

registerLoggingCallbacks( QUnit );

function scheduleBegin() {

runStarted = true;

// Add a slight delay to allow definition of more modules and tests.
if ( defined.setTimeout ) {
setTimeout( function() {
begin();
}, 13 );
} else {
begin();
}
}

function begin() {
var i, l,
modulesLog = [];
Expand Down Expand Up @@ -225,24 +239,19 @@ function pauseProcessing( test ) {
}

function resumeProcessing( test ) {
runStarted = true;

// A slight delay to allow this iteration of the event loop to finish (more assertions, etc.)
if ( defined.setTimeout ) {
setTimeout( function() {
var current = test || config.current;
if ( current && ( current.semaphore > 0 || current.resumed ) ) {
if ( test.semaphore > 0 || test.resumed ) {
return;
}
test.resumed = true;

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

if ( current ) {
current.resumed = true;
}

begin();
}, 13 );
} else {
Expand Down
4 changes: 0 additions & 4 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,16 +627,12 @@ function only( testName, callback ) {
}

function internalStop( test ) {

// If a test is running, adjust its semaphore
test.semaphore += 1;

pauseProcessing( test );
}

function internalStart( test ) {

// If a test is running, adjust its semaphore
test.semaphore -= 1;

// If semaphore is non-numeric, throw error
Expand Down

0 comments on commit 434e8c3

Please # to comment.