[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/cache/stores/memcached/tests/ -> memcached_test.php (source)

   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  /**
  18   * Memcached unit tests.
  19   *
  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.
  22   *
  23   * define('TEST_CACHESTORE_MEMCACHED_TESTSERVERS', '127.0.0.1:11211');
  24   *
  25   * @package    cachestore_memcached
  26   * @copyright  2013 Sam Hemelryk
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  // Include the necessary evils.
  33  global $CFG;
  34  require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
  35  require_once($CFG->dirroot.'/cache/stores/memcached/lib.php');
  36  
  37  /**
  38   * Memcached unit test class.
  39   *
  40   * @package    cachestore_memcached
  41   * @copyright  2013 Sam Hemelryk
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class cachestore_memcached_test extends cachestore_tests {
  45      /**
  46       * Returns the memcached class name
  47       * @return string
  48       */
  49      protected function get_class_name() {
  50          return 'cachestore_memcached';
  51      }
  52  
  53      /**
  54       * Tests the valid keys to ensure they work.
  55       */
  56      public function test_valid_keys() {
  57          $this->resetAfterTest(true);
  58  
  59          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
  60          $instance = cachestore_memcached::initialise_unit_test_instance($definition);
  61  
  62          if (!$instance) { // Something prevented memcached store to be inited (extension, TEST_CACHESTORE_MEMCACHED_TESTSERVERS...).
  63              $this->markTestSkipped();
  64          }
  65  
  66          $keys = array(
  67              // Alphanumeric.
  68              'abc', 'ABC', '123', 'aB1', '1aB',
  69              // Hyphens.
  70              'a-1', '1-a', '-a1', 'a1-',
  71              // Underscores.
  72              'a_1', '1_a', '_a1', 'a1_'
  73          );
  74  
  75          // Set some keys.
  76          foreach ($keys as $key) {
  77              $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
  78          }
  79  
  80          // Get some keys.
  81          foreach ($keys as $key) {
  82              $this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
  83          }
  84  
  85          // Try get many.
  86          $values = $instance->get_many($keys);
  87          foreach ($values as $key => $value) {
  88              $this->assertEquals($key, $value);
  89          }
  90  
  91          // Reset a key.
  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`");
  94  
  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`");
  99          }
 100  
 101          // Try set many, and check that count is correct.
 102          $many = array();
 103          foreach ($keys as $key) {
 104              $many[] = array('key' => $key, 'value' => $key);
 105          }
 106          $returncount = $instance->set_many($many);
 107          $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
 108  
 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`");
 114          }
 115  
 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');
 119  
 120          // Check that each key was deleted.
 121          foreach ($keys as $key) {
 122              $this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
 123          }
 124  
 125          // Set the keys again.
 126          $returncount = $instance->set_many($many);
 127          $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
 128  
 129          // Purge.
 130          $this->assertTrue($instance->purge(), 'Failure to purge');
 131  
 132          // Delete and check that we can't retrieve.
 133          foreach ($keys as $key) {
 134              $this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
 135          }
 136      }
 137  
 138      /**
 139       * Tests the clustering feature.
 140       */
 141      public function test_clustered() {
 142          $this->resetAfterTest(true);
 143  
 144          if (!defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) {
 145              $this->markTestSkipped();
 146          }
 147  
 148          $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHED_TESTSERVERS));
 149  
 150          if (count($testservers) < 2) {
 151              $this->markTestSkipped();
 152          }
 153  
 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');
 162  
 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);
 166  
 167          if (!$instance) {
 168              $this->markTestSkipped();
 169          }
 170  
 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();
 182              }
 183              $checkinstances[] = $checkinstance;
 184          }
 185  
 186          $keys = array(
 187              // Alphanumeric.
 188              'abc', 'ABC', '123', 'aB1', '1aB',
 189              // Hyphens.
 190              'a-1', '1-a', '-a1', 'a1-',
 191              // Underscores.
 192              'a_1', '1_a', '_a1', 'a1_'
 193          );
 194  
 195          // Set each key.
 196          foreach ($keys as $key) {
 197              $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
 198          }
 199  
 200          // Check each 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");
 205              }
 206          }
 207  
 208          // Reset a key.
 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");
 213          }
 214  
 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");
 221              }
 222          }
 223  
 224          // Try set many, and check that count is correct.
 225          $many = array();
 226          foreach ($keys as $key) {
 227              $many[] = array('key' => $key, 'value' => $key);
 228          }
 229          $returncount = $instance->set_many($many);
 230          $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
 231  
 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`");
 237          }
 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");
 243              }
 244          }
 245  
 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');
 249  
 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");
 255              }
 256          }
 257  
 258          // Set the keys again.
 259          $returncount = $instance->set_many($many);
 260          $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
 261  
 262          // Purge.
 263          $this->assertTrue($instance->purge(), 'Failure to purge');
 264  
 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");
 270              }
 271          }
 272      }
 273  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1