[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/testing/classes/ -> test_lock.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   * Tests lock
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once (__DIR__.'/../lib.php');
  27  
  28  /**
  29   * Tests lock to prevent concurrent executions of the same test suite
  30   *
  31   * @package    core
  32   * @category   test
  33   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class test_lock {
  37  
  38      /**
  39       * @var array Array of resource used for prevention of parallel test execution
  40       */
  41      protected static $lockhandles = array();
  42  
  43      /**
  44       * Prevent parallel test execution - this can not work in Moodle because we modify database and dataroot.
  45       *
  46       * Note: do not call manually!
  47       *
  48       * @internal
  49       * @static
  50       * @param    string  $framework Test framework
  51       * @return   void
  52       */
  53      public static function acquire($framework) {
  54          global $CFG;
  55  
  56          $datarootpath = $CFG->{$framework . '_dataroot'} . '/' . $framework;
  57          $lockfile = $datarootpath . '/lock';
  58          if (!file_exists($datarootpath)) {
  59              // Dataroot not initialised yet.
  60              return;
  61          }
  62          if (!file_exists($lockfile)) {
  63              file_put_contents($lockfile, 'This file prevents concurrent execution of Moodle ' . $framework . ' tests');
  64              testing_fix_file_permissions($lockfile);
  65          }
  66          if (self::$lockhandles[$framework] = fopen($lockfile, 'r')) {
  67              $wouldblock = null;
  68              $locked = flock(self::$lockhandles[$framework], (LOCK_EX | LOCK_NB), $wouldblock);
  69              if (!$locked) {
  70                  if ($wouldblock) {
  71                      echo "Waiting for other test execution to complete...\n";
  72                  }
  73                  $locked = flock(self::$lockhandles[$framework], LOCK_EX);
  74              }
  75              if (!$locked) {
  76                  fclose(self::$lockhandles[$framework]);
  77                  self::$lockhandles[$framework] = null;
  78              }
  79          }
  80          register_shutdown_function(array('test_lock', 'release'), $framework);
  81      }
  82  
  83      /**
  84       * Note: do not call manually!
  85       * @internal
  86       * @static
  87       * @param    string  $framework phpunit|behat
  88       * @return   void
  89       */
  90      public static function release($framework) {
  91          if (self::$lockhandles[$framework]) {
  92              flock(self::$lockhandles[$framework], LOCK_UN);
  93              fclose(self::$lockhandles[$framework]);
  94              self::$lockhandles[$framework] = null;
  95          }
  96      }
  97  
  98  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1