Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\task;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
// We need to keep this here because there is a provider
22
// needing \core\task\adhoc_test_task and cannot move it
23
// to setUpBeforeClass() or similar. Whenever we allow to
24
// autoload fixtures, this can be removed.
25
require_once(__DIR__ . '/../fixtures/task_fixtures.php');
26
 
27
/**
28
 * This file contains the unit tests for the task manager.
29
 *
30
 * @package   core
31
 * @category  test
32
 * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @covers \core\task\manager
35
 */
36
final class manager_test extends \advanced_testcase {
37
    /**
38
     * Data provider for test_get_candidate_adhoc_tasks.
39
     *
40
     * @return array
41
     */
42
    public static function get_candidate_adhoc_tasks_provider(): array {
43
        return [
44
            [
45
                'concurrencylimit' => 5,
46
                'limit' => 100,
47
                'pertasklimits' => [],
48
                'tasks' => [
49
                    new adhoc_test_task(time() - 20, null),
50
                    new adhoc_test_task(time() - 20, null),
51
                    new adhoc_test_task(time() - 20, null),
52
                    new adhoc_test_task(time() - 20, null),
53
                    new adhoc_test_task(time() - 20, null),
54
                ],
55
                'expected' => [
56
                    adhoc_test_task::class,
57
                    adhoc_test_task::class,
58
                    adhoc_test_task::class,
59
                    adhoc_test_task::class,
60
                    adhoc_test_task::class,
61
                ],
62
            ],
63
            [
64
                'concurrencylimit' => 5,
65
                'limit' => 100,
66
                'pertasklimits' => [],
67
                'tasks' => [
68
                    new adhoc_test_task(time() - 20, time()),
69
                    new adhoc_test_task(time() - 20, null),
70
                    new adhoc_test_task(time() - 20, null),
71
                    new adhoc_test_task(time() - 20, null),
72
                    new adhoc_test_task(time() - 20, null),
73
                ],
74
                'expected' => [
75
                    adhoc_test_task::class,
76
                    adhoc_test_task::class,
77
                    adhoc_test_task::class,
78
                    adhoc_test_task::class,
79
                ],
80
            ],
81
            [
82
                'concurrencylimit' => 1,
83
                'limit' => 100,
84
                'pertasklimits' => [],
85
                'tasks' => [
86
                    new adhoc_test_task(time() - 20, time()),
87
                    new adhoc_test_task(time() - 20, null),
88
                    new adhoc_test_task(time() - 20, null),
89
                    new adhoc_test_task(time() - 20, null),
90
                    new adhoc_test_task(time() - 20, null),
91
                ],
92
                'expected' => [],
93
            ],
94
            [
95
                'concurrencylimit' => 2,
96
                'limit' => 100,
97
                'pertasklimits' => [],
98
                'tasks' => [
99
                    new adhoc_test_task(time() - 20, time()),
100
                    new adhoc_test_task(time() - 20, time()),
101
                    new adhoc_test_task(time() - 20, null),
102
                    new adhoc_test_task(time() - 20, null),
103
                    new adhoc_test_task(time() - 20, null),
104
                ],
105
                'expected' => [],
106
            ],
107
            [
108
                'concurrencylimit' => 2,
109
                'limit' => 100,
110
                'pertasklimits' => [],
111
                'tasks' => [
112
                    new adhoc_test_task(time() - 20, time()),
113
                    new adhoc_test_task(time() - 20, time()),
114
                    new adhoc_test2_task(time() - 20, time()),
115
                    new adhoc_test2_task(time() - 20, time()),
116
                    new adhoc_test3_task(time() - 20, null),
117
                ],
118
                'expected' => [adhoc_test3_task::class],
119
            ],
120
            [
121
                'concurrencylimit' => 2,
122
                'limit' => 2,
123
                'pertasklimits' => [],
124
                'tasks' => [
125
                    new adhoc_test_task(time() - 20, null),
126
                    new adhoc_test_task(time() - 20, null),
127
                    new adhoc_test_task(time() - 20, null),
128
                    new adhoc_test2_task(time() - 20, null),
129
                ],
130
                'expected' => [
131
                    adhoc_test_task::class,
132
                    adhoc_test_task::class,
133
                ],
134
            ],
135
            [
136
                'concurrencylimit' => 2,
137
                'limit' => 2,
138
                'pertasklimits' => [],
139
                'tasks' => [
140
                    new adhoc_test_task(time() - 20, time()),
141
                    new adhoc_test_task(time() - 20, time()),
142
                    new adhoc_test_task(time() - 20, null),
143
                    new adhoc_test2_task(time() - 20, null),
144
                ],
145
                'expected' => [
146
                    adhoc_test2_task::class,
147
                ],
148
            ],
149
            [
150
                'concurrencylimit' => 3,
151
                'limit' => 100,
152
                'pertasklimits' => [],
153
                'tasks' => [
154
                    new adhoc_test_task(time() - 20, time()),
155
                    new adhoc_test_task(time() - 20, time()),
156
                    new adhoc_test_task(time() - 20, null),
157
                    new adhoc_test2_task(time() - 20, time()),
158
                    new adhoc_test2_task(time() - 20, time()),
159
                    new adhoc_test2_task(time() - 20, null),
160
                    new adhoc_test3_task(time() - 20, time()),
161
                    new adhoc_test3_task(time() - 20, time()),
162
                    new adhoc_test3_task(time() - 20, null),
163
                    new adhoc_test4_task(time() - 20, time()),
164
                    new adhoc_test4_task(time() - 20, time()),
165
                    new adhoc_test4_task(time() - 20, null),
166
                    new adhoc_test5_task(time() - 20, time()),
167
                    new adhoc_test5_task(time() - 20, time()),
168
                    new adhoc_test5_task(time() - 20, null),
169
                ],
170
                'expected' => [
171
                    adhoc_test_task::class,
172
                    adhoc_test2_task::class,
173
                    adhoc_test3_task::class,
174
                    adhoc_test4_task::class,
175
                    adhoc_test5_task::class,
176
                ],
177
            ],
178
            [
179
                'concurrencylimit' => 3,
180
                'limit' => 100,
181
                'pertasklimits' => [
182
                    'adhoc_test_task' => 2,
183
                    'adhoc_test2_task' => 2,
184
                    'adhoc_test3_task' => 2,
185
                    'adhoc_test4_task' => 2,
186
                    'adhoc_test5_task' => 2,
187
                ],
188
                'tasks' => [
189
                    new adhoc_test_task(time() - 20, time()),
190
                    new adhoc_test_task(time() - 20, time()),
191
                    new adhoc_test_task(time() - 20, null),
192
                    new adhoc_test2_task(time() - 20, time()),
193
                    new adhoc_test2_task(time() - 20, time()),
194
                    new adhoc_test2_task(time() - 20, null),
195
                    new adhoc_test3_task(time() - 20, time()),
196
                    new adhoc_test3_task(time() - 20, time()),
197
                    new adhoc_test3_task(time() - 20, null),
198
                    new adhoc_test4_task(time() - 20, time()),
199
                    new adhoc_test4_task(time() - 20, time()),
200
                    new adhoc_test4_task(time() - 20, null),
201
                    new adhoc_test5_task(time() - 20, time()),
202
                    new adhoc_test5_task(time() - 20, time()),
203
                    new adhoc_test5_task(time() - 20, null),
204
                ],
205
                'expected' => [],
206
            ],
207
        ];
208
    }
209
 
210
    /**
211
     * Test that the candidate adhoc tasks are returned in the right order.
212
     *
213
     * @dataProvider get_candidate_adhoc_tasks_provider
214
     *
215
     * @param int $concurrencylimit The max number of runners each task can consume
216
     * @param int $limit SQL limit
217
     * @param array $pertasklimits Per-task limits
218
     * @param array $tasks Array of tasks to put in DB and retrieve
219
     * @param array $expected Array of expected classnames
220
     */
221
    public function test_get_candidate_adhoc_tasks(
222
        int $concurrencylimit,
223
        int $limit,
224
        array $pertasklimits,
225
        array $tasks,
226
        array $expected
227
    ): void {
228
        $this->resetAfterTest();
229
 
230
        foreach ($tasks as $task) {
231
            manager::queue_adhoc_task($task);
232
        }
233
 
234
        $candidates = manager::get_candidate_adhoc_tasks(time(), $limit, $concurrencylimit, $pertasklimits);
235
        $this->assertEquals(
236
            array_map(
237
                function (string $classname): string {
238
                    return '\\' . $classname;
239
                },
240
                $expected
241
            ),
242
            array_column($candidates, 'classname')
243
        );
244
    }
245
 
246
    /**
247
     * Test that adhoc tasks are set as failed when shutdown is called during execution.
248
     */
249
    public function test_adhoc_task_running_will_fail_when_shutdown(): void {
250
        $this->resetAfterTest();
251
        $this->preventResetByRollback();
252
 
253
        $task1 = new adhoc_test_task();
254
        $task1->set_next_run_time(time() - 20);
255
        manager::queue_adhoc_task($task1);
256
 
257
        $next1 = manager::get_next_adhoc_task(time());
258
        \core\task\manager::adhoc_task_starting($next1);
259
 
260
        self::assertEmpty(manager::get_failed_adhoc_tasks());
261
 
262
        // Trigger shutdown handler.
263
        \core_shutdown_manager::shutdown_handler();
264
 
265
        $failedtasks = manager::get_failed_adhoc_tasks();
266
 
267
        self::assertCount(1, $failedtasks);
268
        self::assertEquals($next1->get_id(), $failedtasks[0]->get_id());
269
    }
270
 
271
    /**
272
     * Test that scheduled tasks are set as failed when shutdown is called during execution.
273
     */
274
    public function test_scheduled_task_running_will_fail_when_shutdown(): void {
275
        global $DB;
276
 
277
        $this->resetAfterTest();
278
        $this->preventResetByRollback();
279
 
280
        // Disable all the tasks, so we can insert our own and be sure it's the only one being run.
281
        $DB->set_field('task_scheduled', 'disabled', 1);
282
 
283
        $task1 = new scheduled_test_task();
284
        $task1->set_minute('*');
285
        $task1->set_next_run_time(time() - HOURSECS);
286
        $DB->insert_record('task_scheduled', manager::record_from_scheduled_task($task1));
287
 
288
        $next1 = \core\task\manager::get_next_scheduled_task(time());
289
        \core\task\manager::scheduled_task_starting($next1);
290
 
291
        $running = manager::get_running_tasks();
292
        $this->assertCount(1, $running);
293
 
294
        // Trigger shutdown handler.
295
        \core_shutdown_manager::shutdown_handler();
296
 
297
        $running = manager::get_running_tasks();
298
        $this->assertCount(0, $running);
299
 
300
        $scheduledtask1 = manager::get_scheduled_task(scheduled_test_task::class);
301
        self::assertGreaterThan($next1->get_fail_delay(), $scheduledtask1->get_fail_delay());
302
    }
303
}