forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueTest.php
95 lines (77 loc) · 3.13 KB
/
QueueTest.php
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
<?php
declare(strict_types=1);
use Carbon\Carbon;
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
class QueueTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
// Always start with a clean slate
Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->truncate();
Queue::getDatabase()->table(Config::get('queue.failed.table'))->truncate();
}
public function testQueueJobLifeCycle(): void
{
$id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test');
$this->assertNotNull($id);
// Get and reserve the test job (next available)
$job = Queue::pop('test');
$this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job);
$this->assertEquals(1, $job->isReserved());
$this->assertEquals(json_encode([
'displayName' => 'test',
'job' => 'test',
'maxTries' => null,
'delay' => null,
'timeout' => null,
'data' => ['action' => 'QueueJobLifeCycle'],
]), $job->getRawBody());
// Remove reserved job
$job->delete();
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
}
public function testQueueJobExpired(): void
{
$id = Queue::push('test', ['action' => 'QueueJobExpired'], 'test');
$this->assertNotNull($id);
// Expire the test job
$expiry = Carbon::now()->subSeconds(Config::get('queue.connections.database.expire'))->getTimestamp();
Queue::getDatabase()
->table(Config::get('queue.connections.database.table'))
->where('_id', $id)
->update(['reserved' => 1, 'reserved_at' => $expiry]);
// Expect an attempted older job in the queue
$job = Queue::pop('test');
$this->assertEquals(1, $job->attempts());
$this->assertGreaterThan($expiry, $job->reservedAt());
$job->delete();
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
}
public function testFailQueueJob(): void
{
$provider = app('queue.failer');
$this->assertInstanceOf(MongoFailedJobProvider::class, $provider);
}
public function testFindFailJobNull(): void
{
Config::set('queue.failed.database', 'mongodb');
$provider = app('queue.failer');
$this->assertNull($provider->find(1));
}
public function testIncrementAttempts(): void
{
$job_id = Queue::push('test1', ['action' => 'QueueJobExpired'], 'test');
$this->assertNotNull($job_id);
$job_id = Queue::push('test2', ['action' => 'QueueJobExpired'], 'test');
$this->assertNotNull($job_id);
$job = Queue::pop('test');
$this->assertEquals(1, $job->attempts());
$job->delete();
$others_jobs = Queue::getDatabase()
->table(Config::get('queue.connections.database.table'))
->get();
$this->assertCount(1, $others_jobs);
$this->assertEquals(0, $others_jobs[0]['attempts']);
}
}