From 789b1ef0b8c8a8ea1e34f637c80e28f2ac6ca0cd Mon Sep 17 00:00:00 2001
From: Brendan Heywood
Date: Wed, 24 Apr 2019 17:26:16 +1000
Subject: [PATCH] Added connection abort test
---
aborttest.php | 99 ++++++++++++++++++++++++++++++++++++++
lang/en/tool_heartbeat.php | 16 ++++--
version.php | 4 +-
3 files changed, 114 insertions(+), 5 deletions(-)
create mode 100644 aborttest.php
diff --git a/aborttest.php b/aborttest.php
new file mode 100644
index 0000000..23b703d
--- /dev/null
+++ b/aborttest.php
@@ -0,0 +1,99 @@
+.
+
+/**
+ * Performs a request abort test
+ *
+ * @package tool_heartbeat
+ * @copyright 2019 Brendan Heywood
+ * @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 "Stage: $stage
";
+
+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 "Start again
";
+ echo "Start again with ignore_user_abort
";
+
+ echo $OUTPUT->footer();
+}
+
+if ($stage == 1) {
+ echo <<This should show a moving progress bar, but after 1 seconds the page should reload and it should NOT get to 100%.
+
+
+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;
+}
+
diff --git a/lang/en/tool_heartbeat.php b/lang/en/tool_heartbeat.php
index a03162c..f50c665 100644
--- a/lang/en/tool_heartbeat.php
+++ b/lang/en/tool_heartbeat.php
@@ -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'] = '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.
+It works in 2 stages:
+
+- Stage one takes 10 seconds and renders a progress bar
+
- In javascript the progress bar page is aborted after 1 second
+- 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.
+- If the abort did not work it will continue as an orphaned process holding the session lock until it times out after 5 seconds
+
+';
$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.';
diff --git a/version.php b/version.php
index 2709306..68f9f9c 100644
--- a/version.php
+++ b/version.php
@@ -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';