Proyectos de Subversion Moodle

Rev

| 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 cachestore_redis;
18
 
19
/**
20
 * TTL support test for Redis cache.
21
 *
22
 * If you wish to use these unit tests all you need to do is add the following definition to
23
 * your config.php file.
24
 *
25
 * define('TEST_CACHESTORE_REDIS_TESTSERVERS', '127.0.0.1');
26
 *
27
 * @package cachestore_redis
28
 * @copyright 2021 The Open University
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @covers \cachestore_redis
31
 */
32
final class ttl_test extends \advanced_testcase {
33
    /** @var \cachestore_redis|null Cache store  */
34
    protected $store = null;
35
 
36
    public function setUp(): void {
37
        // Make sure cachestore_redis is available.
38
        require_once(__DIR__ . '/../lib.php');
39
        if (!\cachestore_redis::are_requirements_met() || !defined('TEST_CACHESTORE_REDIS_TESTSERVERS')) {
40
            $this->markTestSkipped('Could not test cachestore_redis. Requirements are not met.');
41
        }
42
 
43
        // Set up a Redis store with a fake definition that has TTL set to 10 seconds.
44
        $definition = \cache_definition::load('core/wibble', [
45
                'mode' => 1,
46
                'simplekeys' => true,
47
                'simpledata' => true,
48
                'ttl' => 10,
49
                'component' => 'core',
50
                'area' => 'wibble',
51
                'selectedsharingoption' => 2,
52
                'userinputsharingkey' => '',
53
                'sharingoptions' => 15,
54
        ]);
55
        $this->store = new \cachestore_redis('Test', \cachestore_redis::unit_test_configuration());
56
        $this->store->initialise($definition);
57
 
58
        parent::setUp();
59
    }
60
 
61
    protected function tearDown(): void {
62
        parent::tearDown();
63
 
64
        if ($this->store instanceof \cachestore_redis) {
65
            $this->store->purge();
66
        }
67
    }
68
 
69
    /**
70
     * Test calling set_many with an empty array
71
     *
72
     * Trivial test to ensure we don't trigger an ArgumentCountError when calling zAdd with invalid parameters
73
     */
74
    public function test_set_many_empty(): void {
75
        $this->assertEquals(0, $this->store->set_many([]));
76
    }
77
 
78
    /**
79
     * Tests expiring data.
80
     */
81
    public function test_expire_ttl(): void {
82
        $this->resetAfterTest();
83
 
84
        // Set some data at time 100.
85
        \cachestore_redis::set_phpunit_time(100);
86
        $this->store->set('a', 1);
87
        $this->store->set('b', 2);
88
        $this->store->set_many([['key' => 'c', 'value' => 3], ['key' => 'd', 'value' => 4],
89
                ['key' => 'e', 'value' => 5], ['key' => 'f', 'value' => 6],
90
                ['key' => 'g', 'value' => 7], ['key' => 'h', 'value' => 8]]);
91
 
92
        // Set some other data at time 110, including some of the existing values. Whether the
93
        // value changes or not, its TTL should update.
94
        \cachestore_redis::set_phpunit_time(110);
95
        $this->store->set('b', 2);
96
        $this->store->set_many([['key' => 'c', 'value' => 99], ['key' => 'd', 'value' => 4]]);
97
 
98
        // Check all the data is still set.
99
        $this->assertEqualsCanonicalizing(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
100
                $this->store->find_all());
101
 
102
        // Delete some data (to check deletion doesn't confuse expiry).
103
        $this->store->delete('f');
104
        $this->store->delete_many(['g', 'h']);
105
 
106
        // Set time to 115 and expire data.
107
        \cachestore_redis::set_phpunit_time(115);
108
        $info = $this->store->expire_ttl();
109
 
110
        // We are expecting keys a and e to be deleted.
111
        $this->assertEquals(2, $info['keys']);
112
        $this->assertEquals(1, $info['batches']);
113
 
114
        // Check the keys are as expected.
115
        $this->assertEqualsCanonicalizing(['b', 'c', 'd'], $this->store->find_all());
116
 
117
        // Might as well check the values of the surviving keys.
118
        $this->assertEquals(2, $this->store->get('b'));
119
        $this->assertEquals(99, $this->store->get('c'));
120
        $this->assertEquals(4, $this->store->get('d'));
121
    }
122
}