/
PreventOverlappingJobsTest.php
87 lines (67 loc) · 2.39 KB
/
PreventOverlappingJobsTest.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
<?php
namespace Illuminate\Tests\Integration\Queue;
use Illuminate\Bus\Dispatcher;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\CallQueuedHandler;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\PreventOverlappingJobs;
use Mockery as m;
use Orchestra\Testbench\TestCase;
/**
* @group integration
*/
class PreventOverlappingJobsTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
m::close();
}
public function testNonOverlappingJobsAreExecuted()
{
OverlappingTestJob::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
$job = m::mock(Job::class);
$job->shouldReceive('hasFailed')->andReturn(false);
$job->shouldReceive('isReleased')->andReturn(false);
$job->shouldReceive('isDeletedOrReleased')->andReturn(false);
$job->shouldReceive('delete')->once();
$instance->call($job, [
'command' => serialize($command = new OverlappingTestJob),
]);
$lockKey = (new PreventOverlappingJobs)->getLockKey($command);
$this->assertTrue(OverlappingTestJob::$handled);
$this->assertTrue($this->app->get(Cache::class)->lock($lockKey, 10)->acquire());
}
public function testOverlappingJobsAreNotExecuted()
{
OverlappingTestJob::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
$lockKey = (new PreventOverlappingJobs)->getLockKey($command = new OverlappingTestJob);
$this->app->get(Cache::class)->lock($lockKey, 10)->acquire();
$job = m::mock(Job::class);
$job->shouldReceive('hasFailed')->andReturn(false);
$job->shouldReceive('isReleased')->andReturn(false);
$job->shouldReceive('isDeletedOrReleased')->andReturn(false);
$job->shouldReceive('delete')->once();
$instance->call($job, [
'command' => serialize($command),
]);
$this->assertFalse(OverlappingTestJob::$handled);
}
}
class OverlappingTestJob
{
use InteractsWithQueue, Queueable;
public static $handled = false;
public function handle()
{
static::$handled = true;
}
public function middleware()
{
return [new PreventOverlappingJobs];
}
}