Skip to content

Commit 00b3343

Browse files
committed
Refactor some tests
1 parent 9ecafb6 commit 00b3343

File tree

5 files changed

+58
-48
lines changed

5 files changed

+58
-48
lines changed

src/CloudTasksQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private function taskName(string $queueName, array $payload): string
175175
$this->config['project'],
176176
$this->config['location'],
177177
$queueName,
178-
$displayName.'-'.$payload['uuid'].'-'.Carbon::now()->getTimeStampMs(),
178+
$displayName.'-'. bin2hex(random_bytes(8)),
179179
);
180180
}
181181

tests/QueueTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,16 @@ public function it_adds_a_task_name_based_on_the_display_name()
463463
{
464464
// Arrange
465465
CloudTasksApi::fake();
466-
Carbon::setTestNow(Carbon::create(2023, 6, 1, 20, 2, 37));
467466

468467
// Act
469468
$this->dispatch((new SimpleJob()));
470469

471470
// Assert
472-
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
473-
$uuid = \Safe\json_decode($task->getHttpRequest()->getBody(), true)['uuid'];
474-
475-
return $task->getName() === 'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/Tests-Support-SimpleJob-'.$uuid.'-1685649757000';
471+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
472+
return str_starts_with(
473+
$task->getName(),
474+
'projects/my-test-project/locations/europe-west6/queues/barbequeue/tasks/Tests-Support-SimpleJob'
475+
);
476476
});
477477
}
478478
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
class FailingJobWithUnlimitedTries extends FailingJob
8+
{
9+
public $tries = 500;
10+
}

tests/TaskHandlerTest.php

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Tests\Support\FailingJobWithMaxTries;
1919
use Tests\Support\FailingJobWithMaxTriesAndRetryUntil;
2020
use Tests\Support\FailingJobWithRetryUntil;
21+
use Tests\Support\FailingJobWithUnlimitedTries;
2122
use Tests\Support\SimpleJob;
2223

