-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRabbitMQHandlerTest.php
More file actions
388 lines (296 loc) · 12.3 KB
/
Copy pathRabbitMQHandlerTest.php
File metadata and controls
388 lines (296 loc) · 12.3 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Queue.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests;
use CodeIgniter\Exceptions\CriticalError;
use CodeIgniter\Queue\Entities\QueueJob;
use CodeIgniter\Queue\Exceptions\QueueException;
use CodeIgniter\Queue\Handlers\RabbitMQHandler;
use CodeIgniter\Queue\QueuePushResult;
use CodeIgniter\Test\ReflectionHelper;
use Exception;
use PhpAmqpLib\Connection\AMQPConnectionFactory;
use Tests\Support\Config\Queue as QueueConfig;
use Tests\Support\TestCase;
use Throwable;
/**
* @internal
*/
final class RabbitMQHandlerTest extends TestCase
{
use ReflectionHelper;
private QueueConfig $config;
private ?RabbitMQHandler $handler = null;
protected function setUp(): void
{
parent::setUp();
$this->config = config(QueueConfig::class);
// Skip tests if RabbitMQ is not available
if (! $this->isRabbitMQAvailable()) {
$this->markTestSkipped('RabbitMQ is not available for testing');
}
try {
$this->handler = new RabbitMQHandler($this->config);
} catch (CriticalError) {
$this->markTestSkipped('Cannot connect to RabbitMQ server');
}
}
protected function tearDown(): void
{
if ($this->handler !== null) {
// Clear test queues
try {
$this->handler->clear('test-queue');
$this->handler->clear('test-queue-1');
$this->handler->clear('test-queue-2');
$this->handler->clear('priority-test');
$this->handler->clear('custom-priority-queue');
} catch (Throwable) {
// Ignore cleanup errors
}
}
parent::tearDown();
}
public function testRabbitMQHandler(): void
{
$this->assertInstanceOf(RabbitMQHandler::class, $this->handler);
$this->assertSame('rabbitmq', $this->handler->name());
}
public function testRabbitMQConnectionFailure(): void
{
$this->expectException(CriticalError::class);
$this->expectExceptionMessage('Queue: RabbitMQ connection failed.');
$badConfig = clone $this->config;
$badConfig->rabbitmq['host'] = 'nonexistent-host';
$badConfig->rabbitmq['port'] = 12345;
new RabbitMQHandler($badConfig);
}
public function testPushJob(): void
{
$result = $this->handler->push('test-queue', 'success', ['message' => 'Hello World']);
$this->assertInstanceOf(QueuePushResult::class, $result);
$this->assertTrue($result->getStatus());
$this->assertIsInt($result->getJobId());
$this->assertNull($result->getError());
}
public function testPushJobWithDelay(): void
{
$result = $this->handler->setDelay(30)->push('test-queue', 'success', ['message' => 'Delayed']);
$this->assertInstanceOf(QueuePushResult::class, $result);
$this->assertTrue($result->getStatus());
}
public function testPushJobWithPriority(): void
{
$this->config->queuePriorities['priority-test'] = ['high', 'default', 'low'];
$result = $this->handler->setPriority('high')->push('priority-test', 'success', ['priority' => 'high']);
$this->assertTrue($result->getStatus());
}
public function testPopJob(): void
{
$this->handler->push('test-queue', 'success', ['message' => 'Test Pop']);
// Give RabbitMQ a moment to process
usleep(100_000);
$job = $this->handler->pop('test-queue', ['default']);
if ($job !== null) {
$this->assertInstanceOf(QueueJob::class, $job);
$this->assertSame('test-queue', $job->queue);
$this->assertSame('success', $job->payload['job']);
$this->assertSame(['message' => 'Test Pop'], $job->payload['data']);
// Clean up - mark as done
$this->handler->done($job);
}
}
public function testPopJobWithPriorities(): void
{
$this->config->queuePriorities['priority-test'] = ['high', 'default', 'low'];
// Push jobs with different priorities
$this->handler->setPriority('low')->push('priority-test', 'success', ['priority' => 'low']);
$this->handler->setPriority('high')->push('priority-test', 'success', ['priority' => 'high']);
$this->handler->setPriority('default')->push('priority-test', 'success', ['priority' => 'default']);
usleep(100_000);
// Should get high priority job first
$job = $this->handler->pop('priority-test', ['high', 'default', 'low']);
if ($job !== null) {
$this->assertSame('high', $job->priority);
$this->handler->done($job);
}
}
public function testJobFailure(): void
{
$this->handler->push('test-queue', 'failure', ['message' => 'Will Fail']);
usleep(100_000);
$job = $this->handler->pop('test-queue', ['default']);
if ($job !== null) {
$exception = new Exception('Test failure');
$result = $this->handler->failed($job, $exception, false);
$this->assertTrue($result);
}
}
public function testJobLater(): void
{
$this->handler->push('test-queue', 'success', ['message' => 'Reschedule']);
usleep(100_000);
$job = $this->handler->pop('test-queue', ['default']);
if ($job !== null) {
$result = $this->handler->later($job, 60);
$this->assertTrue($result);
}
}
public function testClearQueue(): void
{
$this->handler->push('test-queue', 'success', ['message' => 'Clear Test 1']);
$this->handler->push('test-queue', 'success', ['message' => 'Clear Test 2']);
usleep(100_000);
$result = $this->handler->clear('test-queue');
$this->assertTrue($result);
// Verify queue is empty
$job = $this->handler->pop('test-queue', ['default']);
$this->assertNull($job);
}
public function testIncorrectJobHandler(): void
{
$this->expectException(QueueException::class);
$this->handler->push('test-queue', 'nonexistent-job', []);
}
public function testIncorrectQueueFormat(): void
{
$this->expectException(QueueException::class);
$this->handler->push('invalid queue name!', 'success', []);
}
public function testIncorrectPriority(): void
{
$this->expectException(QueueException::class);
$this->config->queuePriorities['test-queue'] = ['high', 'low'];
$this->handler->setPriority('medium')->push('test-queue', 'success', []);
}
public function testCustomPriorityMapping(): void
{
// Define custom priorities for a queue
$this->config->queuePriorities['custom-priority-queue'] = ['urgent', 'normal', 'low'];
// Test that we can push jobs with custom priorities
$result1 = $this->handler->setPriority('urgent')->push('custom-priority-queue', 'success', ['priority' => 'urgent']);
$result2 = $this->handler->setPriority('normal')->push('custom-priority-queue', 'success', ['priority' => 'normal']);
$result3 = $this->handler->setPriority('low')->push('custom-priority-queue', 'success', ['priority' => 'low']);
$this->assertTrue($result1->getStatus());
$this->assertTrue($result2->getStatus());
$this->assertTrue($result3->getStatus());
usleep(100_000);
// Should get urgent priority job first
$job = $this->handler->pop('custom-priority-queue', ['urgent', 'normal', 'low']);
if ($job !== null) {
$this->assertSame('urgent', $job->payload['data']['priority']);
$this->handler->done($job);
}
// Then normal priority
$job = $this->handler->pop('custom-priority-queue', ['urgent', 'normal', 'low']);
if ($job !== null) {
$this->assertSame('normal', $job->payload['data']['priority']);
$this->handler->done($job);
}
// Finally low priority
$job = $this->handler->pop('custom-priority-queue', ['urgent', 'normal', 'low']);
if ($job !== null) {
$this->assertSame('low', $job->payload['data']['priority']);
$this->handler->done($job);
}
}
public function testPriority(): void
{
$this->handler->setPriority('high');
$this->assertSame('high', self::getPrivateProperty($this->handler, 'priority'));
}
public function testPriorityException(): void
{
$this->expectException(QueueException::class);
$this->expectExceptionMessage('The priority name should consists only lowercase letters.');
$this->handler->setPriority('high_:');
}
public function testPopEmpty(): void
{
$result = $this->handler->pop('empty-queue', ['default']);
$this->assertNull($result);
}
public function testFailedAndKeepJob(): void
{
$this->handler->push('test-queue', 'success', ['test' => 'data']);
$queueJob = $this->handler->pop('test-queue', ['default']);
$this->assertInstanceOf(QueueJob::class, $queueJob);
$err = new Exception('Sample exception');
$result = $this->handler->failed($queueJob, $err, true);
$this->assertTrue($result);
$this->seeInDatabase('queue_jobs_failed', [
'queue' => 'test-queue',
'connection' => 'rabbitmq',
]);
}
public function testFailedAndDontKeepJob(): void
{
$this->handler->push('test-queue', 'success', ['test' => 'data']);
$queueJob = $this->handler->pop('test-queue', ['default']);
$this->assertInstanceOf(QueueJob::class, $queueJob);
$err = new Exception('Sample exception');
$result = $this->handler->failed($queueJob, $err, false);
$this->assertTrue($result);
$this->dontSeeInDatabase('queue_jobs_failed', [
'queue' => 'test-queue',
'connection' => 'rabbitmq',
]);
}
public function testDone(): void
{
$this->handler->push('test-queue', 'success', ['test' => 'data']);
$queueJob = $this->handler->pop('test-queue', ['default']);
$this->assertInstanceOf(QueueJob::class, $queueJob);
$result = $this->handler->done($queueJob);
// Job is acknowledged and removed from RabbitMQ
$this->assertTrue($result);
}
public function testClearAll(): void
{
$this->handler->push('test-queue-1', 'success', ['test' => 'data1']);
$this->handler->push('test-queue-2', 'success', ['test' => 'data2']);
usleep(100_000);
$job1 = $this->handler->pop('test-queue-1', ['default']);
$job2 = $this->handler->pop('test-queue-2', ['default']);
$this->assertInstanceOf(QueueJob::class, $job1);
$this->assertInstanceOf(QueueJob::class, $job2);
// Put jobs back by rejecting them
if (isset($job1->amqpDeliveryTag)) {
$channel = self::getPrivateProperty($this->handler, 'channel');
$channel->basic_nack($job1->amqpDeliveryTag, false, true); // requeue=true
}
if (isset($job2->amqpDeliveryTag)) {
$channel = self::getPrivateProperty($this->handler, 'channel');
$channel->basic_nack($job2->amqpDeliveryTag, false, true);
}
usleep(100_000);
// Clear all queues
$result = $this->handler->clear();
$this->assertTrue($result);
// Verify queues are empty by attempting to pop
$jobAfter1 = $this->handler->pop('test-queue-1', ['default']);
$jobAfter2 = $this->handler->pop('test-queue-2', ['default']);
$this->assertNull($jobAfter1);
$this->assertNull($jobAfter2);
}
public function testJsonEncodeExceptionMethod(): void
{
$exception = QueueException::forFailedJsonEncode('Malformed UTF-8 characters');
$this->assertInstanceOf(QueueException::class, $exception);
$this->assertStringContainsString('Failed to JSON encode queue job: Malformed UTF-8 characters', $exception->getMessage());
}
/**
* Check if RabbitMQ is available for testing.
*/
private function isRabbitMQAvailable(): bool
{
return class_exists(AMQPConnectionFactory::class);
}
}