Merge branch '41988-26' of git://github.com/samhemelryk/moodle

This commit is contained in:
Marina Glancy 2013-10-01 10:01:17 +10:00
commit f09ecc6d8b

View file

@ -241,48 +241,56 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
* closure exception will be used, but you must provide an exception if the closure does not throws * closure exception will be used, but you must provide an exception if the closure does not throws
* an exception. * an exception.
* *
* @throws Exception If it timeouts without receiving something != false from the closure * @throws Exception If it timeouts without receiving something != false from the closure
* @param Closure $lambda The function to execute. * @param Function|array|string $lambda The function to execute or an array passed to call_user_func (maps to a class method)
* @param mixed $args Arguments to pass to the closure * @param mixed $args Arguments to pass to the closure
* @param int $timeout Timeout * @param int $timeout Timeout in seconds
* @param Exception $exception The exception to throw in case it time outs. * @param Exception $exception The exception to throw in case it time outs.
* @param bool $microsleep If set to true it'll sleep micro seconds rather than seconds.
* @return mixed The value returned by the closure * @return mixed The value returned by the closure
*/ */
protected function spin($lambda, $args = false, $timeout = false, $exception = false) { protected function spin($lambda, $args = false, $timeout = false, $exception = false, $microsleep = false) {
// Using default timeout which is pretty high. // Using default timeout which is pretty high.
if (!$timeout) { if (!$timeout) {
$timeout = self::TIMEOUT; $timeout = self::TIMEOUT;
} }
if ($microsleep) {
// Will sleep 1/10th of a second by default for self::TIMEOUT seconds.
$loops = $timeout * 10;
} else {
// Will sleep for self::TIMEOUT seconds.
$loops = $timeout;
}
for ($i = 0; $i < $timeout; $i++) { for ($i = 0; $i < $loops; $i++) {
// We catch the exception thrown by the step definition to execute it again. // We catch the exception thrown by the step definition to execute it again.
try { try {
// We don't check with !== because most of the time closures will return // We don't check with !== because most of the time closures will return
// direct Behat methods returns and we are not sure it will be always (bool)false // direct Behat methods returns and we are not sure it will be always (bool)false
// if it just runs the behat method without returning anything $return == null. // if it just runs the behat method without returning anything $return == null.
if ($return = $lambda($this, $args)) { if ($return = call_user_func($lambda, $this, $args)) {
return $return; return $return;
} }
} catch (Exception $e) { } catch (Exception $e) {
// We would use the first closure exception if no exception has been provided. // We would use the first closure exception if no exception has been provided.
if (!$exception) { if (!$exception) {
$exception = $e; $exception = $e;
} }
// We wait until no exception is thrown or timeout expires. // We wait until no exception is thrown or timeout expires.
continue; continue;
} }
sleep(1); if ($microsleep) {
usleep(100000);
} else {
sleep(1);
}
} }
// Using coding_exception as is a development issue if no exception has been provided. // Using coding_exception as is a development issue if no exception has been provided.
if (!$exception) { if (!$exception) {
$exception = new coding_exception('spin method requires an exception if the closure doesn\'t throw an exception itself'); $exception = new coding_exception('spin method requires an exception if the callback does not throw an exception');
} }
// Throwing exception to the user. // Throwing exception to the user.