Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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  /**
  18   * This file contains the unit tests for the task manager.
  19   *
  20   * @package   core
  21   * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * This file contains the unit tests for the task manager.
  29   *
  30   * @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class core_task_manager_testcase extends advanced_testcase {
  34  
  35      public function test_ensure_adhoc_task_qos_provider() {
  36          return [
  37              [
  38                  [],
  39                  [],
  40              ],
  41              // A queue with a lopside initial load that needs to be staggered.
  42              [
  43                  [
  44                      (object)['id' => 1, 'classname' => '\core\task\asynchronous_backup_task'],
  45                      (object)['id' => 2, 'classname' => '\core\task\asynchronous_backup_task'],
  46                      (object)['id' => 3, 'classname' => '\core\task\asynchronous_backup_task'],
  47                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
  48                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
  49                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
  50                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  51                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  52                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  53                      (object)['id' => 10, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  54                      (object)['id' => 11, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  55                      (object)['id' => 12, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  56                      (object)['id' => 13, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  57                      (object)['id' => 14, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  58                      (object)['id' => 15, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  59                  ],
  60                  [
  61                      (object)['id' => 1, 'classname' => '\core\task\asynchronous_backup_task'],
  62                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  63                      (object)['id' => 2, 'classname' => '\core\task\asynchronous_backup_task'],
  64                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  65                      (object)['id' => 3, 'classname' => '\core\task\asynchronous_backup_task'],
  66                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  67                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
  68                      (object)['id' => 10, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  69                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
  70                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
  71                      (object)['id' => 11, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  72                      (object)['id' => 12, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  73                      (object)['id' => 13, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  74                      (object)['id' => 14, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  75                      (object)['id' => 15, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  76                  ],
  77              ],
  78              // The same lopsided queue but now the first item is gone.
  79              [
  80                  [
  81                      (object)['id' => 2, 'classname' => '\core\task\asynchronous_backup_task'],
  82                      (object)['id' => 3, 'classname' => '\core\task\asynchronous_backup_task'],
  83                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
  84                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
  85                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
  86                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  87                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  88                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  89                  ],
  90                  [
  91                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  92                      (object)['id' => 2, 'classname' => '\core\task\asynchronous_backup_task'],
  93                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  94                      (object)['id' => 3, 'classname' => '\core\task\asynchronous_backup_task'],
  95                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
  96                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
  97                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
  98                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
  99                  ],
 100              ],
 101              // The same lopsided queue but now the first two items is gone.
 102              [
 103                  [
 104                      (object)['id' => 3, 'classname' => '\core\task\asynchronous_backup_task'],
 105                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
 106                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
 107                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
 108                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 109                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 110                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 111                  ],
 112                  [
 113                      (object)['id' => 3, 'classname' => '\core\task\asynchronous_backup_task'],
 114                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 115                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
 116                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 117                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
 118                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 119                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
 120                  ],
 121              ],
 122              // The same lopsided queue but now the first three items are gone.
 123              [
 124                  [
 125                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
 126                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
 127                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
 128                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 129                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 130                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 131                  ],
 132                  [
 133                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 134                      (object)['id' => 4, 'classname' => '\core\task\asynchronous_backup_task'],
 135                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 136                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
 137                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 138                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
 139                  ],
 140              ],
 141              [
 142                  [
 143                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
 144                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
 145                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 146                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 147                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 148                  ],
 149                  [
 150                      (object)['id' => 5, 'classname' => '\core\task\asynchronous_backup_task'],
 151                      (object)['id' => 7, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 152  
 153                      (object)['id' => 6, 'classname' => '\core\task\asynchronous_backup_task'],
 154                      (object)['id' => 8, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 155  
 156                      (object)['id' => 9, 'classname' => '\tool_dataprivacy\task\process_data_request_task'],
 157                  ],
 158              ],
 159          ];
 160      }
 161  
 162      /**
 163       * Reduces a list of tasks into a simpler string
 164       *
 165       * @param array $input array of tasks
 166       * @return string list of task ids
 167       */
 168      function flatten($tasks) {
 169          $list = '';
 170          foreach ($tasks as $id => $task) {
 171              $list .= ' ' . $task->id;
 172          }
 173          return $list;
 174      }
 175  
 176      /**
 177       * Test that the Quality of Service reordering works.
 178       *
 179       * @dataProvider test_ensure_adhoc_task_qos_provider
 180       *
 181       * @param array $input array of tasks
 182       * @param array $expected array of reordered tasks
 183       * @return void
 184       */
 185      public function test_ensure_adhoc_task_qos(array $input, array $expected) {
 186          $this->resetAfterTest();
 187          $result = \core\task\manager::ensure_adhoc_task_qos($input);
 188  
 189  
 190          $result = $this->flatten($result);
 191          $expected = $this->flatten($expected);
 192  
 193          $this->assertEquals($expected, $result);
 194      }
 195  
 196  }
 197