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