|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Equip\Queue; |
|
4
|
|
|
|
|
5
|
|
|
use Equip\Queue\Driver\DriverInterface; |
|
6
|
|
|
use Equip\Queue\Exception\HandlerException; |
|
7
|
|
|
use Equip\Queue\Serializer\JsonSerializer; |
|
8
|
|
|
use Equip\Queue\Serializer\MessageSerializerInterface; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class WorkerTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var DriverInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $driver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var LoggerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $logger; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Event |
|
26
|
|
|
*/ |
|
27
|
|
|
private $event; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var MessageSerializerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $serializer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Worker |
|
36
|
|
|
*/ |
|
37
|
|
|
private $worker; |
|
38
|
|
|
|
|
39
|
|
|
protected function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->driver = $this->createMock(DriverInterface::class); |
|
42
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
|
43
|
|
|
$this->event = $this->createMock(Event::class); |
|
44
|
|
|
$this->serializer = new JsonSerializer; |
|
45
|
|
|
$this->worker = new Worker($this->driver, $this->event, $this->logger); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testGetHandler() |
|
49
|
|
|
{ |
|
50
|
|
|
$name = 'test'; |
|
51
|
|
|
$routes = [ |
|
52
|
|
|
$name => function () {}, |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
$method = static::getProtectedMethod($this->worker, 'getHandler'); |
|
56
|
|
|
$result = $method->invoke($this->worker, $name, $routes); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertSame($routes[$name], $result); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testGetHandlerNotCallable() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->expectException(HandlerException::class); |
|
64
|
|
|
$this->expectExceptionMessage('The handler for `test` is invalid.'); |
|
65
|
|
|
|
|
66
|
|
|
$name = 'test'; |
|
67
|
|
|
$routes = [ |
|
68
|
|
|
$name => 'test', |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
$method = static::getProtectedMethod($this->worker, 'getHandler'); |
|
72
|
|
|
$method->invoke($this->worker, $name, $routes); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testGetHandlerNoHandler() |
|
76
|
|
|
{ |
|
77
|
|
|
$method = static::getProtectedMethod($this->worker, 'getHandler'); |
|
78
|
|
|
$this->assertNull($method->invoke($this->worker, 'test', [])); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testTickPacketNull() |
|
82
|
|
|
{ |
|
83
|
|
|
$queue = 'test-queue'; |
|
84
|
|
|
$this->driver |
|
85
|
|
|
->expects($this->once()) |
|
86
|
|
|
->method('dequeue') |
|
87
|
|
|
->with($queue) |
|
88
|
|
|
->willReturn(null); |
|
89
|
|
|
|
|
90
|
|
|
$method = static::getProtectedMethod($this->worker, 'tick'); |
|
91
|
|
|
$this->assertTrue($method->invoke($this->worker, $queue)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testTickInvalidHandler() |
|
95
|
|
|
{ |
|
96
|
|
|
$message = [ |
|
97
|
|
|
'queue' => 'test-queue', |
|
98
|
|
|
'handler' => 'foo', |
|
99
|
|
|
'data' => ['foo' => 'bar'], |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
|
|
$this->driver |
|
103
|
|
|
->expects($this->once()) |
|
104
|
|
|
->method('dequeue') |
|
105
|
|
|
->with($message['queue']) |
|
106
|
|
|
->willReturn(json_encode($message)); |
|
107
|
|
|
|
|
108
|
|
|
$method = static::getProtectedMethod($this->worker, 'tick'); |
|
109
|
|
|
$this->assertTrue($method->invoke($this->worker, $message['queue'])); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testTickHandlerException() |
|
113
|
|
|
{ |
|
114
|
|
|
$message = new Message('queue', 'foo', ['foo' => 'bar']); |
|
115
|
|
|
$exception = new Exception; |
|
116
|
|
|
|
|
117
|
|
|
$this->driver |
|
118
|
|
|
->expects($this->once()) |
|
119
|
|
|
->method('dequeue') |
|
120
|
|
|
->with($message->queue()) |
|
121
|
|
|
->willReturn($this->serializer->serialize($message)); |
|
122
|
|
|
|
|
123
|
|
|
$this->event |
|
124
|
|
|
->expects($this->once()) |
|
125
|
|
|
->method('acknowledge') |
|
126
|
|
|
->with($message); |
|
127
|
|
|
|
|
128
|
|
|
$this->event |
|
129
|
|
|
->expects($this->never()) |
|
130
|
|
|
->method('finish') |
|
131
|
|
|
->with($message); |
|
132
|
|
|
|
|
133
|
|
|
$this->event |
|
134
|
|
|
->expects($this->once()) |
|
135
|
|
|
->method('reject') |
|
136
|
|
|
->with($message, $exception); |
|
137
|
|
|
|
|
138
|
|
|
$this->logger |
|
139
|
|
|
->expects($this->once()) |
|
140
|
|
|
->method('error') |
|
141
|
|
|
->with($exception->getMessage()); |
|
142
|
|
|
|
|
143
|
|
|
$worker = new Worker( |
|
144
|
|
|
$this->driver, |
|
145
|
|
|
$this->event, |
|
146
|
|
|
$this->logger, |
|
147
|
|
|
$this->serializer, |
|
148
|
|
|
['foo' => function () use ($exception) { throw $exception; }] |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
$method = static::getProtectedMethod($worker, 'tick'); |
|
152
|
|
|
$this->assertTrue($method->invoke($worker, $message->queue())); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testTickHandlerReturnFalse() |
|
156
|
|
|
{ |
|
157
|
|
|
$message = new Message('queue', 'foo', ['foo' => 'bar']); |
|
158
|
|
|
|
|
159
|
|
|
$this->driver |
|
160
|
|
|
->expects($this->once()) |
|
161
|
|
|
->method('dequeue') |
|
162
|
|
|
->with($message->queue()) |
|
163
|
|
|
->willReturn($this->serializer->serialize($message)); |
|
164
|
|
|
|
|
165
|
|
|
$this->event |
|
166
|
|
|
->expects($this->once()) |
|
167
|
|
|
->method('acknowledge') |
|
168
|
|
|
->with($message); |
|
169
|
|
|
|
|
170
|
|
|
$this->logger |
|
171
|
|
|
->expects($this->once()) |
|
172
|
|
|
->method('notice') |
|
173
|
|
|
->with('shutting down by request of `foo`'); |
|
174
|
|
|
|
|
175
|
|
|
$worker = new Worker( |
|
176
|
|
|
$this->driver, |
|
177
|
|
|
$this->event, |
|
178
|
|
|
$this->logger, |
|
179
|
|
|
$this->serializer, |
|
180
|
|
|
['foo' => function () { return false; }] |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
|
|
$method = static::getProtectedMethod($worker, 'tick'); |
|
184
|
|
|
$this->assertFalse($method->invoke($worker, $message->queue())); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function testTick() |
|
188
|
|
|
{ |
|
189
|
|
|
$message = new Message('queue', 'foo', ['name' => 'foo']); |
|
190
|
|
|
|
|
191
|
|
|
$this->driver |
|
192
|
|
|
->expects($this->once()) |
|
193
|
|
|
->method('dequeue') |
|
194
|
|
|
->with($message->queue()) |
|
195
|
|
|
->willReturn($this->serializer->serialize($message)); |
|
196
|
|
|
|
|
197
|
|
|
$this->event |
|
198
|
|
|
->expects($this->once()) |
|
199
|
|
|
->method('acknowledge') |
|
200
|
|
|
->with($message); |
|
201
|
|
|
|
|
202
|
|
|
$this->event |
|
203
|
|
|
->expects($this->once()) |
|
204
|
|
|
->method('finish') |
|
205
|
|
|
->with($message); |
|
206
|
|
|
|
|
207
|
|
|
$this->logger |
|
208
|
|
|
->expects($this->exactly(2)) |
|
209
|
|
|
->method('info') |
|
210
|
|
|
->withConsecutive( |
|
211
|
|
|
['`foo` job started'], |
|
212
|
|
|
['`foo` job finished'] |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
$worker = new Worker( |
|
216
|
|
|
$this->driver, |
|
217
|
|
|
$this->event, |
|
218
|
|
|
$this->logger, |
|
219
|
|
|
$this->serializer, |
|
220
|
|
|
[ |
|
221
|
|
|
'foo' => function ($data) use ($message) { |
|
222
|
|
|
$this->assertSame($message->handler(), $data->handler()); |
|
223
|
|
|
} |
|
224
|
|
|
] |
|
225
|
|
|
); |
|
226
|
|
|
|
|
227
|
|
|
$method = static::getProtectedMethod($worker, 'tick'); |
|
228
|
|
|
$this->assertTrue($method->invoke($worker, $message->queue())); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function testConsume() |
|
232
|
|
|
{ |
|
233
|
|
|
$message = new Message('queue', 'foo', ['name' => 'foo']); |
|
234
|
|
|
|
|
235
|
|
|
$this->driver |
|
236
|
|
|
->expects($this->once()) |
|
237
|
|
|
->method('dequeue') |
|
238
|
|
|
->with($message->queue()) |
|
239
|
|
|
->willReturn($this->serializer->serialize($message)); |
|
240
|
|
|
|
|
241
|
|
|
$this->event |
|
242
|
|
|
->expects($this->once()) |
|
243
|
|
|
->method('acknowledge') |
|
244
|
|
|
->with($message); |
|
245
|
|
|
|
|
246
|
|
|
$this->event |
|
247
|
|
|
->expects($this->once()) |
|
248
|
|
|
->method('finish') |
|
249
|
|
|
->with($message); |
|
250
|
|
|
|
|
251
|
|
|
$worker = new Worker( |
|
252
|
|
|
$this->driver, |
|
253
|
|
|
$this->event, |
|
254
|
|
|
$this->logger, |
|
255
|
|
|
$this->serializer, |
|
256
|
|
|
['foo' => function () { return false; }] |
|
257
|
|
|
); |
|
258
|
|
|
|
|
259
|
|
|
$worker->consume($message->queue()); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|