Skip to content

Commit d7dafdd

Browse files
committed
JobAttemptsTest
1 parent a37b4ee commit d7dafdd

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/vendor
44
composer.lock
55
.phpstorm.meta.php
6-
phpunit.xml
6+
phpunit.xml
7+
.phpunit.result.cache

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"phpunit/phpunit": "^8.4",
2222
"illuminate/events": "^6.0",
2323
"mockery/mockery": "^1.0",
24-
"laravel/horizon": "^3.0"
24+
"laravel/horizon": "^3.0",
25+
"larapack/dd": "^1.1"
2526
},
2627
"autoload": {
2728
"psr-4": {

tests/Functional/JobAttemptsTest.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace VladimirYuldashev\LaravelQueueRabbitMQ\Tests\Functional;
4+
5+
use Enqueue\AmqpLib\AmqpConnectionFactory;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Events\Dispatcher;
8+
use Interop\Amqp\AmqpTopic;
9+
use Interop\Queue\Exception\PurgeQueueNotSupportedException;
10+
use PHPUnit\Framework\TestCase;
11+
use Psr\Log\NullLogger;
12+
use ReflectionException;
13+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
14+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob;
15+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
16+
17+
/**
18+
* @group functional
19+
*/
20+
class JobAttemptsTest extends TestCase
21+
{
22+
/**
23+
* @throws PurgeQueueNotSupportedException
24+
* @throws ReflectionException
25+
*/
26+
public function test()
27+
{
28+
$config = [
29+
'factory_class' => AmqpConnectionFactory::class,
30+
'dsn' => null,
31+
'host' => getenv('HOST'),
32+
'port' => getenv('PORT'),
33+
'login' => 'guest',
34+
'password' => 'guest',
35+
'vhost' => '/',
36+
'options' => [
37+
'exchange' => [
38+
'name' => null,
39+
'declare' => true,
40+
'type' => AmqpTopic::TYPE_DIRECT,
41+
'passive' => false,
42+
'durable' => true,
43+
'auto_delete' => false,
44+
],
45+
46+
'queue' => [
47+
'name' => 'default',
48+
'declare' => true,
49+
'bind' => true,
50+
'passive' => false,
51+
'durable' => true,
52+
'exclusive' => false,
53+
'auto_delete' => false,
54+
'arguments' => '[]',
55+
],
56+
],
57+
'ssl_params' => [
58+
'ssl_on' => false,
59+
'cafile' => null,
60+
'local_cert' => null,
61+
'local_key' => null,
62+
'verify_peer' => true,
63+
'passphrase' => null,
64+
],
65+
];
66+
67+
$connector = new RabbitMQConnector(new Dispatcher());
68+
/** @var RabbitMQQueue $queue */
69+
$queue = $connector->connect($config);
70+
$queue->setContainer($this->createDummyContainer());
71+
72+
// we need it to declare exchange\queue on RabbitMQ side.
73+
$queue->pushRaw('something');
74+
75+
$queue->getContext()->purgeQueue($queue->getContext()->createQueue('default'));
76+
77+
$expectedPayload = __METHOD__.microtime(true);
78+
79+
$queue->pushRaw($expectedPayload);
80+
81+
sleep(1);
82+
83+
$this->assertEquals(1, $queue->size());
84+
85+
$job = $queue->pop();
86+
87+
$this->assertInstanceOf(RabbitMQJob::class, $job);
88+
$this->assertSame(1, $job->attempts());
89+
90+
$job->release();
91+
92+
$this->assertEquals(1, $queue->size());
93+
94+
$job = $queue->pop();
95+
96+
$this->assertInstanceOf(RabbitMQJob::class, $job);
97+
$this->assertSame(2, $job->attempts());
98+
}
99+
100+
private function createDummyContainer()
101+
{
102+
$container = new Container();
103+
$container['log'] = new NullLogger();
104+
105+
return $container;
106+
}
107+
}

0 commit comments

Comments
 (0)