1818use Tests \Support \FailingJobWithMaxTries ;
1919use Tests \Support \FailingJobWithMaxTriesAndRetryUntil ;
2020use Tests \Support \FailingJobWithRetryUntil ;
21+ use Tests \Support \FailingJobWithUnlimitedTries ;
2122use Tests \Support \SimpleJob ;
2223
2324class 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}
0 commit comments