[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/cache/stores/memcache/tests/ -> memcache_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   * Memcache 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_MEMCACHE_TESTSERVERS', '127.0.0.1:11211');
  24   *
  25   * @package    cachestore_memcache
  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/memcache/lib.php');
  36  
  37  /**
  38   * Memcache unit test class.
  39   *
  40   * @package    cachestore_memcache
  41   * @copyright  2013 Sam Hemelryk
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class cachestore_memcache_test extends cachestore_tests {
  45      /**
  46       * Returns the memcache class name
  47       * @return string
  48       */
  49      protected function get_class_name() {
  50          return 'cachestore_memcache';
  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_memcache', 'phpunit_test');
  60          $instance = cachestore_memcache::initialise_unit_test_instance($definition);
  61  
  62          if (!$instance) { // Something prevented memcache store to be inited (extension, TEST_CACHESTORE_MEMCACHE_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_MEMCACHE_TESTSERVERS')) {
 145              $this->markTestSkipped();
 146          }
 147  
 148          $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHE_TESTSERVERS));
 149  
 150          if (count($testservers) < 2) {
 151              $this->markTestSkipped();
 152          }
 153  
 154          // User the first server as our primary.
 155          set_config('testservers', $testservers[0], 'cachestore_memcache');
 156          set_config('testsetservers', TEST_CACHESTORE_MEMCACHE_TESTSERVERS, 'cachestore_memcache');
 157          set_config('testclustered', true, 'cachestore_memcache');
 158  
 159          // First and instance that we can use to test the second server.
 160          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test');
 161          $instance = cachestore_memcache::initialise_test_instance($definition);
 162  
 163          if (!$instance) {
 164              $this->markTestSkipped();
 165          }
 166  
 167          // Now we are going to setup a connection to each independent server.
 168          set_config('testclustered', false, 'cachestore_memcache');
 169          set_config('testsetservers', '', 'cachestore_memcache');
 170          $checkinstances = array();
 171          foreach ($testservers as $testserver) {
 172              set_config('testservers', $testserver, 'cachestore_memcache');
 173              $checkinstance = cachestore_memcache::initialise_test_instance($definition);
 174              if (!$checkinstance) {
 175                  $this->markTestSkipped();
 176              }
 177              $checkinstances[] = $checkinstance;
 178          }
 179  
 180          $keys = array(
 181              // Alphanumeric.
 182              'abc', 'ABC', '123', 'aB1', '1aB',
 183              // Hyphens.
 184              'a-1', '1-a', '-a1', 'a1-',
 185              // Underscores.
 186              'a_1', '1_a', '_a1', 'a1_'
 187          );
 188  
 189          // Set each key.
 190          foreach ($keys as $key) {
 191              $this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
 192          }
 193  
 194          // Check each key.
 195          foreach ($keys as $key) {
 196              $this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
 197              foreach ($checkinstances as $id => $checkinstance) {
 198                  $this->assertEquals($key, $checkinstance->get($key), "Failed to get key `$key` from server $id");
 199              }
 200          }
 201  
 202          // Reset a key.
 203          $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
 204          $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
 205          foreach ($checkinstances as $id => $checkinstance) {
 206              $this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `$key` from server $id");
 207          }
 208  
 209          // Delete and check that we can't retrieve.
 210          foreach ($keys as $key) {
 211              $this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
 212              $this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
 213              foreach ($checkinstances as $id => $checkinstance) {
 214                  $this->assertFalse($checkinstance->get($key), "Retrieved deleted key `$key` from server $id");
 215              }
 216          }
 217  
 218          // Try set many, and check that count is correct.
 219          $many = array();
 220          foreach ($keys as $key) {
 221              $many[] = array('key' => $key, 'value' => $key);
 222          }
 223          $returncount = $instance->set_many($many);
 224          $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
 225  
 226          // Check keys retrieved with get_many.
 227          $values = $instance->get_many($keys);
 228          foreach ($keys as $key) {
 229              $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
 230              $this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
 231          }
 232          foreach ($checkinstances as $id => $checkinstance) {
 233              $values = $checkinstance->get_many($keys);
 234              foreach ($keys as $key) {
 235                  $this->assertTrue(isset($values[$key]), "Failed to get_many key `$key` from server $id");
 236                  $this->assertEquals($key, $values[$key], "Failed to get_many key `$key` from server $id");
 237              }
 238          }
 239  
 240          // Delete many, make sure count matches.
 241          $returncount = $instance->delete_many($keys);
 242          $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
 243  
 244          // Check that each key was deleted.
 245          foreach ($keys as $key) {
 246              $this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
 247              foreach ($checkinstances as $id => $checkinstance) {
 248                  $this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `$key` from server $id");
 249              }
 250          }
 251  
 252          // Set the keys again.
 253          $returncount = $instance->set_many($many);
 254          $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
 255  
 256          // Purge.
 257          $this->assertTrue($instance->purge(), 'Failure to purge');
 258  
 259          // Delete and check that we can't retrieve.
 260          foreach ($keys as $key) {
 261              $this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
 262              foreach ($checkinstances as $id => $checkinstance) {
 263                  $this->assertFalse($checkinstance->get($key), "Retrieved purged key `$key` from server 2");
 264              }
 265          }
 266      }
 267  
 268      /**
 269       * Test our checks for encoding.
 270       */
 271      public function test_require_encoding() {
 272          $this->assertTrue(cachestore_memcache::require_encoding('dev'));
 273          $this->assertTrue(cachestore_memcache::require_encoding('1.0'));
 274          $this->assertTrue(cachestore_memcache::require_encoding('1.0.0'));
 275          $this->assertTrue(cachestore_memcache::require_encoding('2.0'));
 276          $this->assertTrue(cachestore_memcache::require_encoding('2.0.8'));
 277          $this->assertTrue(cachestore_memcache::require_encoding('2.2.8'));
 278          $this->assertTrue(cachestore_memcache::require_encoding('3.0'));
 279          $this->assertTrue(cachestore_memcache::require_encoding('3.0-dev'));
 280          $this->assertTrue(cachestore_memcache::require_encoding('3.0.0'));
 281          $this->assertTrue(cachestore_memcache::require_encoding('3.0.1'));
 282          $this->assertTrue(cachestore_memcache::require_encoding('3.0.2-dev'));
 283          $this->assertTrue(cachestore_memcache::require_encoding('3.0.2'));
 284          $this->assertTrue(cachestore_memcache::require_encoding('3.0.3-dev'));
 285          $this->assertFalse(cachestore_memcache::require_encoding('3.0.3'));
 286          $this->assertFalse(cachestore_memcache::require_encoding('3.0.4'));
 287          $this->assertFalse(cachestore_memcache::require_encoding('3.0.4-dev'));
 288          $this->assertFalse(cachestore_memcache::require_encoding('3.0.8'));
 289          $this->assertFalse(cachestore_memcache::require_encoding('3.1.0'));
 290          $this->assertFalse(cachestore_memcache::require_encoding('3.1.2'));
 291  
 292      }
 293  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1