From 82330cb6b38371b176c3852e64cd51e08780c94b Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Mon, 24 Jun 2024 21:58:56 +0200 Subject: [PATCH 1/2] Add tests for custom queue on batched jobs --- tests/QueueTest.php | 32 ++++++++++++++++++++++++++++++++ tests/Support/BaseJob.php | 3 ++- tests/Support/SimpleJob.php | 10 +--------- tests/TestCase.php | 20 +++++++++++++++----- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 0683fd3..2386e76 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -12,6 +12,7 @@ use Illuminate\Queue\Events\JobQueued; use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Queue; @@ -27,6 +28,7 @@ use Tests\Support\JobOutput; use Tests\Support\JobThatWillBeReleased; use Tests\Support\SimpleJob; +use Tests\Support\SimpleJobWithTimeout; use Tests\Support\User; use Tests\Support\UserJob; @@ -518,4 +520,34 @@ public function headers_can_be_added_to_the_task_with_job_context() return $task->getHttpRequest()->getHeaders()['X-MyHeader'] === SimpleJob::class; }); } + + #[Test] + public function batched_jobs_with_custom_queue_are_dispatched_on_the_custom_queue() + { + // Arrange + CloudTasksApi::fake(); + + // Act + $this->dispatch(Bus::batch([ + tap(new SimpleJob(), function (SimpleJob $job) { + $job->queue = 'my-queue1'; + }), + tap(new SimpleJobWithTimeout(), function (SimpleJob $job) { + $job->queue = 'my-queue2'; + }), + ])->onQueue('my-batch-queue')); + + // Assert + CloudTasksApi::assertCreatedTaskCount(2); + + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return str_contains($task->getName(), 'SimpleJob') + && str_contains($task->getName(), 'my-batch-queue'); + }); + + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return str_contains($task->getName(), 'SimpleJobWithTimeout') + && str_contains($task->getName(), 'my-batch-queue'); + }); + } } diff --git a/tests/Support/BaseJob.php b/tests/Support/BaseJob.php index 4f9cc94..98b9688 100644 --- a/tests/Support/BaseJob.php +++ b/tests/Support/BaseJob.php @@ -4,6 +4,7 @@ namespace Tests\Support; +use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -12,5 +13,5 @@ class BaseJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; } diff --git a/tests/Support/SimpleJob.php b/tests/Support/SimpleJob.php index 4825355..007a308 100644 --- a/tests/Support/SimpleJob.php +++ b/tests/Support/SimpleJob.php @@ -4,16 +4,8 @@ namespace Tests\Support; -use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Foundation\Bus\Dispatchable; -use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\SerializesModels; - -class SimpleJob implements ShouldQueue +class SimpleJob extends BaseJob { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $tries = 3; public $id = 0; diff --git a/tests/TestCase.php b/tests/TestCase.php index cdaff0e..7c02999 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,8 +5,7 @@ namespace Tests; use Google\Cloud\Tasks\V2\Client\CloudTasksClient; -use Illuminate\Foundation\Bus\PendingClosureDispatch; -use Illuminate\Foundation\Bus\PendingDispatch; +use Illuminate\Bus\PendingBatch; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Event; use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksServiceProvider; @@ -42,6 +41,7 @@ protected function defineDatabaseMigrations() // Necessary to test the [failed_jobs] table. $this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations'); + $this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations/queue'); } protected function getEnvironmentSetUp($app) @@ -95,11 +95,21 @@ public function dispatch($job, ?string $onQueue = null): DispatchedJob $task = $event->task; }); - tap(dispatch($job), function (PendingClosureDispatch|PendingDispatch $pendingDispatch) use ($onQueue) { - if ($onQueue !== null) { + if ($job instanceof PendingBatch) { + if ($onQueue) { + $job->onQueue($onQueue); + } + + $job->dispatch(); + } else { + $pendingDispatch = dispatch($job); + + if ($onQueue) { $pendingDispatch->onQueue($onQueue); } - }); + + unset($pendingDispatch); + } return new DispatchedJob($payload, $task, $this); } From c7b88174df158f8f302d7e3e612a902b61e5de0d Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Wed, 26 Jun 2024 22:55:12 +0200 Subject: [PATCH 2/2] Set the batching db connection --- tests/TestCase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 7c02999..7bf5b86 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -47,6 +47,7 @@ protected function defineDatabaseMigrations() protected function getEnvironmentSetUp($app) { $app['config']->set('database.default', 'testbench'); + $app['config']->set('queue.batching.database', 'testbench'); $port = env('DB_DRIVER') === 'mysql' ? 3307 : 5432; $app['config']->set('database.connections.testbench', [ 'driver' => env('DB_DRIVER', 'mysql'),