Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 400 and 401]

   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  namespace cachestore_redis;
  18  
  19  use cache_store;
  20  use cache_definition;
  21  use cachestore_redis;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  require_once (__DIR__.'/../../../tests/fixtures/stores.php');
  26  require_once (__DIR__.'/../lib.php');
  27  
  28  /**
  29   * Redis cache test.
  30   *
  31   * If you wish to use these unit tests all you need to do is add the following definition to
  32   * your config.php file.
  33   *
  34   * define('TEST_CACHESTORE_REDIS_TESTSERVERS', '127.0.0.1');
  35   *
  36   * @package   cachestore_redis
  37   * @covers    \cachestore_redis
  38   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class store_test extends \cachestore_tests {
  42      /**
  43       * @var cachestore_redis
  44       */
  45      protected $store;
  46  
  47      /**
  48       * Returns the MongoDB class name
  49       *
  50       * @return string
  51       */
  52      protected function get_class_name() {
  53          return 'cachestore_redis';
  54      }
  55  
  56      public function setUp(): void {
  57          if (!cachestore_redis::are_requirements_met() || !defined('TEST_CACHESTORE_REDIS_TESTSERVERS')) {
  58              $this->markTestSkipped('Could not test cachestore_redis. Requirements are not met.');
  59          }
  60          parent::setUp();
  61      }
  62      protected function tearDown(): void {
  63          parent::tearDown();
  64  
  65          if ($this->store instanceof cachestore_redis) {
  66              $this->store->purge();
  67          }
  68      }
  69  
  70      /**
  71       * Creates the required cachestore for the tests to run against Redis.
  72       *
  73       * @param array $extraconfig Extra configuration options for Redis instance, if any
  74       * @param bool $ttl True to use a cache definition with TTL enabled
  75       * @return cachestore_redis
  76       */
  77      protected function create_cachestore_redis(array $extraconfig = [], bool $ttl = false): cachestore_redis {
  78          if ($ttl) {
  79              /** @var cache_definition $definition */
  80              $definition = cache_definition::load('core/wibble', [
  81                  'mode' => 1,
  82                  'simplekeys' => true,
  83                  'simpledata' => true,
  84                  'ttl' => 10,
  85                  'component' => 'core',
  86                  'area' => 'wibble',
  87                  'selectedsharingoption' => 2,
  88                  'userinputsharingkey' => '',
  89                  'sharingoptions' => 15,
  90              ]);
  91          } else {
  92              /** @var cache_definition $definition */
  93              $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
  94          }
  95          $configuration = array_merge(cachestore_redis::unit_test_configuration(), $extraconfig);
  96          $store = new cachestore_redis('Test', $configuration);
  97          $store->initialise($definition);
  98  
  99          $this->store = $store;
 100  
 101          if (!$store) {
 102              $this->markTestSkipped();
 103          }
 104  
 105          return $store;
 106      }
 107  
 108      public function test_has() {
 109          $store = $this->create_cachestore_redis();
 110  
 111          $this->assertTrue($store->set('foo', 'bar'));
 112          $this->assertTrue($store->has('foo'));
 113          $this->assertFalse($store->has('bat'));
 114      }
 115  
 116      public function test_has_any() {
 117          $store = $this->create_cachestore_redis();
 118  
 119          $this->assertTrue($store->set('foo', 'bar'));
 120          $this->assertTrue($store->has_any(array('bat', 'foo')));
 121          $this->assertFalse($store->has_any(array('bat', 'baz')));
 122      }
 123  
 124      public function test_has_all() {
 125          $store = $this->create_cachestore_redis();
 126  
 127          $this->assertTrue($store->set('foo', 'bar'));
 128          $this->assertTrue($store->set('bat', 'baz'));
 129          $this->assertTrue($store->has_all(array('foo', 'bat')));
 130          $this->assertFalse($store->has_all(array('foo', 'bat', 'this')));
 131      }
 132  
 133      public function test_lock() {
 134          $store = $this->create_cachestore_redis();
 135  
 136          $this->assertTrue($store->acquire_lock('lock', '123'));
 137          $this->assertTrue($store->check_lock_state('lock', '123'));
 138          $this->assertFalse($store->check_lock_state('lock', '321'));
 139          $this->assertNull($store->check_lock_state('notalock', '123'));
 140          $this->assertFalse($store->release_lock('lock', '321'));
 141          $this->assertTrue($store->release_lock('lock', '123'));
 142      }
 143  
 144      /**
 145       * Checks the timeout features of locking.
 146       */
 147      public function test_lock_timeouts(): void {
 148          $store = $this->create_cachestore_redis(['lockwait' => 2, 'locktimeout' => 4]);
 149  
 150          // User 123 acquires lock.
 151          $this->assertTrue($store->acquire_lock('lock', '123'));
 152          $this->assertTrue($store->check_lock_state('lock', '123'));
 153  
 154          // User 456 tries to acquire lock - should fail after about 2 seconds.
 155          $before = microtime(true);
 156          $this->assertFalse($store->acquire_lock('lock', '456'));
 157          $after = microtime(true);
 158          $this->assertEqualsWithDelta(2, $after - $before, 0.5);
 159  
 160          // Wait another 2 seconds and then it should be able to get the lock because of timeout.
 161          sleep(2);
 162          $this->assertTrue($store->acquire_lock('lock', '456'));
 163          $this->assertTrue($store->check_lock_state('lock', '456'));
 164  
 165          // The first user doesn't have the lock any more.
 166          $this->assertFalse($store->check_lock_state('lock', '123'));
 167  
 168          // Releasing the lock from the first user does nothing.
 169          $this->assertFalse($store->release_lock('lock', '123'));
 170          $this->assertTrue($store->check_lock_state('lock', '456'));
 171  
 172          $this->assertTrue($store->release_lock('lock', '456'));
 173      }
 174  
 175      /**
 176       * Tests the shutdown function that is supposed to free any remaining locks.
 177       */
 178      public function test_lock_shutdown(): void {
 179          $store = $this->create_cachestore_redis();
 180          try {
 181              $this->assertTrue($store->acquire_lock('a', '123'));
 182              $this->assertTrue($store->acquire_lock('b', '123'));
 183              $this->assertTrue($store->acquire_lock('c', '123'));
 184              $this->assertTrue($store->check_lock_state('a', '123'));
 185              $this->assertTrue($store->check_lock_state('b', '123'));
 186              $this->assertTrue($store->check_lock_state('c', '123'));
 187          } finally {
 188              $store->shutdown_release_locks();
 189              $this->assertDebuggingCalledCount(3);
 190          }
 191          $this->assertNull($store->check_lock_state('a', '123'));
 192          $this->assertNull($store->check_lock_state('b', '123'));
 193          $this->assertNull($store->check_lock_state('c', '123'));
 194      }
 195  
 196      /**
 197       * Tests the get_last_io_bytes function when not using compression (just returns unknown).
 198       */
 199      public function test_get_last_io_bytes(): void {
 200          $store = $this->create_cachestore_redis();
 201  
 202          $store->set('foo', [1, 2, 3, 4]);
 203          $this->assertEquals(\cache_store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
 204          $store->get('foo');
 205          $this->assertEquals(\cache_store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
 206      }
 207  
 208      /**
 209       * Tests the get_last_io_bytes byte count when using compression.
 210       */
 211      public function test_get_last_io_bytes_compressed(): void {
 212          $store = $this->create_cachestore_redis(['compressor' => cachestore_redis::COMPRESSOR_PHP_GZIP]);
 213  
 214          $alphabet = 'abcdefghijklmnopqrstuvwxyz';
 215  
 216          $store->set('small', $alphabet);
 217          $store->set('large', str_repeat($alphabet, 10));
 218  
 219          $store->get('small');
 220          // Interesting 'compression'.
 221          $this->assertEquals(54, $store->get_last_io_bytes());
 222          $store->get('large');
 223          // This one is actually smaller than uncompressed value!
 224          $this->assertEquals(57, $store->get_last_io_bytes());
 225          $store->get_many(['small', 'large']);
 226          $this->assertEquals(111, $store->get_last_io_bytes());
 227  
 228          $store->set('small', str_repeat($alphabet, 2));
 229          $this->assertEquals(56, $store->get_last_io_bytes());
 230          $store->set_many([
 231                  ['key' => 'small', 'value' => $alphabet],
 232                  ['key' => 'large', 'value' => str_repeat($alphabet, 10)]
 233          ]);
 234          $this->assertEquals(111, $store->get_last_io_bytes());
 235      }
 236  
 237      /**
 238       * Data provider for whether cache uses TTL or not.
 239       *
 240       * @return array Array with true and false options
 241       */
 242      public static function ttl_or_not(): array {
 243          return [
 244              [false],
 245              [true]
 246          ];
 247      }
 248  
 249      /**
 250       * Tests the delete_many function.
 251       *
 252       * The behaviour is different with TTL enabled so we need to test with that kind of definition
 253       * as well as a 'normal' one.
 254       *
 255       * @param bool $ttl True to test using a TTL definition
 256       * @dataProvider ttl_or_not
 257       */
 258      public function test_delete_many(bool $ttl): void {
 259          $store = $this->create_cachestore_redis([], $ttl);
 260  
 261          // Check it works to delete selected items.
 262          $store->set('foo', 'frog');
 263          $store->set('bar', 'amphibian');
 264          $store->set('hmm', 'undead');
 265          $this->store->delete_many(['foo', 'bar']);
 266          $this->assertFalse($store->get('foo'));
 267          $this->assertFalse($store->get('bar'));
 268          $this->assertEquals('undead', $store->get('hmm'));
 269  
 270          // If called with no keys it should do nothing.
 271          $store->delete_many([]);
 272          $this->assertEquals('undead', $store->get('hmm'));
 273      }
 274  
 275  }