Skip to content

Commit

Permalink
Merge pull request #148 from stackkit/feature/batched-job-queue-tests
Browse files Browse the repository at this point in the history
Add tests for custom queue on batched jobs
  • Loading branch information
marickvantuil authored Jun 26, 2024
2 parents d6471a3 + c7b8817 commit 367c8fc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
32 changes: 32 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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');
});
}
}
3 changes: 2 additions & 1 deletion tests/Support/BaseJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,5 +13,5 @@

class BaseJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
}
10 changes: 1 addition & 9 deletions tests/Support/SimpleJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 16 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,11 +41,13 @@ 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)
{
$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'),
Expand Down Expand Up @@ -95,11 +96,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);
}
Expand Down

0 comments on commit 367c8fc

Please sign in to comment.