Skip to content

Commit

Permalink
Add support for a minimum runtime (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspray authored and trowski committed Feb 12, 2019
1 parent 1fcc580 commit a72b1fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
abstract class AsyncTestCase extends PHPUnitTestCase
{
const RUNTIME_PRECISION = 2;

private $timeoutId;

private $realTestName;

private $minimumRuntime;

public function setName($name)
{
parent::setName($name);
Expand All @@ -38,7 +41,15 @@ final public function runAsyncTest(...$args)
try {
Loop::run(function () use (&$returnValue, $args) {
try {
$start = \microtime(true);
$returnValue = yield call([$this, $this->realTestName], ...$args);
$actualRuntime = (int) (\round(\microtime(true) - $start, self::RUNTIME_PRECISION) * 1000);
if ($this->minimumRuntime) {
if ($this->minimumRuntime > $actualRuntime) {
$msg = 'Expected test to take at least %dms but instead took %dms';
$this->fail(\sprintf($msg, $this->minimumRuntime, $actualRuntime));
}
}
} finally {
if (isset($this->timeoutId)) {
Loop::cancel($this->timeoutId);
Expand All @@ -53,6 +64,11 @@ final public function runAsyncTest(...$args)
return $returnValue;
}

final protected function setMinimumRuntime(int $runtime)
{
$this->minimumRuntime = $runtime;
}

final protected function setTimeout(int $timeout)
{
$this->timeoutId = Loop::delay($timeout, function () use ($timeout) {
Expand Down
2 changes: 1 addition & 1 deletion src/LoopReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class LoopReset extends BaseTestListener
public function endTest(Test $test, $time)
{
Loop::set((new Loop\DriverFactory)->create());
gc_collect_cycles(); // extensions using an event loop may otherwise leak the file descriptors to the loop
\gc_collect_cycles(); // extensions using an event loop may otherwise leak the file descriptors to the loop
}
}
14 changes: 13 additions & 1 deletion test/AsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use PHPUnit\Framework\AssertionFailedError;
use function Amp\call;

class AsyncTestCaseTest extends AsyncTestCase
{

public function testThatMethodRunsInLoopContext()
{
$returnDeferred = new Deferred(); // make sure our test runs to completion
Expand Down Expand Up @@ -77,4 +77,16 @@ public function testArgumentSupport(string $foo, int $bar, bool $baz)
$this->assertTrue($baz);
}

public function testSetMinimumRunTime()
{
$this->setMinimumRuntime(100);
$func = function () {
yield new Delayed(75);
return 'finished';
};

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessageRegExp("/Expected test to take at least 100ms but instead took (\d+)ms/");
yield call($func);
}
}

0 comments on commit a72b1fa

Please sign in to comment.