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 $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHED_TESTSERVERS));
146 if (count($testservers) < 2) {
147 $this->markTestSkipped();
150 // Use the first server as our primary.
151 // We need to set a prefix for all, otherwise it uses the name, which will not match between connections.
152 set_config('testprefix', 'pre', 'cachestore_memcached');
153 // We need to set a name, otherwise we get a reused connection.
154 set_config('testname', 'cluster', 'cachestore_memcached');
155 set_config('testservers', $testservers[0], 'cachestore_memcached');
156 set_config('testsetservers', TEST_CACHESTORE_MEMCACHED_TESTSERVERS, 'cachestore_memcached');
157 set_config('testclustered', true, 'cachestore_memcached');
159 // First and instance that we can use to test the second server.
160 $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
161 $instance = cachestore_memcached::initialise_test_instance($definition);
164 $this->markTestSkipped();
167 // Now we are going to setup a connection to each independent server.
168 set_config('testclustered', false, 'cachestore_memcached');
169 set_config('testsetservers', '', 'cachestore_memcached');
170 $checkinstances = array();
171 foreach ($testservers as $testserver) {
172 // We need to set a name, otherwise we get a reused connection.
173 set_config('testname', $testserver, 'cachestore_memcached');
174 set_config('testservers', $testserver, 'cachestore_memcached');
175 $checkinstance = cachestore_memcached::initialise_test_instance($definition);
176 if (!$checkinstance) {
177 $this->markTestSkipped();
179 $checkinstances[] = $checkinstance;
184 'abc', 'ABC', '123', 'aB1', '1aB',
186 'a-1', '1-a', '-a1', 'a1-',
188 'a_1', '1_a', '_a1', 'a1_'
192 foreach ($keys as $key) {
193 $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
197 foreach ($keys as $key) {
198 $this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
199 foreach ($checkinstances as $id => $checkinstance) {
200 $this->assertEquals($key, $checkinstance->get($key), "Failed to get key `$key` from server $id");
205 $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
206 $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
207 foreach ($checkinstances as $id => $checkinstance) {
208 $this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `$key` from server $id");
211 // Delete and check that we can't retrieve.
212 foreach ($keys as $key) {
213 $this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
214 $this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
215 foreach ($checkinstances as $id => $checkinstance) {
216 $this->assertFalse($checkinstance->get($key), "Retrieved deleted key `$key` from server $id");
220 // Try set many, and check that count is correct.
222 foreach ($keys as $key) {
223 $many[] = array('key' => $key, 'value' => $key);
225 $returncount = $instance->set_many($many);
226 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
228 // Check keys retrieved with get_many.
229 $values = $instance->get_many($keys);
230 foreach ($keys as $key) {
231 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
232 $this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
234 foreach ($checkinstances as $id => $checkinstance) {
235 $values = $checkinstance->get_many($keys);
236 foreach ($keys as $key) {
237 $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key` from server $id");
238 $this->assertEquals($key, $values[$key], "Failed to get_many key `$key` from server $id");
242 // Delete many, make sure count matches.
243 $returncount = $instance->delete_many($keys);
244 $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
246 // Check that each key was deleted.
247 foreach ($keys as $key) {
248 $this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
249 foreach ($checkinstances as $id => $checkinstance) {
250 $this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `$key` from server $id");
254 // Set the keys again.
255 $returncount = $instance->set_many($many);
256 $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
259 $this->assertTrue($instance->purge(), 'Failure to purge');
261 // Delete and check that we can't retrieve.
262 foreach ($keys as $key) {
263 $this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
264 foreach ($checkinstances as $id => $checkinstance) {
265 $this->assertFalse($checkinstance->get($key), "Retrieved purged key `$key` from server 2");