Proyectos de Subversion Moodle

Rev

Rev 11 | | 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_file;
18
 
1441 ariadna 19
use core_cache\definition;
20
use core_cache\store;
1 efrain 21
use cachestore_file;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
// Include the necessary evils.
26
global $CFG;
27
require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
28
require_once($CFG->dirroot.'/cache/stores/file/lib.php');
29
 
30
/**
31
 * File unit test class.
32
 *
33
 * @package    cachestore_file
34
 * @copyright  2013 Sam Hemelryk
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 * @covers \cachestore_file
37
 */
1441 ariadna 38
final class store_test extends \cachestore_tests {
1 efrain 39
    /**
40
     * Returns the file class name
41
     * @return string
42
     */
43
    protected function get_class_name() {
44
        return 'cachestore_file';
45
    }
46
 
47
    /**
1441 ariadna 48
     * Provider for set/get tests with all combinations of serializer.
49
     *
50
     * @return array
51
     */
52
    public static function getset_serialization_test_provider(): array {
53
        $data = [
54
            [
55
                'PHP serializer',
56
                \cachestore_file::SERIALIZER_PHP,
57
            ],
58
        ];
59
        if (function_exists('igbinary_serialize')) {
60
            $data[] = [
61
                'Igbinary serializer',
62
                \cachestore_file::SERIALIZER_IGBINARY,
63
            ];
64
        }
65
 
66
        return $data;
67
    }
68
 
69
    /**
70
     * Test get and set function correctly with all combinations of serializer.
71
     *
72
     * @dataProvider getset_serialization_test_provider
73
     * @param string $name
74
     * @param string $serializer
75
     */
76
    public function test_getset_serialization(string $name, string $serializer): void {
77
        $definition = definition::load_adhoc(store::MODE_APPLICATION, 'cachestore_file', 'phpunit_test');
78
        $store = new \cachestore_file('Test', ['serializer' => $serializer]);
79
        $store->initialise($definition);
80
        $originalvalue = 'value12345';
81
        $store->set('key', $originalvalue);
82
        $unserializedvalue = $store->get('key');
83
        self::assertSame($originalvalue, $unserializedvalue, "Invalid serialisation/unserialisation for: {$name}");
84
    }
85
 
86
    /**
1 efrain 87
     * Testing cachestore_file::get with prescan enabled and with
88
     * deleting the cache between the prescan and the call to get.
89
     *
90
     * The deleting of cache simulates some other process purging
91
     * the cache.
92
     */
11 efrain 93
    public function test_cache_get_with_prescan_and_purge(): void {
1 efrain 94
        global $CFG;
95
 
1441 ariadna 96
        $definition = definition::load_adhoc(store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
1 efrain 97
        $name = 'File test';
98
 
99
        $path = make_cache_directory('cachestore_file_test');
100
        $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true));
101
        $cache->initialise($definition);
102
 
103
        $cache->set('testing', 'value');
104
 
105
        $path  = make_cache_directory('cachestore_file_test');
106
        $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true));
107
        $cache->initialise($definition);
108
 
109
        // Let's pretend that some other process purged caches.
110
        remove_dir($CFG->cachedir.'/cachestore_file_test', true);
111
        make_cache_directory('cachestore_file_test');
112
 
113
        $cache->get('testing');
114
    }
115
 
116
    /**
117
     * Tests the get_last_read byte count.
118
     */
119
    public function test_get_last_io_bytes(): void {
1441 ariadna 120
        $definition = definition::load_adhoc(store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
1 efrain 121
        $store = new \cachestore_file('Test');
122
        $store->initialise($definition);
123
 
124
        $store->set('foo', 'bar');
125
        $store->set('frog', 'ribbit');
126
        $store->get('foo');
127
        // It's not 3 bytes, because the data is stored serialized.
128
        $this->assertEquals(10, $store->get_last_io_bytes());
129
        $store->get('frog');
130
        $this->assertEquals(13, $store->get_last_io_bytes());
131
        $store->get_many(['foo', 'frog']);
132
        $this->assertEquals(23, $store->get_last_io_bytes());
133
 
134
        $store->set('foo', 'goo');
135
        $this->assertEquals(10, $store->get_last_io_bytes());
136
        $store->set_many([
137
                ['key' => 'foo', 'value' => 'bar'],
138
                ['key' => 'frog', 'value' => 'jump']
139
        ]);
140
        $this->assertEquals(21, $store->get_last_io_bytes());
141
    }
142
 
11 efrain 143
    public function test_lock(): void {
1 efrain 144
        $store = new \cachestore_file('Test');
145
 
146
        $this->assertTrue($store->acquire_lock('lock', '123'));
147
        $this->assertTrue($store->check_lock_state('lock', '123'));
148
        $this->assertFalse($store->check_lock_state('lock', '321'));
149
        $this->assertNull($store->check_lock_state('notalock', '123'));
150
        $this->assertFalse($store->release_lock('lock', '321'));
151
        $this->assertTrue($store->release_lock('lock', '123'));
152
    }
153
}