TestSessionState implementation refinement;

Move increment/decrement methods to TestSessionState class,
fix some documentation, fix some code style and readability issues
This commit is contained in:
Serge Latyntcev 2019-01-08 16:36:20 +13:00
parent 78dd43ed96
commit 0c078e5027
3 changed files with 51 additions and 47 deletions

View File

@ -571,19 +571,23 @@ class TestSessionEnvironment
* *
* @return bool Whether there are no more pending requests * @return bool Whether there are no more pending requests
*/ */
public function waitForPendingRequests($await=700, $timeout=10000) public function waitForPendingRequests($await = 700, $timeout = 10000)
{ {
$timeout = microtime(true) * 10000 + $timeout; $now = static function () {
$interval = $await < 300 ? 300 : $await; return microtime(true) * 10000;
};
$timeout = $now() + $timeout;
$interval = max(300, $await);
do { do {
$model = TestSessionState::get()->byID(1); $model = TestSessionState::get()->byID(1);
$pendingRequests = $model->PendingRequests > 0; $pendingRequests = $model->PendingRequests > 0;
$lastRequestAwait = $model->LastResponseTimestamp + $await > microtime(true) * 10000; $lastRequestAwait = ($model->LastResponseTimestamp + $await) > $now();
$pending = $pendingRequests || $lastRequestAwait; $pending = $pendingRequests || $lastRequestAwait;
if ($timeout < microtime(true) * 10000) { if ($timeout < $now()) {
// timed out // timed out
return false; return false;
} }

View File

@ -8,10 +8,8 @@ use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware; use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\Queries\SQLUpdate;
/** /**
* Sets state previously initialized through {@link TestSessionController}. * Sets state previously initialized through {@link TestSessionController}.
@ -41,42 +39,17 @@ class TestSessionHTTPMiddleware implements HTTPMiddleware
// Load test state // Load test state
$this->loadTestState($request); $this->loadTestState($request);
$this->incrementModelState(); TestSessionState::incrementState();
// Call with safe teardown // Call with safe teardown
try { try {
return $delegate($request); return $delegate($request);
} finally { } finally {
$this->restoreTestState($request); $this->restoreTestState($request);
$this->decrementModelState(); TestSessionState::decrementState();
} }
} }
protected function incrementModelState() {
$schema = DataObject::getSchema();
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(TestSessionState::class)))
->addWhere(['ID' => 1])
->addAssignments([
'PendingRequests' => [ '"PendingRequests" + 1' => [] ]
]);
$update->execute();
}
protected function decrementModelState() {
$schema = DataObject::getSchema();
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(TestSessionState::class)))
->addWhere(['ID' => 1])
->addAssignments([
'"PendingRequests"' => [ '"PendingRequests" - 1' => [] ],
'"LastResponseTimestamp"' => microtime(true) * 10000
]);
$update->execute();
}
/** /**
* Load test state from environment into "real" environment * Load test state from environment into "real" environment
* *

View File

@ -3,7 +3,7 @@
namespace SilverStripe\TestSession; namespace SilverStripe\TestSession;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Queries\SQLUpdate;
/** /**
* The session state keeps some metadata about the current test session. * The session state keeps some metadata about the current test session.
@ -12,22 +12,49 @@ use SilverStripe\ORM\DataObject;
* *
* The client side (Behat) must not use this class straightforwardly, but rather * The client side (Behat) must not use this class straightforwardly, but rather
* rely on the API of {@see TestSessionEnvironment} or {@see TestSessionController}. * rely on the API of {@see TestSessionEnvironment} or {@see TestSessionController}.
*
* @property int PendingRequests keeps information about how many requests are in progress
* @property float LastResponseTimestamp microtime of the last response made by the server
*/ */
class TestSessionState extends DataObject class TestSessionState extends DataObject
{ {
private static $db = [ private static $table_name = 'TestSessionState';
/**
* Pending requests to keep information
* about how many requests are in progress
* on the server
*/
'PendingRequests' => 'Int',
/** private static $db = [
* The microtime stamp of the last response 'PendingRequests' => 'Int',
* made by the server.
* (well, actually that's rather TestSessionMiddleware)
*/
'LastResponseTimestamp' => 'Decimal(14, 0)' 'LastResponseTimestamp' => 'Decimal(14, 0)'
]; ];
/**
* Increments TestSessionState.PendingRequests number by 1
* to indicate we have one more request in progress
*/
public static function incrementState()
{
$schema = DataObject::getSchema();
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
->addWhere(['ID' => 1])
->assignSQL('"PendingRequests"', '"PendingRequests" + 1');
$update->execute();
}
/**
* Decrements TestSessionState.PendingRequests number by 1
* to indicate we have one more request in progress.
* Also updates TestSessionState.LastResponseTimestamp
* to the current timestamp.
*/
public static function decrementState()
{
$schema = DataObject::getSchema();
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
->addWhere(['ID' => 1])
->assignSQL('"PendingRequests"', '"PendingRequests" - 1')
->assign('"LastResponseTimestamp"', microtime(true) * 10000);
$update->execute();
}
} }