[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> lock_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   * lock unit tests
  19   *
  20   * @package    core
  21   * @category   test
  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  
  28  
  29  /**
  30   * Unit tests for our locking implementations.
  31   *
  32   * @package    core
  33   * @category   test
  34   * @copyright  2013 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class lock_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Some lock types will store data in the database.
  41       */
  42      protected function setUp() {
  43          $this->resetAfterTest(true);
  44      }
  45  
  46      /**
  47       * Run a suite of tests on a lock factory.
  48       * @param \core\lock\lock_factory $lockfactory - A lock factory to test
  49       */
  50      protected function run_on_lock_factory(\core\lock\lock_factory $lockfactory) {
  51  
  52          if ($lockfactory->is_available()) {
  53              // This should work.
  54              $lock1 = $lockfactory->get_lock('abc', 2);
  55              $this->assertNotEmpty($lock1, 'Get a lock');
  56  
  57              if ($lockfactory->supports_timeout()) {
  58                  if ($lockfactory->supports_recursion()) {
  59                      $lock2 = $lockfactory->get_lock('abc', 2);
  60                      $this->assertNotEmpty($lock2, 'Get a stacked lock');
  61                      $this->assertTrue($lock2->release(), 'Release a stacked lock');
  62                  } else {
  63                      // This should timeout.
  64                      $lock2 = $lockfactory->get_lock('abc', 2);
  65                      $this->assertFalse($lock2, 'Cannot get a stacked lock');
  66                  }
  67              }
  68              // Release the lock.
  69              $this->assertTrue($lock1->release(), 'Release a lock');
  70              // Get it again.
  71              $lock3 = $lockfactory->get_lock('abc', 2);
  72  
  73              $this->assertNotEmpty($lock3, 'Get a lock again');
  74              // Release the lock again.
  75              $this->assertTrue($lock3->release(), 'Release a lock again');
  76              // Release the lock again (shouldn't hurt).
  77              $this->assertFalse($lock3->release(), 'Release a lock that is not held');
  78              if (!$lockfactory->supports_auto_release()) {
  79                  // Test that a lock can be claimed after the timeout period.
  80                  $lock4 = $lockfactory->get_lock('abc', 2, 2);
  81                  $this->assertNotEmpty($lock4, 'Get a lock');
  82                  sleep(3);
  83  
  84                  $lock5 = $lockfactory->get_lock('abc', 2, 2);
  85                  $this->assertNotEmpty($lock5, 'Get another lock after a timeout');
  86                  $this->assertTrue($lock5->release(), 'Release the lock');
  87                  $this->assertTrue($lock4->release(), 'Release the lock');
  88              }
  89          }
  90      }
  91  
  92      /**
  93       * Tests the testable lock factories.
  94       * @return void
  95       */
  96      public function test_locks() {
  97          // Run the suite on the current configured default (may be non-core).
  98          $defaultfactory = \core\lock\lock_config::get_lock_factory('default');
  99          $this->run_on_lock_factory($defaultfactory);
 100  
 101          // Manually create the core no-configuration factories.
 102          $dblockfactory = new \core\lock\db_record_lock_factory('test');
 103          $this->run_on_lock_factory($dblockfactory);
 104  
 105          $filelockfactory = new \core\lock\file_lock_factory('test');
 106          $this->run_on_lock_factory($filelockfactory);
 107  
 108      }
 109  
 110  }
 111  


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