From d8c6357b296b8bab1ddcbe6cc5a775c277137b12 Mon Sep 17 00:00:00 2001 From: Vytautas Smilingis Date: Thu, 30 May 2024 15:01:43 +0200 Subject: [PATCH] Explicitly specify job queue --- src/CloudTasksQueue.php | 4 ++++ tests/QueueTest.php | 26 +++++++++++++++++++++++++- tests/TaskHandlerTest.php | 12 ++++++------ tests/TestCase.php | 10 ++++++++-- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 1a32cac..d27c2de 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -98,6 +98,10 @@ public function size($queue = null): int */ public function push($job, $data = '', $queue = null) { + if (! ($job instanceof Closure)) { + $job->queue = $queue ?? $job->queue ?? $this->config['queue']; + } + return $this->enqueueUsing( $job, $this->createPayload($job, $queue, $data), diff --git a/tests/QueueTest.php b/tests/QueueTest.php index c24c8f3..0683fd3 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -6,6 +6,7 @@ use Google\Cloud\Tasks\V2\HttpMethod; use Google\Cloud\Tasks\V2\Task; +use Illuminate\Queue\CallQueuedClosure; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobQueued; @@ -146,9 +147,14 @@ public function it_posts_the_task_the_correct_queue() // Arrange CloudTasksApi::fake(); + $closure = fn () => 'closure job'; + $closureDisplayName = CallQueuedClosure::create($closure)->displayName(); + // Act $this->dispatch((new SimpleJob())); $this->dispatch((new FailingJob())->onQueue('my-special-queue')); + $this->dispatch($closure); + $this->dispatch($closure, 'my-special-queue'); // Assert CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool { @@ -156,7 +162,7 @@ public function it_posts_the_task_the_correct_queue() $command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command(); return $decoded['displayName'] === SimpleJob::class - && ($command['queue'] ?? null) === null + && $command['queue'] === 'barbequeue' && $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue'; }); @@ -168,6 +174,24 @@ public function it_posts_the_task_the_correct_queue() && $command['queue'] === 'my-special-queue' && $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue'; }); + + CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName) use ($closureDisplayName): bool { + $decoded = json_decode($task->getHttpRequest()->getBody(), true); + $command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command(); + + return $decoded['displayName'] === $closureDisplayName + && $command['queue'] === 'barbequeue' + && $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue'; + }); + + CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName) use ($closureDisplayName): bool { + $decoded = json_decode($task->getHttpRequest()->getBody(), true); + $command = IncomingTask::fromJson($task->getHttpRequest()->getBody())->command(); + + return $decoded['displayName'] === $closureDisplayName + && $command['queue'] === 'my-special-queue' + && $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue'; + }); } #[Test] diff --git a/tests/TaskHandlerTest.php b/tests/TaskHandlerTest.php index fc6aa93..c550d4e 100644 --- a/tests/TaskHandlerTest.php +++ b/tests/TaskHandlerTest.php @@ -33,13 +33,13 @@ protected function setUp(): void CloudTasksApi::fake(); } - #[Override] - protected function tearDown(): void - { - parent::tearDown(); + #[Override] + protected function tearDown(): void + { + parent::tearDown(); - CloudTasksQueue::forgetWorkerOptionsCallback(); - } + CloudTasksQueue::forgetWorkerOptionsCallback(); + } #[Test] public function it_can_run_a_task() diff --git a/tests/TestCase.php b/tests/TestCase.php index 026b3c3..cdaff0e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,8 @@ namespace Tests; use Google\Cloud\Tasks\V2\Client\CloudTasksClient; +use Illuminate\Foundation\Bus\PendingClosureDispatch; +use Illuminate\Foundation\Bus\PendingDispatch; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Event; use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksServiceProvider; @@ -82,7 +84,7 @@ protected function setConfigValue($key, $value) $this->app['config']->set('queue.connections.my-cloudtasks-connection.'.$key, $value); } - public function dispatch($job): DispatchedJob + public function dispatch($job, ?string $onQueue = null): DispatchedJob { $payload = null; $task = null; @@ -93,7 +95,11 @@ public function dispatch($job): DispatchedJob $task = $event->task; }); - dispatch($job); + tap(dispatch($job), function (PendingClosureDispatch|PendingDispatch $pendingDispatch) use ($onQueue) { + if ($onQueue !== null) { + $pendingDispatch->onQueue($onQueue); + } + }); return new DispatchedJob($payload, $task, $this); }