From 14099c164baff1a50ecd9726db932956d8a54e63 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 14 Oct 2020 21:17:57 -0700 Subject: [PATCH] Add test coverage for schedule service --- .../Schedules/ProcessScheduleServiceTest.php | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/Integration/Services/Schedules/ProcessScheduleServiceTest.php diff --git a/tests/Integration/Services/Schedules/ProcessScheduleServiceTest.php b/tests/Integration/Services/Schedules/ProcessScheduleServiceTest.php new file mode 100644 index 00000000..4941a9bd --- /dev/null +++ b/tests/Integration/Services/Schedules/ProcessScheduleServiceTest.php @@ -0,0 +1,99 @@ +createServerModel(); + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); + + $this->expectException(DisplayException::class); + $this->expectExceptionMessage('Cannot process schedule for task execution: no tasks are registered.'); + + $this->getService()->handle($schedule); + } + + /** + * Test that an error during the schedule update is not persisted to the database. + */ + public function testErrorDuringScheduleDataUpdateDoesNotPersistChanges() + { + $server = $this->createServerModel(); + + /** @var \Pterodactyl\Models\Schedule $schedule */ + $schedule = factory(Schedule::class)->create([ + 'server_id' => $server->id, + 'cron_minute' => 'hodor', // this will break the getNextRunDate() function. + ]); + + /** @var \Pterodactyl\Models\Task $task */ + $task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 1]); + + $this->expectException(InvalidArgumentException::class); + + $this->getService()->handle($schedule); + + $this->assertDatabaseMissing('schedules', ['id' => $schedule->id, 'is_processing' => true]); + $this->assertDatabaseMissing('tasks', ['id' => $task->id, 'is_queued' => true]); + } + + /** + * Test that a job is dispatched as expected using the initial delay. + * + * @param bool $now + * @dataProvider dispatchNowDataProvider + */ + public function testJobCanBeDispatchedWithExpectedInitialDelay($now) + { + $this->swap(Dispatcher::class, $dispatcher = Mockery::mock(Dispatcher::class)); + + $server = $this->createServerModel(); + + /** @var \Pterodactyl\Models\Schedule $schedule */ + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); + + /** @var \Pterodactyl\Models\Task $task */ + $task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'time_offset' => 10, 'sequence_id' => 1]); + + $dispatcher->expects($now ? 'dispatchNow' : 'dispatch')->with(Mockery::on(function (RunTaskJob $job) use ($task) { + return $task->id === $job->task->id && $job->delay === 10; + })); + + $this->getService()->handle($schedule, $now); + + $this->assertDatabaseHas('schedules', ['id' => $schedule->id, 'is_processing' => true]); + $this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => true]); + } + + /** + * @return array + */ + public function dispatchNowDataProvider(): array + { + return [[true], [false]]; + } + + /** + * @return \Pterodactyl\Services\Schedules\ProcessScheduleService + */ + private function getService() + { + return $this->app->make(ProcessScheduleService::class); + } +}