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 * Memcached 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_MEMCACHED_TESTSERVERS', '127.0.0.1:11211');
25 * @package cachestore_memcached
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/memcached/lib.php');
38 * Memcached unit test class.
40 * @package cachestore_memcached
41 * @copyright 2013 Sam Hemelryk
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class cachestore_memcached_test extends cachestore_tests {
46 * Returns the memcached class name
49 protected function get_class_name() {
50 return 'cachestore_memcached';
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_memcached', 'phpunit_test');
60 $instance = cachestore_memcached::initialise_unit_test_instance($definition);
62 if (!$instance) { // Something prevented memcached store to be inited (extension, TEST_CACHESTORE_MEMCACHED_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 if (!defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) {
145 $this->markTestSkipped();
148 $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHED_TESTSERVERS));
150 if (count($testservers) < 2) {
151 $this->markTestSkipped();
154 // Use the first server as our primary.
155 // We need to set a prefix for all, otherwise it uses the name, which will not match between connections.
156 set_config('testprefix', 'pre', 'cachestore_memcached');
157 // We need to set a name, otherwise we get a reused connection.
158 set_config('testname', 'cluster', 'cachestore_memcached');
159 set_config('testservers', $testservers[0], 'cachestore_memcached');
160 set_config('testsetservers', TEST_CACHESTORE_MEMCACHED_TESTSERVERS, 'cachestore_memcached');
161 set_config('testclustered', true, 'cachestore_memcached');
163 // First and instance that we can use to test the second server.
164 $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
165 $instance = cachestore_memcached::initialise_test_instance($definition);
168 $this->markTestSkipped();
171 // Now we are going to setup a connection to each independent server.
172 set_config('testclustered', false, 'cachestore_memcached');
173 set_config('testsetservers', '', 'cachestore_memcached');
174 $checkinstances = array();
175 foreach ($testservers as $testserver) {
176 // We need to set a name, otherwise we get a reused connection.
177 set_config('testname', $testserver, 'cachestore_memcached');
178 set_config('testservers', $testserver, 'cachestore_memcached');
179 $checkinstance = cachestore_memcached::initialise_test_instance($definition);
180 if (!$checkinstance) {
181 $this->markTestSkipped();
183 $checkinstances[] = $checkinstance;
188 'abc', 'ABC', '123', 'aB1', '1aB',
190 'a-1', '1-a', '-a1', 'a1-',
192 'a_1', '1_a', '_a1', 'a1_'
196 foreach ($keys as $key) {
197 $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
201 foreach ($keys as $key) {
202 $this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
203 foreach ($checkinstances as $id => $checkinstance) {
204 $this->assertEquals($key, $checkinstance->get($key), "Failed to get key `$key` from server $id");
209 $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
210 $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
211 foreach ($checkinstances as $id => $checkinstance) {
212 $this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `$key` from server $id");
215 // Delete and check that we can't retrieve.
216 foreach ($keys as $key) {
217 $this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
218 $this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
219 foreach ($checkinstances as $id => $checkinstance) {
220 $this->assertFalse($checkinstance->get($key), "Retrieved deleted key `$key` from server $id");
224 // Try set many, and check that count is correct.
226 foreach ($keys as $key) {
227 $many[] = array('key' => $key, 'value' => $key);
229 $returncount = $instance->set_many($many);
230 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
232 // Check keys retrieved with get_many.
233 $values = $instance->get_many($keys);
234 foreach ($keys as $key) {
235 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
236 $this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
238 foreach ($checkinstances as $id => $checkinstance) {
239 $values = $checkinstance->get_many($keys);
240 foreach ($keys as $key) {
241 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key` from server $id");
242 $this->assertEquals($key, $values[$key], "Failed to get_many key `$key` from server $id");
246 // Delete many, make sure count matches.
247 $returncount = $instance->delete_many($keys);
248 $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
250 // Check that each key was deleted.
251 foreach ($keys as $key) {
252 $this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
253 foreach ($checkinstances as $id => $checkinstance) {
254 $this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `$key` from server $id");
258 // Set the keys again.
259 $returncount = $instance->set_many($many);
260 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
263 $this->assertTrue($instance->purge(), 'Failure to purge');
265 // Delete and check that we can't retrieve.
266 foreach ($keys as $key) {
267 $this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
268 foreach ($checkinstances as $id => $checkinstance) {
269 $this->assertFalse($checkinstance->get($key), "Retrieved purged key `$key` from server 2");