|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Paraunit\Parser\JSON; |
|
6
|
|
|
|
|
7
|
|
|
use Paraunit\Process\AbstractParaunitProcess; |
|
8
|
|
|
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface; |
|
9
|
|
|
use Paraunit\TestResult\MuteTestResult; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class RetryParser |
|
13
|
|
|
* @package Paraunit\Parser\JSON |
|
14
|
|
|
*/ |
|
15
|
|
|
class RetryParser |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TestResultHandlerInterface */ |
|
18
|
|
|
private $testResultContainer; |
|
19
|
|
|
|
|
20
|
|
|
/** @var int */ |
|
21
|
|
|
private $maxRetryCount; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $regexPattern; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* RetryParser constructor. |
|
28
|
|
|
* @param TestResultHandlerInterface $testResultContainer |
|
29
|
|
|
* @param int $maxRetryCount |
|
30
|
|
|
*/ |
|
31
|
55 |
|
public function __construct(TestResultHandlerInterface $testResultContainer, int $maxRetryCount = 3) |
|
32
|
|
|
{ |
|
33
|
55 |
|
$this->testResultContainer = $testResultContainer; |
|
34
|
55 |
|
$this->maxRetryCount = $maxRetryCount; |
|
35
|
|
|
|
|
36
|
|
|
$patterns = [ |
|
37
|
55 |
|
'The EntityManager is closed', |
|
38
|
|
|
// MySQL |
|
39
|
|
|
'Deadlock found', |
|
40
|
|
|
'Lock wait timeout exceeded', |
|
41
|
|
|
'SAVEPOINT \w+ does not exist', |
|
42
|
|
|
// SQLite |
|
43
|
|
|
'General error: 5 database is locked', |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
55 |
|
$this->regexPattern = $this->buildRegexPattern($patterns); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
50 |
|
public function processWillBeRetried(AbstractParaunitProcess $process, array $logs): bool |
|
50
|
|
|
{ |
|
51
|
50 |
|
if ($process->getRetryCount() >= $this->maxRetryCount) { |
|
52
|
7 |
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
49 |
|
foreach ($logs as $logItem) { |
|
56
|
49 |
|
if ($this->containsRetriableError($logItem)) { |
|
57
|
11 |
|
$process->markAsToBeRetried(); |
|
58
|
11 |
|
$testResult = new MuteTestResult(); |
|
59
|
11 |
|
$this->testResultContainer->handleTestResult($process, $testResult); |
|
60
|
|
|
|
|
61
|
49 |
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
40 |
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
49 |
|
private function containsRetriableError(\stdClass $log): bool |
|
69
|
|
|
{ |
|
70
|
49 |
|
return property_exists($log, 'status') |
|
71
|
49 |
|
&& $log->status === 'error' |
|
72
|
49 |
|
&& preg_match($this->regexPattern, $log->message); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string[] $patterns |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
55 |
|
private function buildRegexPattern(array $patterns): string |
|
80
|
|
|
{ |
|
81
|
55 |
|
$regex = implode('|', $patterns); |
|
82
|
|
|
|
|
83
|
55 |
|
return '/' . $regex . '/'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|