|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Queue; |
| 4 | + |
| 5 | +use Illuminate\Bus\Queueable; |
| 6 | +use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing; |
| 7 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 9 | +use Illuminate\Queue\InteractsWithQueue; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Orchestra\Testbench\Attributes\WithMigration; |
| 12 | + |
| 13 | +#[WithMigration] |
| 14 | +#[WithMigration('cache')] |
| 15 | +#[WithMigration('queue')] |
| 16 | +class UniqueUntilProcessingJobTest extends QueueTestCase |
| 17 | +{ |
| 18 | + protected function defineEnvironment($app) |
| 19 | + { |
| 20 | + parent::defineEnvironment($app); |
| 21 | + $app['config']->set('queue.default', 'database'); |
| 22 | + $app['config']->set('cache.default', 'database'); |
| 23 | + $this->driver = 'database'; |
| 24 | + } |
| 25 | + |
| 26 | + public function testShouldBeUniqueUntilProcessingReleasesLockWhenJobIsReleasedByAMiddleware() |
| 27 | + { |
| 28 | + // Job that does not release and gets processed |
| 29 | + UniqueTestJobThatDoesNotRelease::dispatch(); |
| 30 | + $lockKey = DB::table('cache_locks')->orderBy('id')->first()->key; |
| 31 | + $this->assertNotNull($lockKey); |
| 32 | + $this->runQueueWorkerCommand(['--once' => true]); |
| 33 | + $this->assertFalse(UniqueTestJobThatDoesNotRelease::$released); |
| 34 | + $lockKey = DB::table('cache_locks')->first()->key ?? null; |
| 35 | + $this->assertNull($lockKey); |
| 36 | + $this->assertDatabaseCount('jobs', 0); |
| 37 | + |
| 38 | + // Job that releases and does not get processed |
| 39 | + UniqueUntilProcessingJobThatReleases::dispatch(); |
| 40 | + $lockKey = DB::table('cache_locks')->first()->key; |
| 41 | + $this->assertNotNull($lockKey); |
| 42 | + $this->runQueueWorkerCommand(['--once' => true]); |
| 43 | + $this->assertFalse(UniqueUntilProcessingJobThatReleases::$handled); |
| 44 | + $this->assertTrue(UniqueUntilProcessingJobThatReleases::$released); |
| 45 | + $lockKey = DB::table('cache_locks')->orderBy('id')->first()->key ?? null; |
| 46 | + $this->assertNotNull($lockKey); |
| 47 | + |
| 48 | + UniqueUntilProcessingJobThatReleases::dispatch(); |
| 49 | + $this->assertDatabaseCount('jobs', 1); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class UniqueTestJobThatDoesNotRelease implements ShouldQueue, ShouldBeUniqueUntilProcessing |
| 54 | +{ |
| 55 | + use InteractsWithQueue, Queueable, Dispatchable; |
| 56 | + |
| 57 | + public static $handled = false; |
| 58 | + public static $released = false; |
| 59 | + |
| 60 | + public function __construct() |
| 61 | + { |
| 62 | + static::$handled = false; |
| 63 | + static::$released = false; |
| 64 | + } |
| 65 | + |
| 66 | + public function handle() |
| 67 | + { |
| 68 | + static::$handled = true; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class UniqueUntilProcessingJobThatReleases extends UniqueTestJobThatDoesNotRelease |
| 73 | +{ |
| 74 | + public function middleware() |
| 75 | + { |
| 76 | + return [ |
| 77 | + function ($job) { |
| 78 | + static::$released = true; |
| 79 | + |
| 80 | + return $job->release(30); |
| 81 | + }, |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + public function uniqueId() |
| 86 | + { |
| 87 | + return 100; |
| 88 | + } |
| 89 | +} |
0 commit comments