Skip to content

Commit 48a8075

Browse files
authored
Merge pull request #6 from tyx/feature/configurable-wait-for-moco
🔨 Make configurable the tempo to wait for moco
2 parents 0a275a2 + 0ec0b26 commit 48a8075

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

features/check-moco.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ Feature: Check moco
3636
When I run behat "-f progress features/call_moco.feature"
3737
Then it should fail with:
3838
"""
39-
You should run moco by "bin/moco start -p 9997 -c features/fixtures.yml"
39+
Cannot connect to moco on 127.0.0.1 : Connection refused. Ensure to run moco with "bin/moco start -p 9997 -c features/fixtures.yml"
4040
"""

src/MocoWriter.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function __construct($jsonFile, $hostname, $port)
2222
public function mockHttpCall(array $matchedRequest, array $mockedResponse, array $events = [])
2323
{
2424
$entry = [
25-
"request" => $matchedRequest,
26-
"response" => $mockedResponse
25+
'request' => $matchedRequest,
26+
'response' => $mockedResponse,
2727
];
2828

2929
if (0 < count($events)) {
@@ -33,35 +33,47 @@ public function mockHttpCall(array $matchedRequest, array $mockedResponse, array
3333
$this->payload[] = $entry;
3434
}
3535

36-
public function writeForMoco()
36+
public function writeForMoco($maxAttempt = 10, $tempoInMs = 200)
3737
{
3838
file_put_contents($this->jsonFile, json_encode($this->payload));
3939
// We need to wait for moco detecting the fixtures file changed
4040
// If not, we can perform a request on old configFile
4141
sleep(1);
42-
$this->waitForMoco();
42+
$this->waitForMoco($maxAttempt, $tempoInMs);
4343
}
4444

45-
public function reset()
45+
public function reset($maxAttempt = 10, $tempoInMs = 200)
4646
{
4747
$this->payload = [];
4848
// To avoid false positive by having the next scenario using moco response of the previous scenario
49-
$this->writeForMoco();
49+
$this->writeForMoco($maxAttempt, $tempoInMs);
5050
}
5151

52-
private function waitForMoco()
52+
private function waitForMoco($maxAttempt, $tempoInMs)
5353
{
5454
$attempts = 0;
55-
$max = 10;
5655
$ip = gethostbyname($this->hostname);
57-
while (false === @stream_socket_client('tcp://'.$ip.':'.$this->port, $errno, $errstr, 5) && ($attempts < $max)) {
58-
usleep(200000); // 200ms
59-
$attempts++;
56+
$up = false;
57+
while ($attempts < $maxAttempt && false === $up) {
58+
$socket = @stream_socket_client('tcp://'.$ip.':'.$this->port, $errno, $errstr, 5);
59+
60+
if (false === $socket) {
61+
usleep($tempoInMs * 1000);
62+
++$attempts;
63+
} else {
64+
$up = true;
65+
@fclose($socket);
66+
}
6067
}
6168

62-
if ($max <= $attempts) {
69+
if ($maxAttempt <= $attempts) {
6370
throw new \Exception(
64-
sprintf('You should run moco by "bin/moco start -p %s -c %s"', $this->port, $this->jsonFile)
71+
sprintf('Cannot connect to moco on %s : %s. Ensure to run moco with "bin/moco start -p %s -c %s"',
72+
$ip,
73+
$errstr,
74+
$this->port,
75+
$this->jsonFile
76+
)
6577
);
6678
}
6779
}

tests/units/MocoWriter.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,49 @@ public function test_it_should_write_events_if_present()
5959
->then
6060
->function('file_put_contents')
6161
->wasCalledWithArguments('fixtures.json', json_encode([
62-
['request' => ['uri' => '/coucou'], 'response' => ['status' => '404'], 'on' => ['complete' => 'ok']]
62+
['request' => ['uri' => '/coucou'], 'response' => ['status' => '404'], 'on' => ['complete' => 'ok']],
6363
]))
6464
->once()
6565
;
6666
}
67+
68+
public function test_it_should_try_to_connect_when_first_tries_fail()
69+
{
70+
$this
71+
->given(
72+
$this->newTestedInstance('fixtures.json', '127.0.0.1', '8888'),
73+
$this->function->file_put_contents = true,
74+
$this->function->stream_socket_client = false,
75+
$this->function->stream_socket_client[5] = true,
76+
$this->testedInstance->mockHttpCall(['uri' => '/coucou'], ['status' => '404'], ['complete' => 'ok'])
77+
)
78+
->when(
79+
$this->testedInstance->writeForMoco(5, 1)
80+
)
81+
->then
82+
->function('stream_socket_client')
83+
->wasCalled()
84+
->exactly(5)
85+
;
86+
}
87+
88+
public function test_it_should_lead_to_exception_when_cannot_connect()
89+
{
90+
$this
91+
->given(
92+
$this->newTestedInstance('fixtures.json', '127.0.0.1', '8888'),
93+
$this->function->file_put_contents = true,
94+
$this->function->stream_socket_client = false,
95+
$this->testedInstance->mockHttpCall(['uri' => '/coucou'], ['status' => '404'], ['complete' => 'ok'])
96+
)
97+
->exception(function () {
98+
$this->testedInstance->writeForMoco(10, 1);
99+
})
100+
->message->contains('Cannot connect to moco')
101+
->then
102+
->function('stream_socket_client')
103+
->wasCalled()
104+
->exactly(10)
105+
;
106+
}
67107
}

0 commit comments

Comments
 (0)