diff --git a/README.md b/README.md index 15fae1f..d8d9cb9 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,11 @@ is a random token stored in the browser session, in order to make the test session specific to the executing browser, and allow multiple people using their own test session in the same webroot. +The module also keeps some metadata about the session state in the database, +so that it may be available for the clients as well. +E.g. the silverstripe-behat-extension may use it through this module APIs, +allowing us to introduce some grey-box testing techniques. + The module also serves as an initializer for the [SilverStripe Behat Extension](https://github.com/silverstripe-labs/silverstripe-behat-extension/). It is required for Behat because the Behat CLI test runner needs to persist diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php index 6e62a7d..9ffbe16 100644 --- a/src/TestSessionEnvironment.php +++ b/src/TestSessionEnvironment.php @@ -335,6 +335,8 @@ class TestSessionEnvironment // Connect to the new database, overwriting the old DB connection (if any) DB::connect($databaseConfig); } + + TestSessionState::create()->write(); // initialize the session state } // Mailer @@ -363,6 +365,7 @@ class TestSessionEnvironment } $this->saveState($state); + $this->extend('onAfterApplyState'); } @@ -558,4 +561,38 @@ class TestSessionEnvironment { return PUBLIC_PATH . DIRECTORY_SEPARATOR . 'assets_backup'; } + + + /** + * Wait for pending requests + * + * @param int $await Time to wait (in ms) after the last response (to allow the browser react) + * @param int $timeout For how long (in ms) do we wait before giving up + * + * @return bool Whether there are no more pending requests + */ + public function waitForPendingRequests($await = 700, $timeout = 10000) + { + $now = static function () { + return microtime(true) * 10000; + }; + + $timeout = $now() + $timeout; + $interval = max(300, $await); + do { + $model = TestSessionState::get()->byID(1); + + $pendingRequests = $model->PendingRequests > 0; + $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/TestSessionHTTPMiddleware.php b/src/TestSessionHTTPMiddleware.php index c75bc38..ac8be5c 100644 --- a/src/TestSessionHTTPMiddleware.php +++ b/src/TestSessionHTTPMiddleware.php @@ -39,12 +39,14 @@ class TestSessionHTTPMiddleware implements HTTPMiddleware // Load test state $this->loadTestState($request); + TestSessionState::incrementState(); // Call with safe teardown try { return $delegate($request); } finally { $this->restoreTestState($request); + TestSessionState::decrementState(); } } diff --git a/src/TestSessionState.php b/src/TestSessionState.php new file mode 100644 index 0000000..3755392 --- /dev/null +++ b/src/TestSessionState.php @@ -0,0 +1,60 @@ + 'Int', + '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(); + } +}