|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Services\Schedules; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use Pterodactyl\Models\Task; |
| 7 | +use InvalidArgumentException; |
| 8 | +use Pterodactyl\Models\Schedule; |
| 9 | +use Illuminate\Support\Facades\Bus; |
| 10 | +use Illuminate\Contracts\Bus\Dispatcher; |
| 11 | +use Pterodactyl\Jobs\Schedule\RunTaskJob; |
| 12 | +use Pterodactyl\Exceptions\DisplayException; |
| 13 | +use Pterodactyl\Tests\Integration\IntegrationTestCase; |
| 14 | +use Pterodactyl\Services\Schedules\ProcessScheduleService; |
| 15 | + |
| 16 | +class ProcessScheduleServiceTest extends IntegrationTestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Test that a schedule with no tasks registered returns an error. |
| 20 | + */ |
| 21 | + public function testScheduleWithNoTasksReturnsException() |
| 22 | + { |
| 23 | + $server = $this->createServerModel(); |
| 24 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 25 | + |
| 26 | + $this->expectException(DisplayException::class); |
| 27 | + $this->expectExceptionMessage('Cannot process schedule for task execution: no tasks are registered.'); |
| 28 | + |
| 29 | + $this->getService()->handle($schedule); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Test that an error during the schedule update is not persisted to the database. |
| 34 | + */ |
| 35 | + public function testErrorDuringScheduleDataUpdateDoesNotPersistChanges() |
| 36 | + { |
| 37 | + $server = $this->createServerModel(); |
| 38 | + |
| 39 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 40 | + $schedule = factory(Schedule::class)->create([ |
| 41 | + 'server_id' => $server->id, |
| 42 | + 'cron_minute' => 'hodor', // this will break the getNextRunDate() function. |
| 43 | + ]); |
| 44 | + |
| 45 | + /** @var \Pterodactyl\Models\Task $task */ |
| 46 | + $task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 1]); |
| 47 | + |
| 48 | + $this->expectException(InvalidArgumentException::class); |
| 49 | + |
| 50 | + $this->getService()->handle($schedule); |
| 51 | + |
| 52 | + $this->assertDatabaseMissing('schedules', ['id' => $schedule->id, 'is_processing' => true]); |
| 53 | + $this->assertDatabaseMissing('tasks', ['id' => $task->id, 'is_queued' => true]); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test that a job is dispatched as expected using the initial delay. |
| 58 | + * |
| 59 | + * @param bool $now |
| 60 | + * @dataProvider dispatchNowDataProvider |
| 61 | + */ |
| 62 | + public function testJobCanBeDispatchedWithExpectedInitialDelay($now) |
| 63 | + { |
| 64 | + $this->swap(Dispatcher::class, $dispatcher = Mockery::mock(Dispatcher::class)); |
| 65 | + |
| 66 | + $server = $this->createServerModel(); |
| 67 | + |
| 68 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 69 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 70 | + |
| 71 | + /** @var \Pterodactyl\Models\Task $task */ |
| 72 | + $task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'time_offset' => 10, 'sequence_id' => 1]); |
| 73 | + |
| 74 | + $dispatcher->expects($now ? 'dispatchNow' : 'dispatch')->with(Mockery::on(function (RunTaskJob $job) use ($task) { |
| 75 | + return $task->id === $job->task->id && $job->delay === 10; |
| 76 | + })); |
| 77 | + |
| 78 | + $this->getService()->handle($schedule, $now); |
| 79 | + |
| 80 | + $this->assertDatabaseHas('schedules', ['id' => $schedule->id, 'is_processing' => true]); |
| 81 | + $this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => true]); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return array |
| 86 | + */ |
| 87 | + public function dispatchNowDataProvider(): array |
| 88 | + { |
| 89 | + return [[true], [false]]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return \Pterodactyl\Services\Schedules\ProcessScheduleService |
| 94 | + */ |
| 95 | + private function getService() |
| 96 | + { |
| 97 | + return $this->app->make(ProcessScheduleService::class); |
| 98 | + } |
| 99 | +} |
0 commit comments