Skip to content

Commit

Permalink
Move runtime calculation outside of Loop::run()
Browse files Browse the repository at this point in the history
Test method may only set watchers, not necessarily return a promise.
  • Loading branch information
trowski committed Feb 12, 2019
1 parent 5b901e6 commit b1944b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ final public function runAsyncTest(...$args)
$returnValue = null;

try {
$start = \microtime(true);

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);
}
}
});

$actualRuntime = (int) (\round(\microtime(true) - $start, self::RUNTIME_PRECISION) * 1000);
if ($this->minimumRuntime > $actualRuntime) {
$msg = 'Expected test to take at least %dms but instead took %dms';
$this->fail(\sprintf($msg, $this->minimumRuntime, $actualRuntime));
}
} finally {
Loop::set((new Loop\DriverFactory)->create());
\gc_collect_cycles(); // extensions using an event loop may otherwise leak the file descriptors to the loop
Expand Down
6 changes: 6 additions & 0 deletions test/AsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public function testSetMinimumRunTime(): \Generator
yield call($func);
}

public function testSetMinimumRunTimeWithWatchersOnly()
{
$this->setMinimumRuntime(100);
Loop::delay(100, $this->createCallback(1));
}

public function testCreateCallback()
{
$mock = $this->createCallback(1, function (int $value): int {
Expand Down

0 comments on commit b1944b6

Please sign in to comment.