Skip to content

Commit fcba1f0

Browse files
committed
Handle exceptions from nested loops
1 parent 1bde7b1 commit fcba1f0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/AsyncTestCase.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,23 @@ final public function runAsyncTest(...$args)
5353

5454
$start = \microtime(true);
5555

56-
Loop::run(function () use (&$returnValue, $args) {
56+
Loop::run(function () use (&$returnValue, &$exception, $args) {
5757
try {
5858
$returnValue = yield call([$this, $this->realTestName], ...$args);
59+
} catch (\Throwable $exception) {
60+
// Also catches exception from potential nested loop.
61+
// Exception is rethrown after Loop::run().
5962
} finally {
6063
if (isset($this->timeoutId)) {
6164
Loop::cancel($this->timeoutId);
6265
}
6366
}
6467
});
6568

69+
if (isset($exception)) {
70+
throw $exception;
71+
}
72+
6673
$actualRuntime = (int) (\round(\microtime(true) - $start, self::RUNTIME_PRECISION) * 1000);
6774
if ($this->minimumRuntime > $actualRuntime) {
6875
$msg = 'Expected test to take at least %dms but instead took %dms';

test/AsyncTestCaseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public function testExpectingAnExceptionThrown(): \Generator
5757
yield $throwException();
5858
}
5959

60+
public function testNestedLoop()
61+
{
62+
$this->expectException(\Exception::class);
63+
$this->expectExceptionMessage('threw the error');
64+
65+
Loop::run(function () {
66+
throw new \Exception('threw the error');
67+
});
68+
}
69+
6070
public function testExpectingAnErrorThrown(): \Generator
6171
{
6272
$this->expectException(\Error::class);

0 commit comments

Comments
 (0)