Skip to content

Commit

Permalink
Added connection abort test
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Oct 11, 2023
1 parent 23c610a commit 789b1ef
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
99 changes: 99 additions & 0 deletions aborttest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Performs a request abort test
*
* @package tool_heartbeat
* @copyright 2019 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);

require(__DIR__ . '/../../../config.php');
require_login();

$stage = optional_param('stage', 1, PARAM_NUMBER);
$ignoreabort = optional_param('ignoreabort', 0, PARAM_NUMBER);

if ($ignoreabort) {
ignore_user_abort(true);
// Worst case it should die in 5 seconds.
set_time_limit(5);
}

$syscontext = context_system::instance();
$url = new moodle_url('/admin/tool/heartbeat/aborttest.php');
$PAGE->set_url($url);
$PAGE->set_context($syscontext);
$PAGE->set_pagelayout('standard');
$PAGE->set_cacheable(false);
$url->params(array('stage' => 2));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('testabort', 'tool_heartbeat'));

echo get_string('testaborthelp', 'tool_heartbeat');
echo "<h3>Stage: $stage</h3>";

if ($stage == 2) {
$progress = $SESSION->abortprogress;

if ($progress > 0 && $progress < 100) {
echo $OUTPUT->notification("Yay! the request was correctly aborted at {$progress}%", \core\output\notification::NOTIFY_SUCCESS);
} else {
echo $OUTPUT->notification("Doh! the request was not aborted: {$progress}%", \core\output\notification::NOTIFY_ERROR);
}

echo "<p><a class='btn btn-primary' href='aborttest.php'>Start again</a></p>";
echo "<p><a class='btn btn-danger' href='aborttest.php?ignoreabort=1'>Start again with ignore_user_abort</a></p>";

echo $OUTPUT->footer();
}

if ($stage == 1) {
echo <<<EOF
<p>This should show a moving progress bar, but after 1 seconds the page should reload and it should NOT get to 100%.</p>
<script>
setTimeout(function(){
window.stop();
location.href = '{$url->out()}';
},1000);
</script>
EOF;

$progressbar = new progress_bar();
$progressbar->create();

echo $OUTPUT->footer();

$SESSION->abortprogress = 0;

$totalseconds = 10;
$progressbar->update_full(0, '0%');
for ($c = 1; $c <= 100; $c += .1) {
usleep(10000);
$progressbar->update_full($c, sprintf('%.1f%%', $c));
$SESSION->abortprogress = $c;
if (connection_status() != CONNECTION_NORMAL) {
// @codingStandardsIgnoreStart
error_log("Aborting stage 1 at $c %");
// @codingStandardsIgnoreEnd
}
}
$SESSION->abortprogress = 100;
}

16 changes: 13 additions & 3 deletions lang/en/tool_heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@
$string['pluginname'] = 'Heartbeat';
$string['errorlog'] = 'Error log period';
$string['errorlogdesc'] = 'To help ensure that all web server logging is working we can emit an intermittent message to the error_log. Set this to 0 to turn it off.';
$string['testing'] = 'Test heartbeat';
$string['testingdesc'] = 'You can use this to temporarily fake a warn or error condition to test that your monitoring is correctly working end to end.';
$string['normal'] = 'Normal monitoring';
$string['progress'] = 'Progress bar test';
$string['progresshelp'] = 'This tests that all the various output buffers in the entire stack are corrent including but not limited to php, ob, gzip/deflat, varnish, nginx etc';
$string['testwarning'] = 'Fake a warning';
$string['testabort'] = 'Test abort';
$string['testaborthelp'] = '<p>This tests wether the whole stack is correctly passing a request cancel signal from the browser to php through whatever layers are in the middle, such as load balancers, cdn\'s, caches, php-fm etc.</p>
<p>It works in 2 stages:<p>
<ol>
<li>Stage one takes 10 seconds and renders a progress bar</ii>
<li>In javascript the progress bar page is aborted after 1 second</li>
<li>If the abort works fully the process is killed which unlocks the session and the page reloads quickly showing where the progress bar had gotten to.</li>
<li>If the abort did not work it will continue as an orphaned process holding the session lock until it times out after 5 seconds</li>
</ol>
';
$string['testerror'] = 'Fake a critical';
$string['testwarning'] = 'Fake a warning';
$string['testing'] = 'Test heartbeat';
$string['testingdesc'] = 'You can use this to temporarily fake a warn or error condition to test that your monitoring is correctly working end to end.';
$string['allowedips'] = 'Allowed IPs Config';
$string['allowedipstitle'] = 'IP Blocking Configuration';
$string['allowedipsdescription'] = 'Box to enter safe IP addresses for the heartbeat to respond to.';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023012700;
$plugin->release = 2023012700; // Match release exactly to version.
$plugin->version = 2023101100;
$plugin->release = 2023101100; // Match release exactly to version.
$plugin->requires = 2012120311; // Deep support going back to 2.4.
$plugin->supported = [24, 401];
$plugin->component = 'tool_heartbeat';
Expand Down

0 comments on commit 789b1ef

Please sign in to comment.