/** * Run the unit tests for the store. */ public function test_test_instance() { $class = $this->get_class_name(); if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) { $this->markTestSkipped('Could not test ' . $class . '. Requirements are not met.'); } $modes = $class::get_supported_modes(); if ($modes & cache_store::MODE_APPLICATION) { $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, $class, 'phpunit_test'); $instance = $class::initialise_unit_test_instance($definition); if (!$instance) { $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for application caches.'); } else { $this->run_tests($instance); } } if ($modes & cache_store::MODE_SESSION) { $definition = cache_definition::load_adhoc(cache_store::MODE_SESSION, $class, 'phpunit_test'); $instance = $class::initialise_unit_test_instance($definition); if (!$instance) { $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for session caches.'); } else { $this->run_tests($instance); } } if ($modes & cache_store::MODE_REQUEST) { $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $class, 'phpunit_test'); $instance = $class::initialise_unit_test_instance($definition); if (!$instance) { $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for request caches.'); } else { $this->run_tests($instance); } } }
/** * Test purging the apcu cache store. */ public function test_purge() { if (!cachestore_apcu::are_requirements_met()) { $this->markTestSkipped('Could not test cachestore_apcu. Requirements are not met.'); } $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test'); $instance = cachestore_apcu::initialise_unit_test_instance($definition); // Test a simple purge return. $this->assertTrue($instance->purge()); // Test purge works. $this->assertTrue($instance->set('test', 'monster')); $this->assertSame('monster', $instance->get('test')); $this->assertTrue($instance->purge()); $this->assertFalse($instance->get('test')); // Test purge with custom data. $this->assertTrue($instance->set('test', 'monster')); $this->assertSame('monster', $instance->get('test')); $this->assertTrue(apcu_store('test', 'pirate', 180)); $this->assertSame('monster', $instance->get('test')); $this->assertTrue(apcu_exists('test')); $this->assertSame('pirate', apcu_fetch('test')); // Purge and check that our data is gone but the the custom data is still there. $this->assertTrue($instance->purge()); $this->assertFalse($instance->get('test')); $this->assertTrue(apcu_exists('test')); $this->assertSame('pirate', apcu_fetch('test')); }
/** * Run the unit tests for the store. */ public function test_test_instance() { $class = $this->get_class_name(); $modes = $class::get_supported_modes(); if ($modes & cache_store::MODE_APPLICATION) { $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, $class, 'phpunit_test'); $instance = new $class($class . '_test', $class::unit_test_configuration()); if (!$instance->is_ready()) { $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for application caches.'); } else { $instance->initialise($definition); $this->run_tests($instance); } } if ($modes & cache_store::MODE_SESSION) { $definition = cache_definition::load_adhoc(cache_store::MODE_SESSION, $class, 'phpunit_test'); $instance = new $class($class . '_test', $class::unit_test_configuration()); if (!$instance->is_ready()) { $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for session caches.'); } else { $instance->initialise($definition); $this->run_tests($instance); } } if ($modes & cache_store::MODE_REQUEST) { $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $class, 'phpunit_test'); $instance = new $class($class . '_test', $class::unit_test_configuration()); if (!$instance->is_ready()) { $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for request caches.'); } else { $instance->initialise($definition); $this->run_tests($instance); } } }
protected function setUp() { if (!defined('CACHESTORE_REDIS_TEST_SERVER')) { $this->markTestSkipped('Must define CACHESTORE_REDIS_TEST_SERVER to test Redis cache store'); } if (!cachestore_redis::are_requirements_met()) { $this->markTestSkipped('Requirements for Redis cache store are not met'); } $this->store = new cachestore_redis('test', array('server' => CACHESTORE_REDIS_TEST_SERVER, 'prefix' => 'phpunit')); $this->store->initialise(cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'foo_bar', 'baz')); }
public function test_different_caches_have_different_prefixes() { $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test'); $instance = cachestore_apcu::initialise_unit_test_instance($definition); $definition2 = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test2'); $instance2 = cachestore_apcu::initialise_unit_test_instance($definition2); $instance->set('test1', 1); $this->assertFalse($instance2->get('test1')); $instance2->purge(); $this->assertSame(1, $instance->get('test1')); }
/** * Creates the required cachestore for the tests to run against Redis. * * @return cachestore_redis */ protected function create_cachestore_redis() { /** @var cache_definition $definition */ $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test'); $store = new cachestore_redis('Test', cachestore_redis::unit_test_configuration()); $store->initialise($definition); $this->store = $store; if (!$store) { $this->markTestSkipped(); } return $store; }
/** * A small additional test to make sure definitions that hash a hash starting with a number work OK */ public function test_collection_name() { // This generates a definition that has a hash starting with a number. MDL-46208. $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_mongodb', 'abc'); $instance = cachestore_mongodb::initialise_test_instance($definition); if (!$instance) { $this->markTestSkipped(); } $this->assertTrue($instance->set(1, 'alpha')); $this->assertTrue($instance->set(2, 'beta')); $this->assertEquals('alpha', $instance->get(1)); $this->assertEquals('beta', $instance->get(2)); $this->assertEquals(array(1 => 'alpha', 2 => 'beta'), $instance->get_many(array(1, 2))); }
/** * Testing cachestore_file::get with prescan enabled and with * deleting the cache between the prescan and the call to get. * * The deleting of cache simulates some other process purging * the cache. */ public function test_cache_get_with_prescan_and_purge() { global $CFG; $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cachestore_file', 'phpunit_test'); $name = 'File test'; $path = make_cache_directory('cachestore_file_test'); $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true)); $cache->initialise($definition); $cache->set('testing', 'value'); $path = make_cache_directory('cachestore_file_test'); $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true)); $cache->initialise($definition); // Let's pretend that some other process purged caches. remove_dir($CFG->cachedir . '/cachestore_file_test', true); make_cache_directory('cachestore_file_test'); $cache->get('testing'); }
/** * Tests the valid keys to ensure they work. */ public function test_valid_keys() { $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test'); $instance = cachestore_memcached::initialise_test_instance($definition); if (!$instance) { // Something prevented memcached store to be inited (extension, TEST_CACHESTORE_MEMCACHED_TESTSERVERS...). $this->markTestSkipped(); } $keys = array('abc', 'ABC', '123', 'aB1', '1aB', 'a-1', '1-a', '-a1', 'a1-', 'a_1', '1_a', '_a1', 'a1_'); foreach ($keys as $key) { $this->assertTrue($instance->set($key, $key), "Failed to set key `{$key}`"); } foreach ($keys as $key) { $this->assertEquals($key, $instance->get($key), "Failed to get key `{$key}`"); } $values = $instance->get_many($keys); foreach ($values as $key => $value) { $this->assertEquals($key, $value); } }
/** * Creates a definition instance or returns the existing one if it has already been created. * @param string $component * @param string $area * @param string $aggregate * @return cache_definition */ public function create_definition($component, $area, $aggregate = null) { $id = $component . '/' . $area; if ($aggregate) { $id .= '::' . $aggregate; } if (!array_key_exists($id, $this->definitions)) { // This is the first time this definition has been requested. if ($this->is_initialising()) { // We're initialising the cache right now. Don't try to create another config instance. // We'll just use an ad-hoc cache for the time being. $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area); } else { // Load all the known definitions and find the desired one. $instance = $this->create_config_instance(); $definition = $instance->get_definition_by_id($id); if (!$definition) { // Oh-oh the definition doesn't exist. // There are several things that could be going on here. // We may be installing/upgrading a site and have hit a definition that hasn't been used before. // Of the developer may be trying to use a newly created definition. if ($this->is_updating()) { // The cache is presently initialising and the requested cache definition has not been found. // This means that the cache initialisation has requested something from a cache (I had recursive nightmares about this). // To serve this purpose and avoid errors we are going to make use of an ad-hoc cache rather than // search for the definition which would possibly cause an infitite loop trying to initialise the cache. $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area); if ($aggregate !== null) { // If you get here you deserve a warning. We have to use an ad-hoc cache here, so we can't find the definition and therefor // can't find any information about the datasource or any of its aggregated. // Best of luck. debugging('An unknown cache was requested during development with an aggregate that could not be loaded. Ad-hoc cache used instead.', DEBUG_DEVELOPER); $aggregate = null; } } else { // Either a typo of the developer has just created the definition and is using it for the first time. $this->reset(); $instance = $this->create_config_instance(true); $instance->update_definitions(); $definition = $instance->get_definition_by_id($id); if (!$definition) { throw new coding_exception('The requested cache definition does not exist.' . $id, $id); } else { if (!$this->is_disabled()) { debugging('Cache definitions reparsed causing cache reset in order to locate definition. You should bump the version number to ensure definitions are reprocessed.', DEBUG_DEVELOPER); } } $definition = cache_definition::load($id, $definition, $aggregate); } } else { $definition = cache_definition::load($id, $definition, $aggregate); } } $this->definitions[$id] = $definition; } return $this->definitions[$id]; }
/** * Tests the clustering feature. */ public function test_clustered() { $this->resetAfterTest(true); if (!defined('TEST_CACHESTORE_MEMCACHE_TESTSERVERS')) { $this->markTestSkipped(); } $testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHE_TESTSERVERS)); if (count($testservers) < 2) { $this->markTestSkipped(); } // User the first server as our primary. set_config('testservers', $testservers[0], 'cachestore_memcache'); set_config('testsetservers', TEST_CACHESTORE_MEMCACHE_TESTSERVERS, 'cachestore_memcache'); set_config('testclustered', true, 'cachestore_memcache'); // First and instance that we can use to test the second server. $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test'); $instance = cachestore_memcache::initialise_test_instance($definition); if (!$instance) { $this->markTestSkipped(); } // Now we are going to setup a connection to each independent server. set_config('testclustered', false, 'cachestore_memcache'); set_config('testsetservers', '', 'cachestore_memcache'); $checkinstances = array(); foreach ($testservers as $testserver) { set_config('testservers', $testserver, 'cachestore_memcache'); $checkinstance = cachestore_memcache::initialise_test_instance($definition); if (!$checkinstance) { $this->markTestSkipped(); } $checkinstances[] = $checkinstance; } $keys = array('abc', 'ABC', '123', 'aB1', '1aB', 'a-1', '1-a', '-a1', 'a1-', 'a_1', '1_a', '_a1', 'a1_'); // Set each key. foreach ($keys as $key) { $this->assertTrue($instance->set($key, $key), "Failed to set key `{$key}`"); } // Check each key. foreach ($keys as $key) { $this->assertEquals($key, $instance->get($key), "Failed to get key `{$key}`"); foreach ($checkinstances as $id => $checkinstance) { $this->assertEquals($key, $checkinstance->get($key), "Failed to get key `{$key}` from server {$id}"); } } // Reset a key. $this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `{$key}`"); $this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `{$key}`"); foreach ($checkinstances as $id => $checkinstance) { $this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `{$key}` from server {$id}"); } // Delete and check that we can't retrieve. foreach ($keys as $key) { $this->assertTrue($instance->delete($key), "Failed to delete key `{$key}`"); $this->assertFalse($instance->get($key), "Retrieved deleted key `{$key}`"); foreach ($checkinstances as $id => $checkinstance) { $this->assertFalse($checkinstance->get($key), "Retrieved deleted key `{$key}` from server {$id}"); } } // Try set many, and check that count is correct. $many = array(); foreach ($keys as $key) { $many[] = array('key' => $key, 'value' => $key); } $returncount = $instance->set_many($many); $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match'); // Check keys retrieved with get_many. $values = $instance->get_many($keys); foreach ($keys as $key) { $this->assertTrue(isset($values[$key]), "Failed to get_many key `{$key}`"); $this->assertEquals($key, $values[$key], "Failed to match get_many key `{$key}`"); } foreach ($checkinstances as $id => $checkinstance) { $values = $checkinstance->get_many($keys); foreach ($keys as $key) { $this->assertTrue(isset($values[$key]), "Failed to get_many key `{$key}` from server {$id}"); $this->assertEquals($key, $values[$key], "Failed to get_many key `{$key}` from server {$id}"); } } // Delete many, make sure count matches. $returncount = $instance->delete_many($keys); $this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match'); // Check that each key was deleted. foreach ($keys as $key) { $this->assertFalse($instance->get($key), "Retrieved many deleted key `{$key}`"); foreach ($checkinstances as $id => $checkinstance) { $this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `{$key}` from server {$id}"); } } // Set the keys again. $returncount = $instance->set_many($many); $this->assertEquals(count($many), $returncount, 'Set many count didn\'t match'); // Purge. $this->assertTrue($instance->purge(), 'Failure to purge'); // Delete and check that we can't retrieve. foreach ($keys as $key) { $this->assertFalse($instance->get($key), "Retrieved purged key `{$key}`"); foreach ($checkinstances as $id => $checkinstance) { $this->assertFalse($checkinstance->get($key), "Retrieved purged key `{$key}` from server 2"); } } }
/** * Tests that memcached cache store flushes entire cache when it is using a dedicated cache. */ public function test_dedicated_cache() { if (!cachestore_memcached::are_requirements_met() || !defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) { $this->markTestSkipped('Could not test cachestore_memcached. Requirements are not met.'); } $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test'); $cachestore = $this->create_test_cache_with_config($definition, array('isshared' => false)); $connection = new Memcached(crc32(__METHOD__)); $connection->addServers($this->get_servers(TEST_CACHESTORE_MEMCACHED_TESTSERVERS)); $connection->setOptions(array(Memcached::OPT_COMPRESSION => true, Memcached::OPT_SERIALIZER => Memcached::SERIALIZER_PHP, Memcached::OPT_PREFIX_KEY => 'phpunit_', Memcached::OPT_BUFFER_WRITES => false)); // We must flush first to make sure nothing is there. $connection->flush(); // Test the cachestore. $this->assertFalse($cachestore->get('test')); $this->assertTrue($cachestore->set('test', 'cachestore')); $this->assertSame('cachestore', $cachestore->get('test')); // Test the connection. $this->assertFalse($connection->get('test')); $this->assertEquals(Memcached::RES_NOTFOUND, $connection->getResultCode()); $this->assertTrue($connection->set('test', 'connection')); $this->assertSame('connection', $connection->get('test')); // Test both again and make sure the values are correct. $this->assertSame('cachestore', $cachestore->get('test')); $this->assertSame('connection', $connection->get('test')); // Purge the cachestore and check the connection was also purged. $this->assertTrue($cachestore->purge()); $this->assertFalse($cachestore->get('test')); $this->assertFalse($connection->get('test')); }
/** * Creates an ad-hoc cache from the given param. * * If a cache has already been created using the same params then that cache instance will be returned. * * @param int $mode * @param string $component * @param string $area * @param array $identifiers * @param array $options An array of options, available options are: * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_ * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars * - persistent : If set to true the cache will persist construction requests. * @return cache_application|cache_session|cache_request */ public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) { $key = "{$mode}_{$component}_{$area}"; if (array_key_exists($key, $this->cachesfromparams)) { return $this->cachesfromparams[$key]; } // Get the class. Note this is a late static binding so we need to use get_called_class. $definition = cache_definition::load_adhoc($mode, $component, $area, $options); $definition->set_identifiers($identifiers); $cache = $this->create_cache($definition, $identifiers); if ($definition->should_be_persistent()) { $this->cachesfromparams[$key] = $cache; } return $cache; }
*/ require_once '../config.php'; require_once $CFG->dirroot . '/lib/adminlib.php'; require_once $CFG->dirroot . '/cache/locallib.php'; $count = optional_param('count', 100, PARAM_INT); $count = min($count, 100000); $count = max($count, 0); admin_externalpage_setup('cachetestperformance'); $applicationtable = new html_table(); $applicationtable->head = array(get_string('plugin', 'cache'), get_string('result', 'cache'), get_string('set', 'cache'), get_string('gethit', 'cache'), get_string('getmiss', 'cache'), get_string('delete', 'cache')); $applicationtable->data = array(); $sessiontable = clone $applicationtable; $requesttable = clone $applicationtable; $application = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cache', 'applicationtest'); $session = cache_definition::load_adhoc(cache_store::MODE_SESSION, 'cache', 'sessiontest'); $request = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cache', 'requesttest'); $strinvalidplugin = new lang_string('invalidplugin', 'cache'); $strunsupportedmode = new lang_string('unsupportedmode', 'cache'); $struntestable = new lang_string('untestable', 'cache'); $strtested = new lang_string('tested', 'cache'); $strnotready = new lang_string('storenotready', 'cache'); foreach (core_component::get_plugin_list_with_file('cachestore', 'lib.php', true) as $plugin => $path) { $class = 'cachestore_' . $plugin; $plugin = get_string('pluginname', 'cachestore_' . $plugin); if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) { $applicationtable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-'); $sessiontable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-'); $requesttable->data[] = array($plugin, $strinvalidplugin, '-', '-', '-', '-'); continue; } if (!$class::is_supported_mode(cache_store::MODE_APPLICATION)) {
/** * Purges a store given its name. * * @param string $storename * @return bool */ public static function purge_store($storename) { $config = cache_config::instance(); foreach ($config->get_all_stores() as $store) { if ($store['name'] !== $storename) { continue; } $class = $store['class']; $instance = new $class($store['name'], $store['configuration']); if (!$instance->is_ready()) { continue; } $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'core', 'cache_purge'); $instance->initialise($definition); $instance->purge(); return true; } return false; }
/** * Creates an ad-hoc cache from the given param. * * @param int $mode * @param string $component * @param string $area * @param array $identifiers * @param array $options An array of options, available options are: * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_ * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars * - staticacceleration : If set to true the cache will hold onto all data passing through it. * - staticaccelerationsize : Sets the max size of the static acceleration array. * @return cache_application|cache_session|cache_request */ public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) { $definition = cache_definition::load_adhoc($mode, $component, $area); $cache = $this->create_cache($definition, $identifiers); return $cache; }