Extract waiting for the server to start to the Utils class
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
This commit is contained in:
parent
34510b73a2
commit
c452390d59
2 changed files with 32 additions and 21 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue