From b1944b62aa03cda408c669ce192acaf2c13390e8 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Tue, 12 Feb 2019 17:59:57 -0600 Subject: [PATCH] Move runtime calculation outside of Loop::run() Test method may only set watchers, not necessarily return a promise. --- src/AsyncTestCase.php | 16 ++++++++-------- test/AsyncTestCaseTest.php | 6 ++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/AsyncTestCase.php b/src/AsyncTestCase.php index cea1edd..8f63e5e 100644 --- a/src/AsyncTestCase.php +++ b/src/AsyncTestCase.php @@ -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 diff --git a/test/AsyncTestCaseTest.php b/test/AsyncTestCaseTest.php index 8980383..c76ecbc 100644 --- a/test/AsyncTestCaseTest.php +++ b/test/AsyncTestCaseTest.php @@ -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 {