Skip to content

Commit 36258c8

Browse files
committed
add wait for mock server to acceptance tests
1 parent cab8a8f commit 36258c8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/Acceptance/PubNubContext.php

+30
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
class PubNubContext
99
{
10+
protected int $retryLimit = 12;
11+
1012
/** @BeforeScenario */
1113
public function before(BeforeScenarioScope $scope): void
1214
{
15+
$this->waitForServer();
1316
$ch = curl_init();
1417
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1518
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
@@ -27,6 +30,7 @@ public function before(BeforeScenarioScope $scope): void
2730
/** @AfterScenario */
2831
public function after(AfterScenarioScope $scope): void
2932
{
33+
$this->waitForServer();
3034
$ch = curl_init();
3135
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
3236
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
@@ -39,4 +43,30 @@ public function after(AfterScenarioScope $scope): void
3943
}
4044
curl_close($ch);
4145
}
46+
47+
protected function waitForServer()
48+
{
49+
50+
for ($i = 1; $i <= $this->retryLimit; $i++) {
51+
print("Trying to connect ($i/$this->retryLimit)...\n");
52+
$ch = curl_init();
53+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
54+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
55+
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
56+
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8090');
57+
curl_exec($ch);
58+
$err = curl_error($ch);
59+
if ($err === "") {
60+
print("Server started\n");
61+
break;
62+
}
63+
print($err . "\n");
64+
65+
if ($i === $this->retryLimit) {
66+
print("Server not started\n");
67+
exit(1);
68+
}
69+
sleep($i);
70+
}
71+
}
4272
}

0 commit comments

Comments
 (0)