Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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