mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
Merge pull request #59 from open-sausages/pulls/2.2/wait-for-pending-requests-for-real
Fix TestSessionState and TestSessionEnvironment
This commit is contained in:
commit
8827e97417
@ -573,24 +573,22 @@ class TestSessionEnvironment
|
|||||||
*/
|
*/
|
||||||
public function waitForPendingRequests($await = 700, $timeout = 10000)
|
public function waitForPendingRequests($await = 700, $timeout = 10000)
|
||||||
{
|
{
|
||||||
$now = static function () {
|
$timeout = TestSessionState::millitime() + $timeout;
|
||||||
return microtime(true) * 10000;
|
|
||||||
};
|
|
||||||
|
|
||||||
$timeout = $now() + $timeout;
|
|
||||||
$interval = max(300, $await);
|
$interval = max(300, $await);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
$now = TestSessionState::millitime();
|
||||||
|
|
||||||
|
if ($timeout < $now) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$model = TestSessionState::get()->byID(1);
|
$model = TestSessionState::get()->byID(1);
|
||||||
|
|
||||||
$pendingRequests = $model->PendingRequests > 0;
|
$pendingRequests = $model->PendingRequests > 0;
|
||||||
$lastRequestAwait = ($model->LastResponseTimestamp + $await) > $now();
|
$lastRequestAwait = ($model->LastResponseTimestamp + $await) > $now;
|
||||||
|
|
||||||
$pending = $pendingRequests || $lastRequestAwait;
|
$pending = $pendingRequests || $lastRequestAwait;
|
||||||
|
|
||||||
if ($timeout < $now()) {
|
|
||||||
// timed out
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} while ($pending && (usleep($interval * 1000) || true));
|
} while ($pending && (usleep($interval * 1000) || true));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -53,8 +53,18 @@ class TestSessionState extends DataObject
|
|||||||
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
|
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
|
||||||
->addWhere(['ID' => 1])
|
->addWhere(['ID' => 1])
|
||||||
->assignSQL('"PendingRequests"', '"PendingRequests" - 1')
|
->assignSQL('"PendingRequests"', '"PendingRequests" - 1')
|
||||||
->assign('"LastResponseTimestamp"', microtime(true) * 10000);
|
->assign('"LastResponseTimestamp"', self::millitime());
|
||||||
|
|
||||||
$update->execute();
|
$update->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns unix timestamp in milliseconds
|
||||||
|
*
|
||||||
|
* @return float milliseconds since 1970
|
||||||
|
*/
|
||||||
|
public static function millitime()
|
||||||
|
{
|
||||||
|
return round(microtime(true) * 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user