44
55use Firebase \JWT \JWT ;
66use Firebase \JWT \SignatureInvalidException ;
7+ use Google \Cloud \Tasks \V2 \Attempt ;
78use Google \Cloud \Tasks \V2 \CloudTasksClient ;
9+ use Google \Cloud \Tasks \V2 \Task ;
10+ use Google \Protobuf \Timestamp ;
811use Illuminate \Cache \Events \CacheHit ;
912use Illuminate \Cache \Events \KeyWritten ;
1013use Illuminate \Support \Facades \DB ;
@@ -27,6 +30,8 @@ class TaskHandlerTest extends TestCase
2730
2831 private $ request ;
2932
33+ private $ cloudTasksClient ;
34+
3035 protected function setUp (): void
3136 {
3237 parent ::setUp ();
@@ -46,7 +51,8 @@ protected function setUp(): void
4651 $ googlePublicKey ->shouldReceive ('getPublicKey ' )->andReturnNull ();
4752 $ googlePublicKey ->shouldReceive ('getKidFromOpenIdToken ' )->andReturnNull ();
4853
49- $ cloudTasksClient = Mockery::mock (new CloudTasksClient ());
54+ $ cloudTasksClient = Mockery::mock (new CloudTasksClient ())->byDefault ();
55+ $ this ->cloudTasksClient = $ cloudTasksClient ;
5056
5157 // Ensure we don't fetch the Queue name and attempts each test...
5258 $ cloudTasksClient ->shouldReceive ('queueName ' )->andReturn ('my-queue ' );
@@ -56,9 +62,25 @@ public function getRetryConfig() {
5662 public function getMaxAttempts () {
5763 return 3 ;
5864 }
65+
66+ public function getMaxRetryDuration () {
67+ return new class {
68+ public function getSeconds () {
69+ return 30 ;
70+ }
71+ };
72+ }
5973 };
6074 }
6175 });
76+ $ cloudTasksClient ->shouldReceive ('taskName ' )->andReturn ('FakeTaskName ' );
77+ $ cloudTasksClient ->shouldReceive ('getTask ' )->byDefault ()->andReturn (new class {
78+ public function getFirstAttempt () {
79+ return null ;
80+ }
81+ });
82+
83+ $ cloudTasksClient ->shouldReceive ('deleteTask ' )->andReturnNull ();
6284
6385 $ this ->handler = new TaskHandler (
6486 $ cloudTasksClient ,
@@ -171,6 +193,58 @@ public function after_max_attempts_it_will_log_to_failed_table()
171193 ]);
172194 }
173195
196+ /** @test */
197+ public function after_max_attempts_it_will_delete_the_task ()
198+ {
199+ $ this ->request ->headers ->add (['X-CloudTasks-TaskRetryCount ' => 2 ]);
200+
201+ rescue (function () {
202+ $ this ->handler ->handle ($ this ->failingJob ());
203+ });
204+
205+ $ this ->cloudTasksClient ->shouldHaveReceived ('deleteTask ' )->once ();
206+ }
207+
208+ /** @test */
209+ public function after_max_retry_until_it_will_delete_the_task ()
210+ {
211+ $ this ->request ->headers ->add (['X-CloudTasks-TaskRetryCount ' => 1 ]);
212+
213+ $ this ->cloudTasksClient
214+ ->shouldReceive ('getTask ' )
215+ ->byDefault ()
216+ ->andReturn (new class {
217+ public function getFirstAttempt () {
218+ return (new Attempt ())
219+ ->setDispatchTime (new Timestamp ([
220+ 'seconds ' => time () - 29 ,
221+ ]));
222+ }
223+ });
224+
225+ rescue (function () {
226+ $ this ->handler ->handle ($ this ->failingJob ());
227+ });
228+
229+ $ this ->cloudTasksClient ->shouldNotHaveReceived ('deleteTask ' );
230+
231+ $ this ->cloudTasksClient ->shouldReceive ('getTask ' )
232+ ->andReturn (new class {
233+ public function getFirstAttempt () {
234+ return (new Attempt ())
235+ ->setDispatchTime (new Timestamp ([
236+ 'seconds ' => time () - 30 ,
237+ ]));
238+ }
239+ });
240+
241+ rescue (function () {
242+ $ this ->handler ->handle ($ this ->failingJob ());
243+ });
244+
245+ $ this ->cloudTasksClient ->shouldHaveReceived ('deleteTask ' )->once ();
246+ }
247+
174248 private function simpleJob ()
175249 {
176250 return json_decode (file_get_contents (__DIR__ . '/Support/test-job-payload.json ' ), true );
0 commit comments