4
4
5
5
use Firebase \JWT \JWT ;
6
6
use Firebase \JWT \SignatureInvalidException ;
7
+ use Google \Cloud \Tasks \V2 \Attempt ;
7
8
use Google \Cloud \Tasks \V2 \CloudTasksClient ;
9
+ use Google \Cloud \Tasks \V2 \Task ;
10
+ use Google \Protobuf \Timestamp ;
8
11
use Illuminate \Cache \Events \CacheHit ;
9
12
use Illuminate \Cache \Events \KeyWritten ;
10
13
use Illuminate \Support \Facades \DB ;
@@ -27,6 +30,8 @@ class TaskHandlerTest extends TestCase
27
30
28
31
private $ request ;
29
32
33
+ private $ cloudTasksClient ;
34
+
30
35
protected function setUp (): void
31
36
{
32
37
parent ::setUp ();
@@ -46,7 +51,8 @@ protected function setUp(): void
46
51
$ googlePublicKey ->shouldReceive ('getPublicKey ' )->andReturnNull ();
47
52
$ googlePublicKey ->shouldReceive ('getKidFromOpenIdToken ' )->andReturnNull ();
48
53
49
- $ cloudTasksClient = Mockery::mock (new CloudTasksClient ());
54
+ $ cloudTasksClient = Mockery::mock (new CloudTasksClient ())->byDefault ();
55
+ $ this ->cloudTasksClient = $ cloudTasksClient ;
50
56
51
57
// Ensure we don't fetch the Queue name and attempts each test...
52
58
$ cloudTasksClient ->shouldReceive ('queueName ' )->andReturn ('my-queue ' );
@@ -56,9 +62,25 @@ public function getRetryConfig() {
56
62
public function getMaxAttempts () {
57
63
return 3 ;
58
64
}
65
+
66
+ public function getMaxRetryDuration () {
67
+ return new class {
68
+ public function getSeconds () {
69
+ return 30 ;
70
+ }
71
+ };
72
+ }
59
73
};
60
74
}
61
75
});
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 ();
62
84
63
85
$ this ->handler = new TaskHandler (
64
86
$ cloudTasksClient ,
@@ -171,6 +193,58 @@ public function after_max_attempts_it_will_log_to_failed_table()
171
193
]);
172
194
}
173
195
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
+
174
248
private function simpleJob ()
175
249
{
176
250
return json_decode (file_get_contents (__DIR__ . '/Support/test-job-payload.json ' ), true );
0 commit comments