From c452390d591b50f2dc603e8f077d7489d5f22cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Sun, 16 Apr 2017 12:57:48 +0200 Subject: [PATCH] Extract waiting for the server to start to the Utils class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- .../core/NextcloudTestServerDockerHelper.php | 22 +------------ build/acceptance/features/core/Utils.php | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/build/acceptance/features/core/NextcloudTestServerDockerHelper.php b/build/acceptance/features/core/NextcloudTestServerDockerHelper.php index 5e7c0b2df7..120e13fe70 100644 --- a/build/acceptance/features/core/NextcloudTestServerDockerHelper.php +++ b/build/acceptance/features/core/NextcloudTestServerDockerHelper.php @@ -111,14 +111,8 @@ class NextcloudTestServerDockerHelper implements NextcloudTestServerHelper { public function setUp() { $this->createAndStartContainer(); - $serverAddress = $this->getNextcloudTestServerAddress(); - - $isServerReadyCallback = function() use ($serverAddress) { - return $this->isServerReady($serverAddress); - }; $timeout = 10; - $timeoutStep = 0.5; - if (!Utils::waitFor($isServerReadyCallback, $timeout, $timeoutStep)) { + if (!Utils::waitForServer($this->getBaseUrl(), $timeout)) { throw new Exception("Docker container for Nextcloud (" . $this->containerName . ") or its Nextcloud test server could not be started"); } } @@ -144,20 +138,6 @@ class NextcloudTestServerDockerHelper implements NextcloudTestServerHelper { $this->executeDockerCommand("run --detach --user=www-data --publish 127.0.0.1:" . $this->hostPortRangeForContainer . ":80 --name=" . $this->containerName . " " . $this->imageName); } - private function isServerReady($serverAddress) { - $curlHandle = curl_init("http://" . $serverAddress); - - // Returning the transfer as the result of curl_exec prevents the - // transfer from being written to the output. - curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); - - $transfer = curl_exec($curlHandle); - - curl_close($curlHandle); - - return $transfer !== false; - } - /** * Cleans up the Nextcloud test server. * diff --git a/build/acceptance/features/core/Utils.php b/build/acceptance/features/core/Utils.php index 5dc52cd737..86b7515e4c 100644 --- a/build/acceptance/features/core/Utils.php +++ b/build/acceptance/features/core/Utils.php @@ -56,4 +56,35 @@ class Utils { return $conditionMet; } + /** + * Waits at most $timeout seconds for the server at the given URL to be up, + * checking it again every $timeoutStep seconds. + * + * Note that it does not verify whether the URL returns a valid HTTP status + * or not; it simply checks that the server at the given URL is accessible. + * + * @param string $url the URL for the server to check. + * @param float $timeout the number of seconds (decimals allowed) to wait at + * most for the server. + * @param float $timeoutStep the number of seconds (decimals allowed) to + * wait before checking the server again; by default, 0.5 seconds. + * @return boolean true if the server was found, false otherwise. + */ + public static function waitForServer($url, $timeout, $timeoutStep = 0.5) { + $isServerUpCallback = function() use ($url) { + $curlHandle = curl_init($url); + + // Returning the transfer as the result of curl_exec prevents the + // transfer from being written to the output. + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + + $transfer = curl_exec($curlHandle); + + curl_close($curlHandle); + + return $transfer !== false; + }; + return self::waitFor($isServerUpCallback, $timeout, $timeoutStep); + } + }