forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWorkerTest.php
More file actions
executable file
·289 lines (238 loc) · 8.67 KB
/
QueueWorkerTest.php
File metadata and controls
executable file
·289 lines (238 loc) · 8.67 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
use Illuminate\Queue\WorkerOptions;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\MaxAttemptsExceededException;
class QueueWorkerTest extends PHPUnit_Framework_TestCase
{
public $events;
public $exceptionHandler;
public function __construct()
{
$this->events = Mockery::spy(Dispatcher::class);
$this->exceptionHandler = Mockery::spy(ExceptionHandler::class);
}
public function test_job_can_be_fired()
{
$worker = $this->getWorker('default', ['queue' => [$job = new WorkerFakeJob]]);
$worker->runNextJob('default', 'queue', new WorkerOptions);
$this->assertTrue($job->fired);
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobProcessing::class))->once();
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobProcessed::class))->once();
}
public function test_job_can_be_fired_based_on_priority()
{
$worker = $this->getWorker('default', [
'high' => [$highJob = new WorkerFakeJob, $secondHighJob = new WorkerFakeJob], 'low' => [$lowJob = new WorkerFakeJob],
]);
$worker->runNextJob('default', 'high,low', new WorkerOptions);
$this->assertTrue($highJob->fired);
$this->assertFalse($secondHighJob->fired);
$this->assertFalse($lowJob->fired);
$worker->runNextJob('default', 'high,low', new WorkerOptions);
$this->assertTrue($secondHighJob->fired);
$this->assertFalse($lowJob->fired);
$worker->runNextJob('default', 'high,low', new WorkerOptions);
$this->assertTrue($lowJob->fired);
}
public function test_exception_is_reported_if_connection_throws_exception_on_job_pop()
{
$worker = new InsomniacWorker(
new WorkerFakeManager('default', new BrokenQueueConnection($e = new RuntimeException)),
$this->events,
$this->exceptionHandler
);
$worker->runNextJob('default', 'queue', $this->workerOptions());
$this->exceptionHandler->shouldHaveReceived('report')->with($e);
}
public function test_worker_sleeps_when_queue_is_empty()
{
$worker = $this->getWorker('default', ['queue' => []]);
$worker->runNextJob('default', 'queue', $this->workerOptions(['sleep' => 5]));
$this->assertEquals(5, $worker->sleptFor);
}
public function test_job_is_released_on_exception()
{
$e = new RuntimeException;
$job = new WorkerFakeJob(function () use ($e) {
throw $e;
});
$worker = $this->getWorker('default', ['queue' => [$job]]);
$worker->runNextJob('default', 'queue', $this->workerOptions(['delay' => 10]));
$this->assertEquals(10, $job->releaseAfter);
$this->assertFalse($job->deleted);
$this->exceptionHandler->shouldHaveReceived('report')->with($e);
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobExceptionOccurred::class))->once();
$this->events->shouldNotHaveReceived('fire', [Mockery::type(JobProcessed::class)]);
}
public function test_job_is_not_released_if_it_has_exceeded_max_attempts()
{
$e = new RuntimeException;
$job = new WorkerFakeJob(function ($job) use ($e) {
// In normal use this would be incremented by being popped off the queue
$job->attempts++;
throw $e;
});
$job->attempts = 1;
$worker = $this->getWorker('default', ['queue' => [$job]]);
$worker->runNextJob('default', 'queue', $this->workerOptions(['maxTries' => 1]));
$this->assertNull($job->releaseAfter);
$this->assertTrue($job->deleted);
$this->assertEquals($e, $job->failedWith);
$this->exceptionHandler->shouldHaveReceived('report')->with($e);
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobExceptionOccurred::class))->once();
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobFailed::class))->once();
$this->events->shouldNotHaveReceived('fire', [Mockery::type(JobProcessed::class)]);
}
public function test_job_is_failed_if_it_has_already_exceeded_max_attempts()
{
$job = new WorkerFakeJob(function ($job) {
$job->attempts++;
});
$job->attempts = 2;
$worker = $this->getWorker('default', ['queue' => [$job]]);
$worker->runNextJob('default', 'queue', $this->workerOptions(['maxTries' => 1]));
$this->assertNull($job->releaseAfter);
$this->assertTrue($job->deleted);
$this->assertInstanceOf(MaxAttemptsExceededException::class, $job->failedWith);
$this->exceptionHandler->shouldHaveReceived('report')->with(Mockery::type(MaxAttemptsExceededException::class));
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobExceptionOccurred::class))->once();
$this->events->shouldHaveReceived('fire')->with(Mockery::type(JobFailed::class))->once();
$this->events->shouldNotHaveReceived('fire', [Mockery::type(JobProcessed::class)]);
}
/**
* Helpers...
*/
private function getWorker($connectionName = 'default', $jobs = [])
{
return new InsomniacWorker(
...$this->workerDependencies($connectionName, $jobs)
);
}
private function workerDependencies($connectionName = 'default', $jobs = [])
{
return [
new WorkerFakeManager($connectionName, new WorkerFakeConnection($jobs)),
$this->events,
$this->exceptionHandler,
];
}
private function workerOptions(array $overrides = [])
{
$options = new WorkerOptions;
foreach ($overrides as $key => $value) {
$options->{$key} = $value;
}
return $options;
}
}
/**
* Fakes.
*/
class InsomniacWorker extends Illuminate\Queue\Worker
{
public $sleptFor;
public function sleep($seconds)
{
$this->sleptFor = $seconds;
}
}
class WorkerFakeManager extends Illuminate\Queue\QueueManager
{
public $connections = [];
public function __construct($name, $connection)
{
$this->connections[$name] = $connection;
}
public function connection($name = null)
{
return $this->connections[$name];
}
}
class WorkerFakeConnection
{
public $jobs = [];
public function __construct($jobs)
{
$this->jobs = $jobs;
}
public function pop($queue)
{
return array_shift($this->jobs[$queue]);
}
}
class BrokenQueueConnection
{
public $exception;
public function __construct($exception)
{
$this->exception = $exception;
}
public function pop($queue)
{
throw $this->exception;
}
}
class WorkerFakeJob
{
public $fired = false;
public $callback;
public $deleted = false;
public $releaseAfter;
public $attempts = 0;
public $failedWith;
public function __construct($callback = null)
{
$this->callback = $callback ?: function () {
};
}
public function fire()
{
$this->fired = true;
$this->callback->__invoke($this);
}
public function payload()
{
return [];
}
public function delete()
{
$this->deleted = true;
}
public function isDeleted()
{
return $this->deleted;
}
public function release($delay)
{
$this->releaseAfter = $delay;
}
public function attempts()
{
return $this->attempts;
}
public function failed($e)
{
$this->failedWith = $e;
}
public function testJobSleepsWhenAnExceptionIsThrownForADaemonWorker()
{
$exceptionHandler = m::mock('Illuminate\Contracts\Debug\ExceptionHandler');
$job = m::mock('Illuminate\Contracts\Queue\Job');
$job->shouldReceive('fire')->once()->andReturnUsing(function () {
throw new RuntimeException;
});
$worker = m::mock('Illuminate\Queue\Worker', [$manager = m::mock('Illuminate\Queue\QueueManager')])->makePartial();
$manager->shouldReceive('connection')->once()->with('connection')->andReturn($connection = m::mock('StdClass'));
$manager->shouldReceive('getName')->andReturn('connection');
$connection->shouldReceive('pop')->once()->with('queue')->andReturn($job);
$worker->shouldReceive('sleep')->once()->with(3);
$exceptionHandler->shouldReceive('report')->once();
$worker->setDaemonExceptionHandler($exceptionHandler);
$worker->pop('connection', 'queue');
}
}