Skip to content

Commit

Permalink
MDL-65249 Session: Throw exception if number of attempts exceeded.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Tregubov authored and snake committed Jun 26, 2019
1 parent 2937760 commit 61373c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/classes/session/redis.php
Expand Up @@ -199,9 +199,7 @@ public function init() {
$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++;
Expand All @@ -211,7 +209,9 @@ public function init() {
}

// We have exhausted our retries, time to give up.
return false;
if (isset($logstring)) {
throw new RedisException($logstring);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions lib/tests/session_redis_test.php
Expand Up @@ -270,6 +270,24 @@ public function test_kill_sessions_removes_the_session_from_redis() {
$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.
*/
Expand Down

0 comments on commit 61373c8

Please sign in to comment.