Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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\lock;
  18  
  19  /**
  20   * Unit tests for the lock factory.
  21   *
  22   * @covers \core\lock\timing_wrapper_lock_factory
  23   * @package core
  24   * @copyright 2022 The Open University
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class timing_wrapper_lock_factory_test extends \advanced_testcase {
  28  
  29      /**
  30       * Tests lock timing wrapper class.
  31       */
  32      public function test_lock_timing(): void {
  33          global $PERF;
  34  
  35          $this->resetAfterTest();
  36  
  37          // Reset the storage in case previous tests have added anything.
  38          unset($PERF->locks);
  39  
  40          // Set up a lock factory using the db record lock type which is always available.
  41          $lockfactory = new timing_wrapper_lock_factory('phpunit',
  42                  new db_record_lock_factory('phpunit'));
  43  
  44          // Get 2 locks and release them.
  45          $before = microtime(true);
  46          $lock1 = $lockfactory->get_lock('frog', 2);
  47          $lock2 = $lockfactory->get_lock('toad', 2);
  48          $lock2->release();
  49          $lock1->release();
  50          $duration = microtime(true) - $before;
  51  
  52          // Confirm that perf info is now logged and appears plausible.
  53          $this->assertObjectHasAttribute('locks', $PERF);
  54          $this->assertEquals('phpunit', $PERF->locks[0]->type);
  55          $this->assertEquals('frog', $PERF->locks[0]->resource);
  56          $this->assertTrue($PERF->locks[0]->wait <= $duration);
  57          $this->assertTrue($PERF->locks[0]->success);
  58          $this->assertTrue($PERF->locks[0]->held <= $duration);
  59          $this->assertEquals('phpunit', $PERF->locks[1]->type);
  60          $this->assertEquals('toad', $PERF->locks[1]->resource);
  61          $this->assertTrue($PERF->locks[1]->wait <= $duration);
  62          $this->assertTrue($PERF->locks[1]->success);
  63          $this->assertTrue($PERF->locks[1]->held <= $duration);
  64      }
  65  }