Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403]

   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  require_once (__DIR__ . '/../fixtures/task_fixtures.php');
  21  
  22  /**
  23   * This file contains the unit tests for the task manager.
  24   *
  25   * @package   core
  26   * @category  test
  27   * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class manager_test extends \advanced_testcase {
  31  
  32      /**
  33       * Data provider for test_get_candidate_adhoc_tasks.
  34       *
  35       * @return array
  36       */
  37      public function get_candidate_adhoc_tasks_provider(): array {
  38          return [
  39              [
  40                  'concurrencylimit' => 5,
  41                  'limit' => 100,
  42                  'pertasklimits' => [],
  43                  'tasks' => [
  44                      new adhoc_test_task(time() - 20, null),
  45                      new adhoc_test_task(time() - 20, null),
  46                      new adhoc_test_task(time() - 20, null),
  47                      new adhoc_test_task(time() - 20, null),
  48                      new adhoc_test_task(time() - 20, null)
  49                  ],
  50                  'expected' => [
  51                      adhoc_test_task::class,
  52                      adhoc_test_task::class,
  53                      adhoc_test_task::class,
  54                      adhoc_test_task::class,
  55                      adhoc_test_task::class
  56                  ]
  57              ],
  58              [
  59                  'concurrencylimit' => 5,
  60                  'limit' => 100,
  61                  'pertasklimits' => [],
  62                  'tasks' => [
  63                      new adhoc_test_task(time() - 20, time()),
  64                      new adhoc_test_task(time() - 20, null),
  65                      new adhoc_test_task(time() - 20, null),
  66                      new adhoc_test_task(time() - 20, null),
  67                      new adhoc_test_task(time() - 20, null)
  68                  ],
  69                  'expected' => [
  70                      adhoc_test_task::class,
  71                      adhoc_test_task::class,
  72                      adhoc_test_task::class,
  73                      adhoc_test_task::class
  74                  ]
  75              ],
  76              [
  77                  'concurrencylimit' => 1,
  78                  'limit' => 100,
  79                  'pertasklimits' => [],
  80                  'tasks' => [
  81                      new adhoc_test_task(time() - 20, time()),
  82                      new adhoc_test_task(time() - 20, null),
  83                      new adhoc_test_task(time() - 20, null),
  84                      new adhoc_test_task(time() - 20, null),
  85                      new adhoc_test_task(time() - 20, null)
  86                  ],
  87                  'expected' => []
  88              ],
  89              [
  90                  'concurrencylimit' => 2,
  91                  'limit' => 100,
  92                  'pertasklimits' => [],
  93                  'tasks' => [
  94                      new adhoc_test_task(time() - 20, time()),
  95                      new adhoc_test_task(time() - 20, time()),
  96                      new adhoc_test_task(time() - 20, null),
  97                      new adhoc_test_task(time() - 20, null),
  98                      new adhoc_test_task(time() - 20, null)
  99                  ],
 100                  'expected' => []
 101              ],
 102              [
 103                  'concurrencylimit' => 2,
 104                  'limit' => 100,
 105                  'pertasklimits' => [],
 106                  'tasks' => [
 107                      new adhoc_test_task(time() - 20, time()),
 108                      new adhoc_test_task(time() - 20, time()),
 109                      new adhoc_test2_task(time() - 20, time()),
 110                      new adhoc_test2_task(time() - 20, time()),
 111                      new adhoc_test3_task(time() - 20, null)
 112                  ],
 113                  'expected' => [adhoc_test3_task::class]
 114              ],
 115              [
 116                  'concurrencylimit' => 2,
 117                  'limit' => 2,
 118                  'pertasklimits' => [],
 119                  'tasks' => [
 120                      new adhoc_test_task(time() - 20, null),
 121                      new adhoc_test_task(time() - 20, null),
 122                      new adhoc_test_task(time() - 20, null),
 123                      new adhoc_test2_task(time() - 20, null),
 124                  ],
 125                  'expected' => [
 126                      adhoc_test_task::class,
 127                      adhoc_test_task::class
 128                  ]
 129              ],
 130              [
 131                  'concurrencylimit' => 2,
 132                  'limit' => 2,
 133                  'pertasklimits' => [],
 134                  'tasks' => [
 135                      new adhoc_test_task(time() - 20, time()),
 136                      new adhoc_test_task(time() - 20, time()),
 137                      new adhoc_test_task(time() - 20, null),
 138                      new adhoc_test2_task(time() - 20, null),
 139                  ],
 140                  'expected' => [
 141                      adhoc_test2_task::class
 142                  ]
 143              ],
 144              [
 145                  'concurrencylimit' => 3,
 146                  'limit' => 100,
 147                  'pertasklimits' => [],
 148                  'tasks' => [
 149                      new adhoc_test_task(time() - 20, time()),
 150                      new adhoc_test_task(time() - 20, time()),
 151                      new adhoc_test_task(time() - 20, null),
 152                      new adhoc_test2_task(time() - 20, time()),
 153                      new adhoc_test2_task(time() - 20, time()),
 154                      new adhoc_test2_task(time() - 20, null),
 155                      new adhoc_test3_task(time() - 20, time()),
 156                      new adhoc_test3_task(time() - 20, time()),
 157                      new adhoc_test3_task(time() - 20, null),
 158                      new adhoc_test4_task(time() - 20, time()),
 159                      new adhoc_test4_task(time() - 20, time()),
 160                      new adhoc_test4_task(time() - 20, null),
 161                      new adhoc_test5_task(time() - 20, time()),
 162                      new adhoc_test5_task(time() - 20, time()),
 163                      new adhoc_test5_task(time() - 20, null),
 164                  ],
 165                  'expected' => [
 166                      adhoc_test_task::class,
 167                      adhoc_test2_task::class,
 168                      adhoc_test3_task::class,
 169                      adhoc_test4_task::class,
 170                      adhoc_test5_task::class
 171                  ]
 172              ],
 173              [
 174                  'concurrencylimit' => 3,
 175                  'limit' => 100,
 176                  'pertasklimits' => [
 177                      'adhoc_test_task' => 2,
 178                      'adhoc_test2_task' => 2,
 179                      'adhoc_test3_task' => 2,
 180                      'adhoc_test4_task' => 2,
 181                      'adhoc_test5_task' => 2
 182                  ],
 183                  'tasks' => [
 184                      new adhoc_test_task(time() - 20, time()),
 185                      new adhoc_test_task(time() - 20, time()),
 186                      new adhoc_test_task(time() - 20, null),
 187                      new adhoc_test2_task(time() - 20, time()),
 188                      new adhoc_test2_task(time() - 20, time()),
 189                      new adhoc_test2_task(time() - 20, null),
 190                      new adhoc_test3_task(time() - 20, time()),
 191                      new adhoc_test3_task(time() - 20, time()),
 192                      new adhoc_test3_task(time() - 20, null),
 193                      new adhoc_test4_task(time() - 20, time()),
 194                      new adhoc_test4_task(time() - 20, time()),
 195                      new adhoc_test4_task(time() - 20, null),
 196                      new adhoc_test5_task(time() - 20, time()),
 197                      new adhoc_test5_task(time() - 20, time()),
 198                      new adhoc_test5_task(time() - 20, null),
 199                  ],
 200                  'expected' => []
 201              ]
 202          ];
 203      }
 204  
 205      /**
 206       * Test that the candidate adhoc tasks are returned in the right order.
 207       *
 208       * @dataProvider get_candidate_adhoc_tasks_provider
 209       *
 210       * @param int $concurrencylimit The max number of runners each task can consume
 211       * @param int $limit SQL limit
 212       * @param array $pertasklimits Per-task limits
 213       * @param array $tasks Array of tasks to put in DB and retrieve
 214       * @param array $expected Array of expected classnames
 215       * @return void
 216       * @covers \manager::get_candidate_adhoc_tasks
 217       */
 218      public function test_get_candidate_adhoc_tasks(
 219          int $concurrencylimit,
 220          int $limit,
 221          array $pertasklimits,
 222          array $tasks,
 223          array $expected
 224      ): void {
 225          $this->resetAfterTest();
 226  
 227          foreach ($tasks as $task) {
 228              manager::queue_adhoc_task($task);
 229          }
 230  
 231          $candidates = manager::get_candidate_adhoc_tasks(time(), $limit, $concurrencylimit, $pertasklimits);
 232          $this->assertEquals(
 233              array_map(
 234                  function(string $classname): string {
 235                      return '\\' . $classname;
 236                  },
 237                  $expected
 238              ),
 239              array_column($candidates, 'classname')
 240          );
 241      }
 242  }