-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRetryTrait.php
62 lines (51 loc) · 1.64 KB
/
RetryTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Enqueue\Test;
use PHPUnit\Framework\IncompleteTestError;
use PHPUnit\Framework\SkippedTestError;
use PHPUnit\Util\Test;
trait RetryTrait
{
public function runBare(): void
{
$e = null;
$numberOfRetires = $this->getNumberOfRetries();
if (false == is_numeric($numberOfRetires)) {
throw new \LogicException(sprintf('The $numberOfRetires must be a number but got "%s"', var_export($numberOfRetires, true)));
}
$numberOfRetires = (int) $numberOfRetires;
if ($numberOfRetires <= 0) {
throw new \LogicException(sprintf('The $numberOfRetires must be a positive number greater than 0 but got "%s".', $numberOfRetires));
}
for ($i = 0; $i < $numberOfRetires; ++$i) {
try {
parent::runBare();
return;
} catch (IncompleteTestError $e) {
throw $e;
} catch (SkippedTestError $e) {
throw $e;
} catch (\Throwable $e) {
// last one thrown below
} catch (\Exception $e) {
// last one thrown below
}
}
if ($e) {
throw $e;
}
}
/**
* @return int
*/
private function getNumberOfRetries()
{
$annotations = Test::parseTestMethodAnnotations(static::class, $this->getName(false));
if (isset($annotations['method']['retry'][0])) {
return $annotations['method']['retry'][0];
}
if (isset($annotations['class']['retry'][0])) {
return $annotations['class']['retry'][0];
}
return 1;
}
}