2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Memcache unit tests.
20 * If you wish to use these unit tests all you need to do is add the following definition to
21 * your config.php file.
23 * define('TEST_CACHESTORE_MEMCACHE_TESTSERVERS', '127.0.0.1:11211');
25 * @package cachestore_memcache
26 * @copyright 2013 Sam Hemelryk
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 // Include the necessary evils.
34 require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
35 require_once($CFG->dirroot.'/cache/stores/memcache/lib.php');
38 * Memcache unit test class.
40 * @package cachestore_memcache
41 * @copyright 2013 Sam Hemelryk
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class cachestore_memcache_test extends cachestore_tests {
46 * Returns the memcache class name
49 protected function get_class_name() {
50 return 'cachestore_memcache';
54 * Tests the valid keys to ensure they work.
56 public function test_valid_keys() {
57 $this->resetAfterTest(true);
59 $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test');
60 $instance = cachestore_memcache::initialise_unit_test_instance($definition);
62 if (!$instance) { // Something prevented memcache store to be inited (extension, TEST_CACHESTORE_MEMCACHE_TESTSERVERS...).
63 $this->markTestSkipped();
68 'abc', 'ABC', '123', 'aB1', '1aB',
70 'a-1', '1-a', '-a1', 'a1-',
72 'a_1', '1_a', '_a1', 'a1_'
76 foreach ($keys as $key) {
77 $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
81 foreach ($keys as $key) {
82 $this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
86 $values = $instance->get_many($keys);
87 foreach ($values as $key => $value) {
88 $this->assertEquals($key, $value);
92 $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
93 $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
95 // Delete and check that we can't retrieve.
96 foreach ($keys as $key) {
97 $this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
98 $this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
101 // Try set many, and check that count is correct.
103 foreach ($keys as $key) {
104 $many[] = array('key' => $key, 'value' => $key);
106 $returncount = $instance->set_many($many);
107 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
109 // Check keys retrieved with get_many.
110 $values = $instance->get_many($keys);
111 foreach ($keys as $key) {
112 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
113 $this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
116 // Delete many, make sure count matches.
117 $returncount = $instance->delete_many($keys);
118 $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
120 // Check that each key was deleted.
121 foreach ($keys as $key) {
122 $this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
125 // Set the keys again.
126 $returncount = $instance->set_many($many);
127 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
130 $this->assertTrue($instance->purge(), 'Failure to purge');
132 // Delete and check that we can't retrieve.
133 foreach ($keys as $key) {
134 $this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
139 * Tests the clustering feature.
141 public function test_clustered() {
142 $this->resetAfterTest(true);
144 $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHE_TESTSERVERS));
146 if (count($testservers) < 2) {
147 $this->markTestSkipped();
150 // User the first server as our primary.
151 set_config('testservers', $testservers[0], 'cachestore_memcache');
152 set_config('testsetservers', TEST_CACHESTORE_MEMCACHE_TESTSERVERS, 'cachestore_memcache');
153 set_config('testclustered', true, 'cachestore_memcache');
155 // First and instance that we can use to test the second server.
156 $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test');
157 $instance = cachestore_memcache::initialise_test_instance($definition);
160 $this->markTestSkipped();
163 // Now we are going to setup a connection to each independent server.
164 set_config('testclustered', false, 'cachestore_memcache');
165 set_config('testsetservers', '', 'cachestore_memcache');
166 $checkinstances = array();
167 foreach ($testservers as $testserver) {
168 set_config('testservers', $testserver, 'cachestore_memcache');
169 $checkinstance = cachestore_memcache::initialise_test_instance($definition);
170 if (!$checkinstance) {
171 $this->markTestSkipped();
173 $checkinstances[] = $checkinstance;
178 'abc', 'ABC', '123', 'aB1', '1aB',
180 'a-1', '1-a', '-a1', 'a1-',
182 'a_1', '1_a', '_a1', 'a1_'
186 foreach ($keys as $key) {
187 $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
191 foreach ($keys as $key) {
192 $this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
193 foreach ($checkinstances as $id => $checkinstance) {
194 $this->assertEquals($key, $checkinstance->get($key), "Failed to get key `$key` from server $id");
199 $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
200 $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
201 foreach ($checkinstances as $id => $checkinstance) {
202 $this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `$key` from server $id");
205 // Delete and check that we can't retrieve.
206 foreach ($keys as $key) {
207 $this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
208 $this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
209 foreach ($checkinstances as $id => $checkinstance) {
210 $this->assertFalse($checkinstance->get($key), "Retrieved deleted key `$key` from server $id");
214 // Try set many, and check that count is correct.
216 foreach ($keys as $key) {
217 $many[] = array('key' => $key, 'value' => $key);
219 $returncount = $instance->set_many($many);
220 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
222 // Check keys retrieved with get_many.
223 $values = $instance->get_many($keys);
224 foreach ($keys as $key) {
225 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
226 $this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
228 foreach ($checkinstances as $id => $checkinstance) {
229 $values = $checkinstance->get_many($keys);
230 foreach ($keys as $key) {
231 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key` from server $id");
232 $this->assertEquals($key, $values[$key], "Failed to get_many key `$key` from server $id");
236 // Delete many, make sure count matches.
237 $returncount = $instance->delete_many($keys);
238 $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
240 // Check that each key was deleted.
241 foreach ($keys as $key) {
242 $this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
243 foreach ($checkinstances as $id => $checkinstance) {
244 $this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `$key` from server $id");
248 // Set the keys again.
249 $returncount = $instance->set_many($many);
250 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
253 $this->assertTrue($instance->purge(), 'Failure to purge');
255 // Delete and check that we can't retrieve.
256 foreach ($keys as $key) {
257 $this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
258 foreach ($checkinstances as $id => $checkinstance) {
259 $this->assertFalse($checkinstance->get($key), "Retrieved purged key `$key` from server 2");