MDL-65249 Session: Throw exception if number of attempts exceeded.
authorIlya Tregubov <ilyatregubov@catalyst-au.net>
Wed, 3 Apr 2019 05:47:52 +0000 (16:47 +1100)
committerIlya Tregubov <ilyatregubov@catalyst-au.net>
Tue, 25 Jun 2019 05:09:42 +0000 (15:09 +1000)
lib/classes/session/redis.php
lib/tests/session_redis_test.php

index a669996..d475867 100644 (file)
@@ -199,9 +199,7 @@ class redis extends handler {
                 $logstring = "Failed to connect (try {$counter} out of {$maxnumberofretries}) to redis ";
                 $logstring .= "at {$this->host}:{$this->port}, error returned was: {$e->getMessage()}";
 
-                // @codingStandardsIgnoreStart
-                error_log($logstring);
-                // @codingStandardsIgnoreEnd
+                debugging($logstring);
             }
 
             $counter++;
@@ -211,7 +209,9 @@ class redis extends handler {
         }
 
         // We have exhausted our retries, time to give up.
-        return false;
+        if (isset($logstring)) {
+            throw new RedisException($logstring);
+        }
     }
 
     /**
index 7dc7b21..770f2ed 100644 (file)
@@ -270,6 +270,24 @@ class core_session_redis_testcase extends advanced_testcase {
         $this->assertEmpty($this->redis->keys($this->keyprefix.'*'), 'There should be no session data left.');
     }
 
+    public function test_exception_when_connection_attempts_exceeded() {
+        global $CFG;
+
+        $CFG->session_redis_port = 111111;
+        $actual = '';
+
+        $sess = new \core\session\redis();
+        try {
+            $sess->init();
+        } catch (RedisException $e) {
+            $actual = $e->getMessage();
+        }
+
+        $expected = 'Failed to connect (try 5 out of 5) to redis at 127.0.0.1:111111';
+        $this->assertDebuggingCalledCount(5);
+        $this->assertContains($expected, $actual);
+    }
+
     /**
      * Assert that we don't have any session locks in Redis.
      */