|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/parallel-process. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/parallel-process/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/parallel-process |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\ParallelProcess\Test\Unit\Display; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\ParallelProcess\CallbackRun; |
|
17
|
|
|
use Graze\ParallelProcess\Monitor\PoolLogger; |
|
18
|
|
|
use Graze\ParallelProcess\Pool; |
|
19
|
|
|
use Graze\ParallelProcess\Test\BufferedLogger; |
|
20
|
|
|
use Graze\ParallelProcess\Test\TestCase; |
|
21
|
|
|
use Psr\Log\LogLevel; |
|
22
|
|
|
|
|
23
|
|
|
class PoolLoggerTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var BufferedLogger */ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
/** @var PoolLogger */ |
|
28
|
|
|
private $monitor; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->logger = new BufferedLogger(); |
|
33
|
|
|
$this->monitor = new PoolLogger($this->logger); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testRunStarted() |
|
37
|
|
|
{ |
|
38
|
|
|
$run = new CallbackRun( |
|
39
|
|
|
function () { |
|
40
|
|
|
return true; |
|
41
|
|
|
}, |
|
42
|
|
|
['key' => 'value'] |
|
43
|
|
|
); |
|
44
|
|
|
$this->monitor->monitor($run); |
|
45
|
|
|
|
|
46
|
|
|
$run->start(); |
|
47
|
|
|
|
|
48
|
|
|
$logs = $this->logger->cleanLogs(); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertCount(3, $logs); // add, successful, complete |
|
51
|
|
|
list($level, $message, $context) = reset($logs); |
|
52
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
53
|
|
|
$this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: has started$/i', $message); |
|
54
|
|
|
$this->assertArraySubset( |
|
55
|
|
|
[ |
|
56
|
|
|
'run' => [ |
|
57
|
|
|
'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => false, |
|
58
|
|
|
], |
|
59
|
|
|
], |
|
60
|
|
|
$context |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testRunSuccessful() |
|
65
|
|
|
{ |
|
66
|
|
|
$run = new CallbackRun( |
|
67
|
|
|
function () { |
|
68
|
|
|
return true; |
|
69
|
|
|
}, |
|
70
|
|
|
['key' => 'value'] |
|
71
|
|
|
); |
|
72
|
|
|
$this->monitor->monitor($run); |
|
73
|
|
|
|
|
74
|
|
|
$run->start(); |
|
75
|
|
|
|
|
76
|
|
|
$logs = $this->logger->cleanLogs(); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertCount(3, $logs); // add, successful, complete |
|
79
|
|
|
list($level, $message, $context) = $logs[1]; |
|
80
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
81
|
|
|
$this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: successfully finished$/i', $message); |
|
82
|
|
|
$this->assertArraySubset( |
|
83
|
|
|
[ |
|
84
|
|
|
'run' => [ |
|
85
|
|
|
'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => true, |
|
86
|
|
|
], |
|
87
|
|
|
], |
|
88
|
|
|
$context |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testRunFailed() |
|
93
|
|
|
{ |
|
94
|
|
|
$exception = new \RuntimeException('failed'); |
|
95
|
|
|
$run = new CallbackRun( |
|
96
|
|
|
function () use ($exception) { |
|
97
|
|
|
throw $exception; |
|
98
|
|
|
}, |
|
99
|
|
|
['key' => 'value'] |
|
100
|
|
|
); |
|
101
|
|
|
$this->monitor->monitor($run); |
|
102
|
|
|
|
|
103
|
|
|
$run->start(); |
|
104
|
|
|
|
|
105
|
|
|
$logs = $this->logger->cleanLogs(); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertCount(3, $logs); // add, failed, complete |
|
108
|
|
|
list($level, $message, $context) = $logs[1]; |
|
109
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
110
|
|
|
$this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: failed - failed$/i', $message); |
|
111
|
|
|
$this->assertArraySubset( |
|
112
|
|
|
[ |
|
113
|
|
|
'run' => [ |
|
114
|
|
|
'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => false, |
|
115
|
|
|
], |
|
116
|
|
|
'errors' => ['failed'], |
|
117
|
|
|
], |
|
118
|
|
|
$context |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testRunCompleted() |
|
123
|
|
|
{ |
|
124
|
|
|
$run = new CallbackRun( |
|
125
|
|
|
function () { |
|
126
|
|
|
return true; |
|
127
|
|
|
}, |
|
128
|
|
|
['key' => 'value'] |
|
129
|
|
|
); |
|
130
|
|
|
$this->monitor->monitor($run); |
|
131
|
|
|
|
|
132
|
|
|
$run->start(); |
|
133
|
|
|
|
|
134
|
|
|
$logs = $this->logger->cleanLogs(); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertCount(3, $logs); // add, successful, complete |
|
137
|
|
|
list($level, $message, $context) = $logs[2]; |
|
138
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
139
|
|
|
$this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: has finished running$/i', $message); |
|
140
|
|
|
$this->assertArraySubset( |
|
141
|
|
|
[ |
|
142
|
|
|
'run' => [ |
|
143
|
|
|
'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => true, |
|
144
|
|
|
], |
|
145
|
|
|
], |
|
146
|
|
|
$context |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testPoolRunAdded() |
|
151
|
|
|
{ |
|
152
|
|
|
$pool = new Pool(); |
|
153
|
|
|
$run = new CallbackRun( |
|
154
|
|
|
function () { |
|
155
|
|
|
return true; |
|
156
|
|
|
}, |
|
157
|
|
|
['key' => 'value'] |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$this->monitor->monitor($pool); |
|
161
|
|
|
|
|
162
|
|
|
$pool->add($run); |
|
163
|
|
|
|
|
164
|
|
|
$logs = $this->logger->cleanLogs(); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertCount(1, $logs); |
|
167
|
|
|
list($level, $message, $context) = $logs[0]; |
|
168
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
169
|
|
|
$this->assertRegExp( |
|
170
|
|
|
'/^pool \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: run \[[\\a-zA-Z0-9]+\:[a-z0-9]+\] has been added$/i', |
|
171
|
|
|
$message |
|
172
|
|
|
); |
|
173
|
|
|
$this->assertArraySubset( |
|
174
|
|
|
[ |
|
175
|
|
|
'pool' => [ |
|
176
|
|
|
'tags' => [], 'hasStarted' => false, 'isRunning' => false, 'isSuccessful' => false, |
|
177
|
|
|
'duration' => 0, 'progress' => 0.0, |
|
178
|
|
|
'num_waiting' => 1, 'num_running' => 0, 'num_finished' => 0, |
|
179
|
|
|
], |
|
180
|
|
|
'run' => [ |
|
181
|
|
|
'tags' => ['key' => 'value'], 'hasStarted' => false, 'isRunning' => false, |
|
182
|
|
|
'isSuccessful' => false, 'duration' => 0, 'progress' => null, |
|
183
|
|
|
], |
|
184
|
|
|
], |
|
185
|
|
|
$context |
|
186
|
|
|
); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function testPoolRunUpdatedOnRunStateChange() |
|
190
|
|
|
{ |
|
191
|
|
|
$pool = new Pool(); |
|
192
|
|
|
$run = new CallbackRun( |
|
193
|
|
|
function () { |
|
194
|
|
|
return true; |
|
195
|
|
|
}, |
|
196
|
|
|
['key' => 'value'] |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
$pool->add($run); |
|
200
|
|
|
|
|
201
|
|
|
$this->monitor->monitor($pool); |
|
202
|
|
|
|
|
203
|
|
|
$run->start(); |
|
204
|
|
|
|
|
205
|
|
|
$logs = $this->logger->cleanLogs(); |
|
206
|
|
|
|
|
207
|
|
|
$this->assertCount(8, $logs); |
|
208
|
|
|
list($level, $message, $context) = $logs[1]; |
|
209
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
210
|
|
|
$this->assertRegExp('/^pool \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: updated$/i', $message); |
|
211
|
|
|
$this->assertArraySubset( |
|
212
|
|
|
[ |
|
213
|
|
|
'pool' => [ |
|
214
|
|
|
'tags' => [], 'hasStarted' => true, 'isRunning' => true, 'isSuccessful' => false, |
|
215
|
|
|
'progress' => 0.0, |
|
216
|
|
|
'num_waiting' => 0, 'num_running' => 1, 'num_finished' => 0, |
|
217
|
|
|
], |
|
218
|
|
|
], |
|
219
|
|
|
$context |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
list($level, $message, $context) = $logs[6]; |
|
223
|
|
|
$this->assertEquals(LogLevel::DEBUG, $level); |
|
224
|
|
|
$this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:[a-z0-9]+\]: has finished running$/i', $message); |
|
225
|
|
|
$this->assertArraySubset( |
|
226
|
|
|
[ |
|
227
|
|
|
'pool' => [ |
|
228
|
|
|
'type' => Pool::class, 'tags' => [], 'hasStarted' => true, 'isRunning' => false, |
|
229
|
|
|
'isSuccessful' => true, 'progress' => 1.0, |
|
230
|
|
|
'num_waiting' => 0, 'num_running' => 0, 'num_finished' => 1, |
|
231
|
|
|
], |
|
232
|
|
|
], |
|
233
|
|
|
$context |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|