From 32c8e6a3b171c1cb0727a9f2464c42acbe839579 Mon Sep 17 00:00:00 2001 From: Serge Latyntcev Date: Thu, 10 Jan 2019 11:48:59 +1300 Subject: [PATCH 1/2] Fix TestSessionState and TestSessionEnvironment Fixing a bug that makes it only wait for pending requests, but not for some time after the last response --- src/TestSessionEnvironment.php | 20 +++++++++----------- src/TestSessionState.php | 12 +++++++++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php index 9ffbe16..89443dc 100644 --- a/src/TestSessionEnvironment.php +++ b/src/TestSessionEnvironment.php @@ -573,24 +573,22 @@ class TestSessionEnvironment */ public function waitForPendingRequests($await = 700, $timeout = 10000) { - $now = static function () { - return microtime(true) * 10000; - }; - - $timeout = $now() + $timeout; + $timeout = TestSessionState::microtime() + $timeout; $interval = max(300, $await); + do { + $now = TestSessionState::microtime(); + + if ($timeout < $now) { + return false; + } + $model = TestSessionState::get()->byID(1); $pendingRequests = $model->PendingRequests > 0; - $lastRequestAwait = ($model->LastResponseTimestamp + $await) > $now(); + $lastRequestAwait = ($model->LastResponseTimestamp + $await) > $now; $pending = $pendingRequests || $lastRequestAwait; - - if ($timeout < $now()) { - // timed out - return false; - } } while ($pending && (usleep($interval * 1000) || true)); return true; diff --git a/src/TestSessionState.php b/src/TestSessionState.php index 3755392..1afd494 100644 --- a/src/TestSessionState.php +++ b/src/TestSessionState.php @@ -53,8 +53,18 @@ class TestSessionState extends DataObject $update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class))) ->addWhere(['ID' => 1]) ->assignSQL('"PendingRequests"', '"PendingRequests" - 1') - ->assign('"LastResponseTimestamp"', microtime(true) * 10000); + ->assign('"LastResponseTimestamp"', self::microtime()); $update->execute(); } + + /** + * Returns unix timestamp in milliseconds + * + * @return float milliseconds since 1970 + */ + public static function microtime() + { + return round(microtime(true) * 1000); + } } From f54baefb5a04bd1f8742e0a7acf510eb6e8a79fc Mon Sep 17 00:00:00 2001 From: Serge Latyntcev Date: Thu, 10 Jan 2019 15:30:39 +1300 Subject: [PATCH 2/2] Rename TestSessionState::microtime to millitime --- src/TestSessionEnvironment.php | 4 ++-- src/TestSessionState.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php index 89443dc..f6fcb09 100644 --- a/src/TestSessionEnvironment.php +++ b/src/TestSessionEnvironment.php @@ -573,11 +573,11 @@ class TestSessionEnvironment */ public function waitForPendingRequests($await = 700, $timeout = 10000) { - $timeout = TestSessionState::microtime() + $timeout; + $timeout = TestSessionState::millitime() + $timeout; $interval = max(300, $await); do { - $now = TestSessionState::microtime(); + $now = TestSessionState::millitime(); if ($timeout < $now) { return false; diff --git a/src/TestSessionState.php b/src/TestSessionState.php index 1afd494..7cc3835 100644 --- a/src/TestSessionState.php +++ b/src/TestSessionState.php @@ -53,7 +53,7 @@ class TestSessionState extends DataObject $update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class))) ->addWhere(['ID' => 1]) ->assignSQL('"PendingRequests"', '"PendingRequests" - 1') - ->assign('"LastResponseTimestamp"', self::microtime()); + ->assign('"LastResponseTimestamp"', self::millitime()); $update->execute(); } @@ -63,7 +63,7 @@ class TestSessionState extends DataObject * * @return float milliseconds since 1970 */ - public static function microtime() + public static function millitime() { return round(microtime(true) * 1000); }