forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessScheduleServiceTest.php
More file actions
169 lines (135 loc) · 6.21 KB
/
ProcessScheduleServiceTest.php
File metadata and controls
169 lines (135 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Pterodactyl\Tests\Integration\Services\Schedules;
use Mockery;
use Exception;
use Carbon\CarbonImmutable;
use Pterodactyl\Models\Task;
use InvalidArgumentException;
use Pterodactyl\Models\Schedule;
use Illuminate\Support\Facades\Bus;
use Illuminate\Contracts\Bus\Dispatcher;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Services\Schedules\ProcessScheduleService;
class ProcessScheduleServiceTest extends IntegrationTestCase
{
/**
* Test that a schedule with no tasks registered returns an error.
*/
public function testScheduleWithNoTasksReturnsException()
{
$server = $this->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)
{
Bus::fake();
$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]);
$this->getService()->handle($schedule, $now);
Bus::assertDispatched(RunTaskJob::class, function ($job) use ($now, $task) {
$this->assertInstanceOf(RunTaskJob::class, $job);
$this->assertSame($task->id, $job->task->id);
// Jobs using dispatchNow should not have a delay associated with them.
$this->assertSame($now ? null : 10, $job->delay);
return true;
});
$this->assertDatabaseHas('schedules', ['id' => $schedule->id, 'is_processing' => true]);
$this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => true]);
}
/**
* Test that even if a schedule's task sequence gets messed up the first task based on
* the ascending order of tasks is used.
*
* @see https://github.com/pterodactyl/panel/issues/2534
*/
public function testFirstSequenceTaskIsFound()
{
Bus::fake();
$server = $this->createServerModel();
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
/** @var \Pterodactyl\Models\Task $task */
$task2 = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 4]);
$task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 2]);
$task3 = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 3]);
$this->getService()->handle($schedule);
Bus::assertDispatched(RunTaskJob::class, function (RunTaskJob $job) use ($task) {
return $task->id === $job->task->id;
});
$this->assertDatabaseHas('schedules', ['id' => $schedule->id, 'is_processing' => true]);
$this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => true]);
$this->assertDatabaseHas('tasks', ['id' => $task2->id, 'is_queued' => false]);
$this->assertDatabaseHas('tasks', ['id' => $task3->id, 'is_queued' => false]);
}
/**
* Tests that a task's processing state is reset correctly if using "dispatchNow" and there is
* an exception encountered while running it.
*
* @see https://github.com/pterodactyl/panel/issues/2550
*/
public function testTaskDispatchedNowIsResetProperlyIfErrorIsEncountered()
{
$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, 'last_run_at' => null]);
/** @var \Pterodactyl\Models\Task $task */
$task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 1]);
$dispatcher->expects('dispatchNow')->andThrows(new Exception('Test thrown exception'));
$this->expectException(Exception::class);
$this->expectExceptionMessage('Test thrown exception');
$this->getService()->handle($schedule, true);
$this->assertDatabaseHas('schedules', [
'id' => $schedule->id,
'is_processing' => false,
'last_run_at' => CarbonImmutable::now()->toAtomString(),
]);
$this->assertDatabaseHas('tasks', ['id' => $task->id, 'is_queued' => false]);
}
/**
* @return array
*/
public function dispatchNowDataProvider(): array
{
return [[true], [false]];
}
/**
* @return \Pterodactyl\Services\Schedules\ProcessScheduleService
*/
private function getService()
{
return $this->app->make(ProcessScheduleService::class);
}
}