|
5 | 5 | namespace Tests; |
6 | 6 |
|
7 | 7 | use Google\Cloud\Tasks\V2\HttpMethod; |
| 8 | +use Google\Cloud\Tasks\V2\RetryConfig; |
8 | 9 | use Google\Cloud\Tasks\V2\Task; |
| 10 | +use Illuminate\Queue\Events\JobProcessed; |
| 11 | +use Illuminate\Queue\Events\JobProcessing; |
9 | 12 | use Illuminate\Queue\Events\JobQueued; |
10 | 13 | use Illuminate\Support\Carbon; |
11 | 14 | use Illuminate\Support\Facades\DB; |
12 | 15 | use Illuminate\Support\Facades\Event; |
| 16 | +use Illuminate\Support\Facades\Log; |
| 17 | +use Illuminate\Support\Facades\Queue; |
13 | 18 | use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi; |
14 | 19 | use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased; |
| 20 | +use Stackkit\LaravelGoogleCloudTasksQueue\LogFake; |
15 | 21 | use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator; |
16 | 22 | use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler; |
17 | 23 | use Tests\Support\FailingJob; |
| 24 | +use Tests\Support\FailingJobWithExponentialBackoff; |
18 | 25 | use Tests\Support\JobThatWillBeReleased; |
19 | 26 | use Tests\Support\SimpleJob; |
20 | 27 |
|
@@ -286,4 +293,158 @@ public function jobs_can_be_released_with_a_delay() |
286 | 293 | && $scheduleTime === now()->getTimestamp() + 15; |
287 | 294 | }); |
288 | 295 | } |
| 296 | + |
| 297 | + /** @test */ |
| 298 | + public function test_default_backoff() |
| 299 | + { |
| 300 | + // Arrange |
| 301 | + CloudTasksApi::fake(); |
| 302 | + OpenIdVerificator::fake(); |
| 303 | + Event::fake($this->getJobReleasedAfterExceptionEvent()); |
| 304 | + |
| 305 | + // Act |
| 306 | + $this->dispatch(new FailingJob())->run(); |
| 307 | + |
| 308 | + // Assert |
| 309 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 310 | + return is_null($task->getScheduleTime()); |
| 311 | + }); |
| 312 | + } |
| 313 | + |
| 314 | + /** @test */ |
| 315 | + public function test_backoff_from_queue_config() |
| 316 | + { |
| 317 | + // Arrange |
| 318 | + Carbon::setTestNow(now()->addDay()); |
| 319 | + $this->setConfigValue('backoff', 123); |
| 320 | + CloudTasksApi::fake(); |
| 321 | + OpenIdVerificator::fake(); |
| 322 | + Event::fake($this->getJobReleasedAfterExceptionEvent()); |
| 323 | + |
| 324 | + // Act |
| 325 | + $this->dispatch(new FailingJob())->run(); |
| 326 | + |
| 327 | + // Assert |
| 328 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 329 | + return $task->getScheduleTime() |
| 330 | + && $task->getScheduleTime()->getSeconds() === now()->getTimestamp() + 123; |
| 331 | + }); |
| 332 | + } |
| 333 | + |
| 334 | + /** @test */ |
| 335 | + public function test_backoff_from_job() |
| 336 | + { |
| 337 | + // Arrange |
| 338 | + Carbon::setTestNow(now()->addDay()); |
| 339 | + CloudTasksApi::fake(); |
| 340 | + OpenIdVerificator::fake(); |
| 341 | + Event::fake($this->getJobReleasedAfterExceptionEvent()); |
| 342 | + |
| 343 | + // Act |
| 344 | + $failingJob = new FailingJob(); |
| 345 | + $failingJob->backoff = 123; |
| 346 | + $this->dispatch($failingJob)->run(); |
| 347 | + |
| 348 | + // Assert |
| 349 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 350 | + return $task->getScheduleTime() |
| 351 | + && $task->getScheduleTime()->getSeconds() === now()->getTimestamp() + 123; |
| 352 | + }); |
| 353 | + } |
| 354 | + |
| 355 | + /** @test */ |
| 356 | + public function test_exponential_backoff_from_job_method() |
| 357 | + { |
| 358 | + // Arrange |
| 359 | + Carbon::setTestNow(now()->addDay()); |
| 360 | + CloudTasksApi::fake(); |
| 361 | + OpenIdVerificator::fake(); |
| 362 | + |
| 363 | + // Act |
| 364 | + $releasedJob = $this->dispatch(new FailingJobWithExponentialBackoff()) |
| 365 | + ->runAndGetReleasedJob(); |
| 366 | + $releasedJob = $releasedJob->runAndGetReleasedJob(); |
| 367 | + $releasedJob->run(); |
| 368 | + |
| 369 | + // Assert |
| 370 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 371 | + return $task->getScheduleTime() |
| 372 | + && $task->getScheduleTime()->getSeconds() === now()->getTimestamp() + 50; |
| 373 | + }); |
| 374 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 375 | + return $task->getScheduleTime() |
| 376 | + && $task->getScheduleTime()->getSeconds() === now()->getTimestamp() + 60; |
| 377 | + }); |
| 378 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 379 | + return $task->getScheduleTime() |
| 380 | + && $task->getScheduleTime()->getSeconds() === now()->getTimestamp() + 70; |
| 381 | + }); |
| 382 | + } |
| 383 | + |
| 384 | + /** @test */ |
| 385 | + public function test_failing_method_on_job() |
| 386 | + { |
| 387 | + // Arrange |
| 388 | + CloudTasksApi::fake(); |
| 389 | + CloudTasksApi::partialMock()->shouldReceive('getRetryConfig')->andReturn( |
| 390 | + // -1 is a valid option in Cloud Tasks to indicate there is no max. |
| 391 | + (new RetryConfig())->setMaxAttempts(1) |
| 392 | + ); |
| 393 | + |
| 394 | + OpenIdVerificator::fake(); |
| 395 | + Log::swap(new LogFake()); |
| 396 | + |
| 397 | + // Act |
| 398 | + $this->dispatch(new FailingJob())->run(); |
| 399 | + |
| 400 | + // Assert |
| 401 | + Log::assertLogged('FailingJob:failed'); |
| 402 | + } |
| 403 | + |
| 404 | + /** @test */ |
| 405 | + public function test_queue_before_and_after_hooks() |
| 406 | + { |
| 407 | + // Arrange |
| 408 | + CloudTasksApi::fake(); |
| 409 | + OpenIdVerificator::fake(); |
| 410 | + Log::swap(new LogFake()); |
| 411 | + |
| 412 | + // Act |
| 413 | + Queue::before(function (JobProcessing $event) { |
| 414 | + logger('Queue::before:' . $event->job->payload()['data']['commandName']); |
| 415 | + }); |
| 416 | + Queue::after(function (JobProcessed $event) { |
| 417 | + logger('Queue::after:' . $event->job->payload()['data']['commandName']); |
| 418 | + }); |
| 419 | + $this->dispatch(new SimpleJob())->run(); |
| 420 | + |
| 421 | + // Assert |
| 422 | + Log::assertLogged('Queue::before:Tests\Support\SimpleJob'); |
| 423 | + Log::assertLogged('Queue::after:Tests\Support\SimpleJob'); |
| 424 | + } |
| 425 | + |
| 426 | + /** @test */ |
| 427 | + public function test_queue_looping_hook_not_supported_with_this_package() |
| 428 | + { |
| 429 | + // Arrange |
| 430 | + CloudTasksApi::fake(); |
| 431 | + OpenIdVerificator::fake(); |
| 432 | + Log::swap(new LogFake()); |
| 433 | + |
| 434 | + // Act |
| 435 | + Queue::looping(function () { |
| 436 | + logger('Queue::looping'); |
| 437 | + }); |
| 438 | + $this->dispatch(new SimpleJob())->run(); |
| 439 | + |
| 440 | + // Assert |
| 441 | + Log::assertNotLogged('Queue::looping'); |
| 442 | + } |
| 443 | + |
| 444 | + /** @test */ |
| 445 | + public function test_ignoring_jobs_with_deleted_models() |
| 446 | + { |
| 447 | + // todo |
| 448 | + $this->assertTrue(true); |
| 449 | + } |
289 | 450 | } |
0 commit comments