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 310 and 403] [Versions 39 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;
  18  
  19  /**
  20   * Unit tests for our locking implementations.
  21   *
  22   * @package    core
  23   * @category   test
  24   * @copyright  2013 Damyon Wiese
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class lock_test extends \advanced_testcase {
  28  
  29      /**
  30       * Some lock types will store data in the database.
  31       */
  32      protected function setUp(): void {
  33          $this->resetAfterTest(true);
  34      }
  35  
  36      /**
  37       * Run a suite of tests on a lock factory class.
  38       *
  39       * @param class $lockfactoryclass - A lock factory class to test
  40       */
  41      protected function run_on_lock_factory($lockfactoryclass) {
  42  
  43          $modassignfactory = new $lockfactoryclass('mod_assign');
  44          $tooltaskfactory = new $lockfactoryclass('tool_task');
  45  
  46          // Test for lock clashes between lock stores.
  47          $assignlock = $modassignfactory->get_lock('abc', 0);
  48          $this->assertNotEmpty($assignlock, 'Get a lock "abc" from store "mod_assign"');
  49  
  50          $tasklock = $tooltaskfactory->get_lock('abc', 0);
  51          $this->assertNotEmpty($tasklock, 'Get a lock "abc" from store "tool_task"');
  52  
  53          $assignlock->release();
  54          $tasklock->release();
  55  
  56          $lockfactory = new $lockfactoryclass('default');
  57          if ($lockfactory->is_available()) {
  58              // This should work.
  59              $lock1 = $lockfactory->get_lock('abc', 2);
  60              $this->assertNotEmpty($lock1, 'Get a lock');
  61  
  62              if ($lockfactory->supports_timeout()) {
  63                  // Attempt to obtain a lock within a 2 sec timeout.
  64                  $durationlock2 = -microtime(true);
  65                  $lock2 = $lockfactory->get_lock('abc', 2);
  66                  $durationlock2 += microtime(true);
  67  
  68                  if (!$lock2) { // If the lock was not obtained.
  69                      $this->assertFalse($lock2, 'Cannot get a stacked lock');
  70                      // This should timeout after 2 seconds.
  71                      $this->assertTrue($durationlock2 < 2.5, 'Lock should timeout after no more than 2 seconds');
  72                  } else {
  73                      $this->assertNotEmpty($lock2, 'Get a stacked lock');
  74                      $this->assertTrue($lock2->release(), 'Release a stacked lock');
  75                  }
  76  
  77                  // Attempt to obtain a lock within a 0 sec timeout.
  78                  $durationlock2 = -microtime(true);
  79                  $lock2 = $lockfactory->get_lock('abc', 0);
  80                  $durationlock2 += microtime(true);
  81  
  82                  if (!$lock2) { // If the lock was not obtained.
  83                      // This should timeout almost instantly.
  84                      $this->assertTrue($durationlock2 < 0.100, 'Lock should timeout almost instantly < 100ms');
  85                  } else {
  86                      // This stacked lock should be gained almost instantly.
  87                      $this->assertTrue($durationlock2 < 0.100, 'Lock should be gained almost instantly');
  88                      $lock2->release();
  89  
  90                      // We should also assert that locks fail instantly if locked
  91                      // from another process but this is hard to unit test.
  92                  }
  93              }
  94              // Release the lock.
  95              $this->assertTrue($lock1->release(), 'Release a lock');
  96              // Get it again.
  97              $lock3 = $lockfactory->get_lock('abc', 2);
  98  
  99              $this->assertNotEmpty($lock3, 'Get a lock again');
 100              // Release the lock again.
 101              $this->assertTrue($lock3->release(), 'Release a lock again');
 102              // Release the lock again (shouldn't hurt).
 103              $this->assertFalse($lock3->release(), 'Release a lock that is not held');
 104              if (!$lockfactory->supports_auto_release()) {
 105                  // Test that a lock can be claimed after the timeout period.
 106                  $lock4 = $lockfactory->get_lock('abc', 2, 2);
 107                  $this->assertNotEmpty($lock4, 'Get a lock');
 108                  sleep(3);
 109  
 110                  $lock5 = $lockfactory->get_lock('abc', 2, 2);
 111                  $this->assertNotEmpty($lock5, 'Get another lock after a timeout');
 112                  $this->assertTrue($lock5->release(), 'Release the lock');
 113                  $this->assertTrue($lock4->release(), 'Release the lock');
 114              }
 115          }
 116      }
 117  
 118      /**
 119       * Tests the testable lock factories classes.
 120       * @return void
 121       */
 122      public function test_locks() {
 123          // Run the suite on the current configured default (may be non-core).
 124          $this->run_on_lock_factory(\core\lock\lock_config::get_lock_factory_class());
 125  
 126          // Manually create the core no-configuration factories.
 127          $this->run_on_lock_factory(\core\lock\db_record_lock_factory::class);
 128          $this->run_on_lock_factory(\core\lock\file_lock_factory::class);
 129  
 130      }
 131  
 132  }
 133