Skip to content

Commit

Permalink
MDL-56898 behat: No need to trigger error, as it sometimes hangs behat
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Nov 23, 2016
1 parent ab464e1 commit d7b20b8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 45 deletions.
14 changes: 0 additions & 14 deletions lib/behat/lib.php
Expand Up @@ -134,13 +134,6 @@ function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
return true;
}

// No need to report the before_scenario warning generated to clear last error.
// As error_clear_last is only available in php 7.0+, we trigger E_USER_WARNING
// to clear any last error which was generated during reset in before_scenario.
if (($errno === E_USER_WARNING) && $errstr == 'before_scenario') {
return;
}

// This error handler receives E_ALL | E_STRICT, running the behat test site the debug level is
// set to DEVELOPER and will always include E_NOTICE,E_USER_NOTICE... as part of E_ALL, if the current
// error_reporting() value does not include one of those levels is because it has been forced through
Expand Down Expand Up @@ -183,13 +176,6 @@ function behat_shutdown_function() {
// Ignore E_WARNING, as they might come via ( @ )suppression and might lead to false failure.
if (isset($error['type']) && !($error['type'] & E_WARNING)) {

// No need to report the before_scenario warning generated to clear last error.
// As error_clear_last is only available in php 7.0+, we trigger E_USER_WARNING
// to clear any last error which was generated during reset in before_scenario.
if (($error['type'] & E_USER_WARNING) && $error['message'] == 'before_scenario') {
return;
}

$errors = behat_get_shutdown_process_errors();

$errors[] = $error;
Expand Down
36 changes: 5 additions & 31 deletions lib/tests/behat/behat_hooks.php
Expand Up @@ -96,13 +96,6 @@ class behat_hooks extends behat_base {
*/
protected static $timings = array();

/**
* Keeps track of php error generated during reset.
*
* @var int keep track of how many php errors were generated.
*/
public static $phperrorduringresetcounter = 0;

/**
* Hook to capture BeforeSuite event so as to give access to moodle codebase.
* This will try and catch any exception and exists if anything fails.
Expand Down Expand Up @@ -309,30 +302,12 @@ public function before_scenario(BeforeScenarioScope $scope) {
// Reset $SESSION.
\core\session\manager::init_empty_session();

// Set custom handler to try reset all data, if failed because of previous ajax.
set_error_handler(
function($errno, $errstr, $errfile, $errline) {
behat_hooks::$phperrorduringresetcounter++;
if (behat_hooks::$phperrorduringresetcounter < self::TIMEOUT) {
sleep(1);
behat_util::reset_all_data();
}
return true;
}, -1 & ~E_NOTICE & ~E_WARNING);
// Ignore E_NOTICE and E_WARNING during reset, as this might be caused because of some existing process
// running ajax. This will be investigated in another issue.
$errorlevel = error_reporting();
error_reporting($errorlevel & ~E_NOTICE & ~E_WARNING);
behat_util::reset_all_data();
restore_error_handler();

// Trigger an error which will be ignored by behat_shutdown_function, this is hacky way to clear last error in php < 7.0.
if (self::$phperrorduringresetcounter > 0) {
if (function_exists('error_clear_last')) {
error_clear_last();
} else {
trigger_error('before_scenario', E_USER_WARNING);
}
}

// Reset the counter here, as this won't be required.
self::$phperrorduringresetcounter = 0;
error_reporting($errorlevel);

// Assign valid data to admin user (some generator-related code needs a valid user).
$user = $DB->get_record('user', array('username' => 'admin'));
Expand Down Expand Up @@ -379,7 +354,6 @@ function($errno, $errstr, $errfile, $errline) {
public function after_scenario(AfterScenarioScope $scope) {
try {
$this->wait_for_pending_js();
$this->getSession()->visit($this->locate_path('/README.txt'));
$this->getSession()->reset();
} catch (DriverException $e) {
// Try restart session, if DriverException caught.
Expand Down

0 comments on commit d7b20b8

Please sign in to comment.