2324
class TaskHandlerTest extends TestCase
@@ -146,16 +147,16 @@ public function after_max_retry_until_it_will_log_to_failed_table_and_delete_the
146147
*/
147148
public function test_unlimited_max_attempts()
148149
{
149-
// Arrange
150-
151150
// Act
152-
$job = $this->dispatch(new FailingJob());
153-
foreach (range(1, 50) as $attempt) {
154-
$job->run();
155-
CloudTasksApi::assertDeletedTaskCount($attempt);
156-
CloudTasksApi::assertTaskDeleted($job->task->getName());
157-
$this->assertDatabaseCount('failed_jobs', 0);
151+
$job = $this->dispatch(new FailingJobWithUnlimitedTries());
152+
153+
foreach (range(0, 50) as $attempt) {
154+
usleep(1000);
155+
$job = $job->runAndGetReleasedJob();
158156
}
157+
158+
// -1 because the last job is not run.
159+
CloudTasksApi::assertDeletedTaskCount(51);
159160
}
160161

161162
/**
@@ -164,7 +165,6 @@ public function test_unlimited_max_attempts()
164165
public function test_max_attempts_in_combination_with_retry_until()
165166
{
166167
// Arrange
167-
168168
$this->travelTo('2020-01-01 00:00:00');
169169

170170
$job = $this->dispatch(new FailingJobWithMaxTriesAndRetryUntil());
@@ -174,13 +174,14 @@ public function test_max_attempts_in_combination_with_retry_until()
174174
// Act & Assert
175175

176176
// The max attempts is 3, but the retryUntil is set to 5 minutes from now.
177-
// So when we attempt the job 10 times, it should still not fail.
178-
foreach (range(1, 10) as $attempt) {
179-
$job = $job->runAndGetReleasedJob();
180-
CloudTasksApi::assertDeletedTaskCount($attempt);
181-
CloudTasksApi::assertTaskDeleted($job->task->getName());
182-
$this->assertDatabaseCount('failed_jobs', 0);
183-
}
177+
// So when we attempt the job 4 times, it should still not fail.
178+
$job = $job
179+
->runAndGetReleasedJob()
180+
->runAndGetReleasedJob()
181+
->runAndGetReleasedJob()
182+
->runAndGetReleasedJob();
183+
184+
$this->assertDatabaseCount('failed_jobs', 0);
184185

185186
// Now we travel to 5 minutes from now, and the job should fail.
186187
$this->travelTo('2020-01-01 00:05:00');
@@ -244,16 +245,16 @@ public function attempts_are_tracked_internally()
244245

245246
// Act & Assert
246247
$job = $this->dispatch(new FailingJob());
247-
$job->run();
248-
$releasedJob = null;
248+
249+
$released = $job->runAndGetReleasedJob();
249250

250251
Event::assertDispatched(JobReleasedAfterException::class, function ($event) use (&$releasedJob) {
251252
$releasedJob = $event->job->getRawBody();
252253

253254
return $event->job->attempts() === 1;
254255
});
255256

256-
$this->runFromPayload($releasedJob);
257+
$released->run();
257258

258259
Event::assertDispatched(JobReleasedAfterException::class, function ($event) {
259260
return $event->job->attempts() === 2;
@@ -270,23 +271,9 @@ public function retried_jobs_get_a_new_name()
270271
CloudTasksApi::fake();
271272

272273
// Act & Assert
273-
Carbon::setTestNow(Carbon::createFromTimestamp(1685035628));
274-
$job = $this->dispatch(new FailingJob());
275-
Carbon::setTestNow(Carbon::createFromTimestamp(1685035629));
276-
277-
$job->run();
278-
279-
// Assert
280-
CloudTasksApi::assertCreatedTaskCount(2);
281-
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
282-
[$timestamp] = array_reverse(explode('-', $task->getName()));
283-
284-
return $timestamp === '1685035628000';
285-
});
286-
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
287-
[$timestamp] = array_reverse(explode('-', $task->getName()));
288-
289-
return $timestamp === '1685035629000';
290-
});
274+
$this->assertCount(0, $this->createdTasks);
275+
$this->dispatch(new FailingJob())->runAndGetReleasedJob();
276+
$this->assertCount(2, $this->createdTasks);
277+
$this->assertNotEquals($this->createdTasks[0]->getName(), $this->createdTasks[1]->getName());
291278
}
292279
}

tests/TestCase.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ class TestCase extends \Orchestra\Testbench\TestCase
2727

2828
public string $releasedJobPayload;
2929

30+
public array $createdTasks = [];
31+
3032
protected function setUp(): void
3133
{
3234
parent::setUp();
3335

3436
$this->withFactories(__DIR__.'/../factories');
3537

38+
Event::listen(TaskCreated::class, function (TaskCreated $event) {
39+
$this->createdTasks[] = $event->task;
40+
});
41+
3642
Event::listen(
3743
JobReleasedAfterException::class,
3844
function ($event) {
@@ -123,13 +129,11 @@ protected function setConfigValue($key, $value)
123129
public function dispatch($job)
124130
{
125131
$payload = null;
126-
$payloadAsArray = [];
127132
$task = null;
128133

129-
Event::listen(TaskCreated::class, function (TaskCreated $event) use (&$payload, &$payloadAsArray, &$task) {
134+
Event::listen(TaskCreated::class, function (TaskCreated $event) use (&$payload, &$task) {
130135
$request = $event->task->getHttpRequest() ?? $event->task->getAppEngineHttpRequest();
131136
$payload = $request->getBody();
132-
$payloadAsArray = json_decode($payload, true);
133137
$task = $event->task;
134138
});
135139

@@ -168,9 +172,18 @@ public function runAndGetReleasedJob(): self
168172
app(TaskHandler::class)->handle($this->payload);
169173
});
170174

175+
$releasedTask = end($this->testCase->createdTasks);
176+
177+
if (! $releasedTask) {
178+
$this->testCase->fail('No task was released.');
179+
}
180+
181+
$payload = $releasedTask->getAppEngineHttpRequest()?->getBody()
182+
?: $releasedTask->getHttpRequest()->getBody();
183+
171184
return new self(
172-
$this->testCase->releasedJobPayload,
173-
$this->task,
185+
$payload,
186+
$releasedTask,
174187
$this->testCase
175188
);
176189
}

0 commit comments

Comments
 (0)