Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
use core\session\redis as redis_session;
20
use RedisClusterException;
21
 
22
/**
23
 * Unit tests for Redis cluster in the core/session/redis.php.
24
 *
25
 * NOTE: in order to execute this test you need to set up
26
 *       Redis cluster server and add configuration a constant
27
 *       to config.php or phpunit.xml configuration file:
28
 *
29
 * define('TEST_SESSION_REDIS_HOSTCLUSTER', '127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002');
30
 * define('TEST_SESSION_REDIS_AUTHCLUSTER', 'foobared');
31
 *
32
 * define('TEST_SESSION_REDIS_ENCRYPTCLUSTER', ['verify_peer' => false, 'verify_peer_name' => false]);
33
 * OR
34
 * define('TEST_SESSION_REDIS_ENCRYPTCLUSTER', ['cafile' => '/cafile/dir/ca.crt']);
35
 *
36
 * @package   core
37
 * @copyright 2024 Meirza <meirza.arson@moodle.com>
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 * @coversDefaultClass    \core\session\redis
40
 */
1441 ariadna 41
final class session_redis_cluster_test extends \advanced_testcase {
1 efrain 42
 
43
    /**
44
     * Set up the test environment.
45
     */
46
    public function setUp(): void {
47
        global $CFG;
1441 ariadna 48
        parent::setUp();
1 efrain 49
 
50
        if (!\cache_helper::is_cluster_available()) {
51
            $this->markTestSkipped('Could not test core_session with cluster, class RedisCluster is not available.');
52
        } else if (!defined('TEST_SESSION_REDIS_HOSTCLUSTER')) {
53
            $this->markTestSkipped('Could not test session_redis_cluster_test with cluster, missing configuration. ' .
54
                                  "Example: define('TEST_SESSION_REDIS_HOSTCLUSTER', " .
55
                                  "'localhost:7000,localhost:7001,localhost:7002');");
56
        }
57
        $this->resetAfterTest();
58
        $CFG->session_redis_host = TEST_SESSION_REDIS_HOSTCLUSTER;
59
        if (defined('TEST_SESSION_REDIS_ENCRYPTCLUSTER') && TEST_SESSION_REDIS_ENCRYPTCLUSTER) {
60
            $CFG->session_redis_encrypt = TEST_SESSION_REDIS_ENCRYPTCLUSTER;
61
        }
62
        if (defined('TEST_SESSION_REDIS_AUTHCLUSTER') && TEST_SESSION_REDIS_AUTHCLUSTER) {
63
            $CFG->session_redis_auth = TEST_SESSION_REDIS_AUTHCLUSTER;
64
        }
65
    }
66
 
67
    /**
68
     * Tests compression for session read and write operations.
69
     *
70
     * It covers the behavior of session read and write operations under different compression configurations.
71
     *
72
     * @runInSeparateProcess
73
     * @covers ::read
74
     * @covers ::write
75
     */
76
    public function test_read_and_write(): void {
77
        $rediscluster = new redis_session();
78
        $rediscluster->init();
79
        $this->assertTrue($rediscluster->write('sess1', 'DATA'));
80
        $this->assertSame('DATA', $rediscluster->read('sess1'));
81
        $this->assertTrue($rediscluster->close());
82
    }
83
 
84
    /**
85
     * Tests the behavior when connection attempts to Redis cluster are exceeded.
86
     *
87
     * It sets up the environment to simulate multiple failed connection attempts and
88
     * checks if the expected exception message is received.
89
     *
90
     * @runInSeparateProcess
91
     * @covers ::init
92
     */
93
    public function test_exception_when_connection_attempts_exceeded(): void {
94
        global $CFG;
95
 
96
        $CFG->session_redis_host = '127.0.0.1:1111111,127.0.0.1:1111112,127.0.0.1:1111113';
97
        $actual = '';
98
 
99
        $rediscluster = new redis_session();
100
        try {
101
            $rediscluster->init();
102
        } catch (RedisClusterException $e) {
103
            $actual = $e->getMessage();
104
        }
105
 
106
        $expected = "Failed to connect (try 5 out of 5) to Redis at";
107
        $this->assertDebuggingCalledCount(5);
108
        $this->assertStringContainsString($expected, $actual);
109
    }
110
}