[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> adhoc_task_test.php (source)

   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 unittests for adhock tasks.
  19   *
  20   * @package   core
  21   * @category  phpunit
  22   * @copyright 2013 Damyon Wiese
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  require_once (__DIR__ . '/fixtures/task_fixtures.php');
  28  
  29  
  30  /**
  31   * Test class for adhoc tasks.
  32   *
  33   * @package core
  34   * @category task
  35   * @copyright 2013 Damyon Wiese
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_adhoc_task_testcase extends advanced_testcase {
  39  
  40      /**
  41       * Test basic adhoc task execution.
  42       */
  43      public function test_get_next_adhoc_task_now() {
  44          $this->resetAfterTest(true);
  45  
  46          // Create an adhoc task.
  47          $task = new \core\task\adhoc_test_task();
  48  
  49          // Queue it.
  50          \core\task\manager::queue_adhoc_task($task);
  51  
  52          $now = time();
  53          // Get it from the scheduler.
  54          $task = \core\task\manager::get_next_adhoc_task($now);
  55          $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
  56          $task->execute();
  57          \core\task\manager::adhoc_task_complete($task);
  58      }
  59  
  60      /**
  61       * Test adhoc task failure retry backoff.
  62       */
  63      public function test_get_next_adhoc_task_fail_retry() {
  64          $this->resetAfterTest(true);
  65  
  66          // Create an adhoc task.
  67          $task = new \core\task\adhoc_test_task();
  68          \core\task\manager::queue_adhoc_task($task);
  69  
  70          $now = time();
  71  
  72          // Get it from the scheduler, execute it, and mark it as failed.
  73          $task = \core\task\manager::get_next_adhoc_task($now);
  74          $task->execute();
  75          \core\task\manager::adhoc_task_failed($task);
  76  
  77          // The task will not be returned immediately.
  78          $this->assertNull(\core\task\manager::get_next_adhoc_task($now));
  79  
  80          // Should get the adhoc task (retry after delay).
  81          $task = \core\task\manager::get_next_adhoc_task($now + 120);
  82          $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
  83          $task->execute();
  84  
  85          \core\task\manager::adhoc_task_complete($task);
  86  
  87          // Should not get any task.
  88          $this->assertNull(\core\task\manager::get_next_adhoc_task($now));
  89      }
  90  
  91      /**
  92       * Test future adhoc task execution.
  93       */
  94      public function test_get_next_adhoc_task_future() {
  95          $this->resetAfterTest(true);
  96  
  97          $now = time();
  98          // Create an adhoc task in future.
  99          $task = new \core\task\adhoc_test_task();
 100          $task->set_next_run_time($now + 1000);
 101          \core\task\manager::queue_adhoc_task($task);
 102  
 103          // Fetching the next task should not return anything.
 104          $this->assertNull(\core\task\manager::get_next_adhoc_task($now));
 105  
 106          // Fetching in the future should return the task.
 107          $task = \core\task\manager::get_next_adhoc_task($now + 1020);
 108          $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
 109          $task->execute();
 110          \core\task\manager::adhoc_task_complete($task);
 111      }
 112  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1