Skip to content

Commit 58303bd

Browse files
author
Matt Gaunt
committed
Adding retries to tests
1 parent 4225513 commit 58303bd

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tests/PushServiceTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PushServiceTest extends PHPUnit_Framework_TestCase
3333
* detect current OS, we can probably run this automatically
3434
* for Linux and OS X at a later date.
3535
*/
36-
/** protected function checkRequirements()
36+
/**protected function checkRequirements()
3737
{
3838
parent::checkRequirements();
3939
@@ -104,12 +104,37 @@ public function browserProvider()
104104
);
105105
}
106106

107+
/**
108+
* Selenium tests are flakey so add retries.
109+
*/
110+
public function retryTest($retryCount, $test)
111+
{
112+
// just like above without checking the annotation
113+
for ($i = 0; $i < $retryCount; $i++) {
114+
try {
115+
$test();
116+
return;
117+
}
118+
catch (Exception $e) {
119+
// last one thrown below
120+
}
121+
}
122+
if ($e) {
123+
throw $e;
124+
}
125+
}
126+
107127
/**
108128
* @dataProvider browserProvider
109129
* Run integration tests with browsers
110130
*/
111131
public function testBrowsers($browserId, $browserVersion, $options)
112132
{
133+
$this->retryTest(4, $this->createClosureTest($browserId, $browserVersion, $options));
134+
}
135+
136+
protected function createClosureTest($browserId, $browserVersion, $options) {
137+
return function() use ($browserId, $browserVersion, $options) {
113138
$this->webPush = new WebPush($options);
114139
$this->webPush->setAutomaticPadding(false);
115140

@@ -140,14 +165,23 @@ public function testBrowsers($browserId, $browserVersion, $options)
140165
),
141166
CURLOPT_TIMEOUT => 30,
142167
));
143-
144168
$resp = curl_exec($getSubscriptionCurl);
145169

146170
// Close request to clear up some resources
147171
curl_close($getSubscriptionCurl);
148172

149173
$parsedResp = json_decode($resp);
150174

175+
176+
177+
if (!array_key_exists('data', $parsedResp)) {
178+
echo "\n\n";
179+
echo "Unexpected response from web-push-testing-service.............";
180+
var_dump($resp);
181+
echo "\n\n";
182+
throw new Error('Unexpected response from web-push-testing-service.');
183+
}
184+
151185
$testId = $parsedResp->{'data'}->{'testId'};
152186
$subscription = $parsedResp->{'data'}->{'subscription'};
153187
$endpoint = $subscription->{'endpoint'};
@@ -201,6 +235,7 @@ public function testBrowsers($browserId, $browserVersion, $options)
201235
throw $e;
202236
}
203237
}
238+
};
204239
}
205240

206241
protected function tearDown()

0 commit comments

Comments
 (0)