Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ abstract class AsyncTestCase extends PHPUnitTestCase
/** @var \Generator|null */
private $generator;

/** @var int|null */
private $timeout;

final protected function runTest()
{
parent::setName('runAsyncTest');
return parent::runTest();
}

/** @internal */
final public function runAsyncTest(...$args)
{
Expand All @@ -61,6 +70,28 @@ final public function runAsyncTest(...$args)
$invoked = true;
$exception = $error;
$returnValue = $value;

if ($this->timeout === null) {
Loop::unreference(Loop::defer(function () use ($exception) {
Loop::stop();

if ($exception) {
$this->fail(\sprintf(
'An exception was thrown from the test method or promise returned from test method failed,'
. ' but the event loop continued to run; set a timeout with %s::setTimeout() to allow the loop to continue'
. ' to run for a given period of time; Exception thrown: %s',
self::class,
$exception
));
}

$this->fail(\sprintf(
'The event loop continued to run after the test method completed or the promise returned resolved;'
. ' set a timeout with %s::setTimeout() to allow the loop to continue to run for a given period of time',
self::class
));
}));
}
});
});

Expand Down Expand Up @@ -124,12 +155,6 @@ private function runAsyncTestCycle(array $args): \Generator
return $returnValue;
}

final protected function runTest()
{
parent::setName('runAsyncTest');
return parent::runTest();
}

/**
* Called before each test. Similar to {@see TestCase::setUp()}, except the method may return a promise or
* coroutine (@see \Amp\call()} that will be awaited before executing the test.
Expand Down Expand Up @@ -169,6 +194,8 @@ final protected function setMinimumRuntime(int $runtime)
*/
final protected function setTimeout(int $timeout)
{
$this->timeout = $timeout;

$this->timeoutId = Loop::delay($timeout, function () use ($timeout) {
Loop::stop();
Loop::setErrorHandler(null);
Expand Down
18 changes: 18 additions & 0 deletions test/AsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function testSetMinimumRunTime(): \Generator
public function testSetMinimumRunTimeWithWatchersOnly()
{
$this->setMinimumRuntime(100);
$this->setTimeout(200);
Loop::delay(100, $this->createCallback(1));
}

Expand All @@ -182,4 +183,21 @@ public function testCreateCallback()

$this->assertSame(2, $mock(1));
}

public function testIssue8()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Exception thrown: ');

Loop::repeat(1000, static function () {});
throw new \Exception('Test exception');
}

public function testLoopContinuesToRunAfterResolve()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('event loop continued to run');

Loop::repeat(1000, static function () {});
}
}