Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
/**
20
 * This file contains unit tests for the 'task running' data.
21
 *
22
 * @package   core
23
 * @category  test
24
 * @copyright 2019 The Open University
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
final class running_test extends \advanced_testcase {
28
    public static function setUpBeforeClass(): void {
29
        require_once(__DIR__ . '/../fixtures/task_fixtures.php');
30
    }
31
 
32
    /**
33
     * Test for ad-hoc tasks.
34
     */
11 efrain 35
    public function test_adhoc_task_running(): void {
1 efrain 36
        $this->resetAfterTest();
37
 
38
        // Specify lock factory. The reason is that Postgres locks don't work within a single
39
        // process (i.e. if you try to get a lock that you already locked, it will just let you)
40
        // which is usually OK but not here where we are simulating running two tasks at once in
41
        // the same process.
42
        set_config('lock_factory', '\core\lock\db_record_lock_factory');
43
 
44
        // Create and queue 2 new ad-hoc tasks.
45
        $task1 = new adhoc_test_task();
46
        $task1->set_next_run_time(time() - 20);
47
        manager::queue_adhoc_task($task1);
48
        $task2 = new adhoc_test2_task();
49
        $task2->set_next_run_time(time() - 10);
50
        manager::queue_adhoc_task($task2);
51
 
52
        // Check no tasks are marked running.
53
        $running = manager::get_running_tasks();
54
        $this->assertEmpty($running);
55
 
56
        // Mark the first task running and check results.
57
        $before = time();
58
        $next1 = manager::get_next_adhoc_task(time());
59
        manager::adhoc_task_starting($next1);
60
        $after = time();
61
        $running = manager::get_running_tasks();
62
        $this->assertCount(1, $running);
63
        foreach ($running as $item) {
64
            $this->assertEquals('adhoc', $item->type);
65
            $this->assertLessThanOrEqual($after, $item->timestarted);
66
            $this->assertGreaterThanOrEqual($before, $item->timestarted);
67
        }
68
 
69
        // Mark the second task running and check results.
70
        $next2 = manager::get_next_adhoc_task(time());
71
        manager::adhoc_task_starting($next2);
72
        $running = manager::get_running_tasks();
73
        $this->assertCount(2, $running);
74
 
75
        // Second task completes successfully.
76
        manager::adhoc_task_complete($next2);
77
        $running = manager::get_running_tasks();
78
        $this->assertCount(1, $running);
79
 
80
        // First task fails.
81
        manager::adhoc_task_failed($next1);
82
        $running = manager::get_running_tasks();
83
        $this->assertCount(0, $running);
84
    }
85
 
86
    /**
87
     * Test for scheduled tasks.
88
     */
11 efrain 89
    public function test_scheduled_task_running(): void {
1 efrain 90
        global $DB;
91
        $this->resetAfterTest();
92
 
93
        // Check no tasks are marked running.
94
        $running = manager::get_running_tasks();
95
        $this->assertEmpty($running);
96
 
97
        // Disable all the tasks, except two, and set those two due to run.
98
        $DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ? AND classname != ?',
99
                ['\core\task\session_cleanup_task', '\core\task\file_trash_cleanup_task']);
100
        $DB->set_field('task_scheduled', 'nextruntime', 1,
101
                ['classname' => '\core\task\session_cleanup_task']);
102
        $DB->set_field('task_scheduled', 'nextruntime', 1,
103
                ['classname' => '\core\task\file_trash_cleanup_task']);
104
        $DB->set_field('task_scheduled', 'lastruntime', time() - 1000,
105
                ['classname' => '\core\task\session_cleanup_task']);
106
        $DB->set_field('task_scheduled', 'lastruntime', time() - 500,
107
                ['classname' => '\core\task\file_trash_cleanup_task']);
108
 
109
        // Get the first task and start it off.
110
        $next1 = manager::get_next_scheduled_task(time());
111
        $before = time();
112
        manager::scheduled_task_starting($next1);
113
        $after = time();
114
        $running = manager::get_running_tasks();
115
        $this->assertCount(1, $running);
116
        foreach ($running as $item) {
117
            $this->assertLessThanOrEqual($after, $item->timestarted);
118
            $this->assertGreaterThanOrEqual($before, $item->timestarted);
119
            $this->assertEquals('\core\task\session_cleanup_task', $item->classname);
120
        }
121
 
122
        // Mark the second task running and check results. We have to change the times so the other
123
        // one comes up first, otherwise it repeats the same one.
124
        $DB->set_field('task_scheduled', 'lastruntime', time() - 1500,
125
                ['classname' => '\core\task\file_trash_cleanup_task']);
126
 
127
        // Make sure that there is a time gap between task to sort them as expected.
128
        sleep(1);
129
        $next2 = manager::get_next_scheduled_task(time());
130
        manager::scheduled_task_starting($next2);
131
 
132
        // Check default sorting by timestarted.
133
        $running = manager::get_running_tasks();
134
        $this->assertCount(2, $running);
135
        $item = array_shift($running);
136
        $this->assertEquals('\core\task\session_cleanup_task', $item->classname);
137
        $item = array_shift($running);
138
        $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname);
139
 
140
        // Check sorting by time ASC.
141
        $running = manager::get_running_tasks('time ASC');
142
        $this->assertCount(2, $running);
143
        $item = array_shift($running);
144
        $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname);
145
        $item = array_shift($running);
146
        $this->assertEquals('\core\task\session_cleanup_task', $item->classname);
147
 
148
        // Complete the file trash one.
149
        manager::scheduled_task_complete($next2);
150
        $running = manager::get_running_tasks();
151
        $this->assertCount(1, $running);
152
 
153
        // Other task fails.
154
        manager::scheduled_task_failed($next1);
155
        $running = manager::get_running_tasks();
156
        $this->assertCount(0, $running);
157
    }
158
}