Skip to content

Commit

Permalink
fixed hanging when setupClass is asynchronous while everything else i…
Browse files Browse the repository at this point in the history
…s synchronous (fixes #119)
  • Loading branch information
RealyUniqueName committed Dec 20, 2023
1 parent 9a5ea19 commit 6bdde6c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/utest/Async.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Async {
return resolvedInstance;
}

static inline function strPos(pos:PosInfos):String {
return pos.fileName + ':' + pos.lineNumber;
}

function new(timeoutMs:Int = 250) {
this.timeoutMs = timeoutMs;
startTime = Timer.stamp();
Expand All @@ -37,9 +41,9 @@ class Async {
public function done(?pos:PosInfos) {
if(resolved) {
if(timedOut) {
throw 'Cannot done() at ${pos.fileName}:${pos.lineNumber} because async is timed out.';
throw 'Cannot done() at ${strPos(pos)} because async is timed out.';
} else {
throw 'Cannot done() at ${pos.fileName}:${pos.lineNumber} because async is done already.';
throw 'Cannot done() at ${strPos(pos)} because async is done already.';
}
}
resolved = true;
Expand All @@ -52,10 +56,10 @@ class Async {
*/
public function setTimeout(timeoutMs:Int, ?pos:PosInfos) {
if(resolved) {
throw 'Cannot setTimeout($timeoutMs) at ${pos.fileName}:${pos.lineNumber} because async is done.';
throw 'Cannot setTimeout($timeoutMs) at ${strPos(pos)} because async is done.';
}
if(timedOut) {
throw 'Cannot setTimeout($timeoutMs) at ${pos.fileName}:${pos.lineNumber} because async is timed out.';
throw 'Cannot setTimeout($timeoutMs) at ${strPos(pos)} because async is timed out.';
}

timer.stop();
Expand Down
3 changes: 2 additions & 1 deletion src/utest/Runner.hx
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ private class ITestRunner {
if(setupAsync.timedOut) {
setupFailed(SetupError('setupClass timeout', []));
} else {
runFixtures();
if(runFixtures())
runCases();
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/utest/TestAsyncITest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ class TestAsyncITest extends Test {
Assert.isTrue(setupCallCount > 0);
}

function testAsyncSetupClassWithSyncEverythingElse_doesNotHang(async:Async) {

var currentResults = Assert.results;
Assert.results = new List();

var runner = new Runner();
runner.addCase(new TestAsyncSetupClassWithSyncEverythingElse());

runner.onComplete.add(_ -> {
Assert.results = currentResults;
Assert.pass();
async.done();
});
runner.run();
}

function teardown(async:Async) {
teardownRunning = true;

Expand Down Expand Up @@ -130,4 +146,15 @@ class TestAsyncITest extends Test {
1750
);
}
}

private class TestAsyncSetupClassWithSyncEverythingElse extends Test {
@:timeout(50)
function setupClass(async: Async): Void {
haxe.Timer.delay(() -> async.done(), 10);
}

function testSomething() {
Assert.pass();
}
}

0 comments on commit 6bdde6c

Please sign in to comment.