|
7 | 7 | use Google\Cloud\Tasks\V2\HttpMethod; |
8 | 8 | use Google\Cloud\Tasks\V2\Task; |
9 | 9 | use Illuminate\Queue\Events\JobQueued; |
| 10 | +use Illuminate\Support\Carbon; |
10 | 11 | use Illuminate\Support\Facades\DB; |
11 | 12 | use Illuminate\Support\Facades\Event; |
12 | 13 | use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi; |
| 14 | +use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased; |
| 15 | +use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator; |
13 | 16 | use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler; |
14 | 17 | use Tests\Support\FailingJob; |
| 18 | +use Tests\Support\JobThatWillBeReleased; |
15 | 19 | use Tests\Support\SimpleJob; |
16 | 20 |
|
17 | 21 | class QueueTest extends TestCase |
@@ -210,4 +214,76 @@ public function it_can_dispatch_after_commit_through_config() |
210 | 214 | return $event->job instanceof SimpleJob; |
211 | 215 | }); |
212 | 216 | } |
| 217 | + |
| 218 | + /** |
| 219 | + * @test |
| 220 | + */ |
| 221 | + public function jobs_can_be_released() |
| 222 | + { |
| 223 | + // Arrange |
| 224 | + CloudTasksApi::fake(); |
| 225 | + OpenIdVerificator::fake(); |
| 226 | + Event::fake([ |
| 227 | + $this->getJobReleasedAfterExceptionEvent(), |
| 228 | + JobReleased::class, |
| 229 | + ]); |
| 230 | + |
| 231 | + // Act |
| 232 | + $this->dispatch(new JobThatWillBeReleased())->run(); |
| 233 | + |
| 234 | + // Assert |
| 235 | + Event::assertNotDispatched($this->getJobReleasedAfterExceptionEvent()); |
| 236 | + CloudTasksApi::assertDeletedTaskCount(1); |
| 237 | + $releasedJob = null; |
| 238 | + Event::assertDispatched(JobReleased::class, function (JobReleased $event) use (&$releasedJob) { |
| 239 | + $releasedJob = $event->job; |
| 240 | + return true; |
| 241 | + }); |
| 242 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 243 | + $body = $task->getHttpRequest()->getBody(); |
| 244 | + $decoded = json_decode($body, true); |
| 245 | + return $decoded['data']['commandName'] === 'Tests\\Support\\JobThatWillBeReleased' |
| 246 | + && $decoded['internal']['attempts'] === 1; |
| 247 | + }); |
| 248 | + |
| 249 | + $this->runFromPayload($releasedJob->getRawBody()); |
| 250 | + |
| 251 | + CloudTasksApi::assertDeletedTaskCount(2); |
| 252 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 253 | + $body = $task->getHttpRequest()->getBody(); |
| 254 | + $decoded = json_decode($body, true); |
| 255 | + return $decoded['data']['commandName'] === 'Tests\\Support\\JobThatWillBeReleased' |
| 256 | + && $decoded['internal']['attempts'] === 2; |
| 257 | + }); |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * @test |
| 262 | + */ |
| 263 | + public function jobs_can_be_released_with_a_delay() |
| 264 | + { |
| 265 | + // Arrange |
| 266 | + CloudTasksApi::fake(); |
| 267 | + OpenIdVerificator::fake(); |
| 268 | + Event::fake([ |
| 269 | + $this->getJobReleasedAfterExceptionEvent(), |
| 270 | + JobReleased::class, |
| 271 | + ]); |
| 272 | + Carbon::setTestNow(now()->addDay()); |
| 273 | + |
| 274 | + // Act |
| 275 | + $this->dispatch(new JobThatWillBeReleased(15))->run(); |
| 276 | + |
| 277 | + // Assert |
| 278 | + CloudTasksApi::assertTaskCreated(function (Task $task) { |
| 279 | + $body = $task->getHttpRequest()->getBody(); |
| 280 | + $decoded = json_decode($body, true); |
| 281 | + |
| 282 | + $scheduleTime = $task->getScheduleTime() ? $task->getScheduleTime()->getSeconds() : null; |
| 283 | + |
| 284 | + return $decoded['data']['commandName'] === 'Tests\\Support\\JobThatWillBeReleased' |
| 285 | + && $decoded['internal']['attempts'] === 1 |
| 286 | + && $scheduleTime === now()->getTimestamp() + 15; |
| 287 | + }); |
| 288 | + } |
213 | 289 | } |
0 commit comments