[ Index ]

PHP Cross Reference of Unnamed Project

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          $datarootpath = $CFG->{$framework . '_dataroot'} . '/' . $framework;
  56          $lockfile = $datarootpath . '/lock';
  57          if (!file_exists($datarootpath)) {
  58              // Dataroot not initialised yet.
  59              return;
  60          }
  61          if (!file_exists($lockfile)) {
  62              file_put_contents($lockfile, 'This file prevents concurrent execution of Moodle ' . $framework . ' tests');
  63              testing_fix_file_permissions($lockfile);
  64          }
  65          if (self::$lockhandles[$framework] = fopen($lockfile, 'r')) {
  66              $wouldblock = null;
  67              $locked = flock(self::$lockhandles[$framework], (LOCK_EX | LOCK_NB), $wouldblock);
  68              if (!$locked) {
  69                  if ($wouldblock) {
  70                      echo "Waiting for other test execution to complete...\n";
  71                  }
  72                  $locked = flock(self::$lockhandles[$framework], LOCK_EX);
  73              }
  74              if (!$locked) {
  75                  fclose(self::$lockhandles[$framework]);
  76                  self::$lockhandles[$framework] = null;
  77              }
  78          }
  79          register_shutdown_function(array('test_lock', 'release'), $framework);
  80      }
  81  
  82      /**
  83       * Note: do not call manually!
  84       * @internal
  85       * @static
  86       * @param    string  $framework phpunit|behat
  87       * @return   void
  88       */
  89      public static function release($framework) {
  90          if (self::$lockhandles[$framework]) {
  91              flock(self::$lockhandles[$framework], LOCK_UN);
  92              fclose(self::$lockhandles[$framework]);
  93              self::$lockhandles[$framework] = null;
  94          }
  95      }
  96  
  97  }


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