/** * Test the maxsize option. */ public function test_maxsize() { $defid = 'phpunit/testmaxsize'; $config = cache_config_testing::instance(); $config->phpunit_add_definition($defid, array('mode' => cache_store::MODE_REQUEST, 'component' => 'phpunit', 'area' => 'testmaxsize', 'maxsize' => 3)); $definition = cache_definition::load($defid, $config->get_definition_by_id($defid)); $instance = cachestore_static::initialise_test_instance($definition); $this->assertTrue($instance->set('key1', 'value1')); $this->assertTrue($instance->set('key2', 'value2')); $this->assertTrue($instance->set('key3', 'value3')); $this->assertTrue($instance->has('key1')); $this->assertTrue($instance->has('key2')); $this->assertTrue($instance->has('key3')); $this->assertTrue($instance->set('key4', 'value4')); $this->assertTrue($instance->set('key5', 'value5')); $this->assertFalse($instance->has('key1')); $this->assertFalse($instance->has('key2')); $this->assertTrue($instance->has('key3')); $this->assertTrue($instance->has('key4')); $this->assertTrue($instance->has('key5')); $this->assertFalse($instance->get('key1')); $this->assertFalse($instance->get('key2')); $this->assertEquals('value3', $instance->get('key3')); $this->assertEquals('value4', $instance->get('key4')); $this->assertEquals('value5', $instance->get('key5')); // Test adding one more. $this->assertTrue($instance->set('key6', 'value6')); $this->assertFalse($instance->get('key3')); // Test reducing and then adding to make sure we don't lost one. $this->assertTrue($instance->delete('key6')); $this->assertTrue($instance->set('key7', 'value7')); $this->assertEquals('value4', $instance->get('key4')); // Set the same key three times to make sure it doesn't count overrides. for ($i = 0; $i < 3; $i++) { $this->assertTrue($instance->set('key8', 'value8')); } $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size'); // Test adding many. $this->assertEquals(3, $instance->set_many(array(array('key' => 'keyA', 'value' => 'valueA'), array('key' => 'keyB', 'value' => 'valueB'), array('key' => 'keyC', 'value' => 'valueC')))); $this->assertEquals(array('key4' => false, 'key5' => false, 'key6' => false, 'key7' => false, 'keyA' => 'valueA', 'keyB' => 'valueB', 'keyC' => 'valueC'), $instance->get_many(array('key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'))); }
/** * Test the hash_key functionality. */ public function test_hash_key() { $this->resetAfterTest(); set_debugging(DEBUG_ALL); // First with simplekeys $instance = cache_config_testing::instance(true); $instance->phpunit_add_definition('phpunit/hashtest', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'hashtest', 'simplekeys' => true)); $factory = cache_factory::instance(); $definition = $factory->create_definition('phpunit', 'hashtest'); $result = cache_helper::hash_key('test', $definition); $this->assertEquals('test-' . $definition->generate_single_key_prefix(), $result); try { cache_helper::hash_key('test/test', $definition); $this->fail('Invalid key was allowed, you should see this.'); } catch (coding_exception $e) { $this->assertEquals('test/test', $e->debuginfo); } // Second without simple keys $instance->phpunit_add_definition('phpunit/hashtest2', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'hashtest2', 'simplekeys' => false)); $definition = $factory->create_definition('phpunit', 'hashtest2'); $result = cache_helper::hash_key('test', $definition); $this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test'), $result); $result = cache_helper::hash_key('test/test', $definition); $this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test/test'), $result); }
public function test_static_acceleration() { $instance = cache_config_testing::instance(); $instance->phpunit_add_definition('phpunit/accelerated', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated', 'staticacceleration' => true, 'staticaccelerationsize' => 3)); $instance->phpunit_add_definition('phpunit/accelerated2', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated2', 'staticacceleration' => true, 'staticaccelerationsize' => 3)); $instance->phpunit_add_definition('phpunit/accelerated3', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated3', 'staticacceleration' => true, 'staticaccelerationsize' => 3)); $instance->phpunit_add_definition('phpunit/accelerated4', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'accelerated4', 'staticacceleration' => true, 'staticaccelerationsize' => 4)); $instance->phpunit_add_definition('phpunit/simpledataarea1', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'simpledataarea1', 'staticacceleration' => true, 'simpledata' => false)); $instance->phpunit_add_definition('phpunit/simpledataarea2', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'simpledataarea2', 'staticacceleration' => true, 'simpledata' => true)); $cache = cache::make('phpunit', 'accelerated'); $this->assertInstanceOf('cache_phpunit_application', $cache); // Set and get three elements. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertEquals('A', $cache->get('a')); $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c'))); // Make sure all items are in static acceleration array. $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); // Add new value and make sure it is in cache and it is in array. $this->assertTrue($cache->set('d', 'D')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('D', $cache->get('d')); // Now the least recent accessed item (a) is no longer in acceleration array. $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); // Adding and deleting element. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->delete('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->has('a')); // Make sure "purge" deletes from the array as well. $cache->purge(); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('c')); $this->assertFalse($cache->phpunit_static_acceleration_get('d')); $this->assertFalse($cache->phpunit_static_acceleration_get('e')); // Check that the array holds the last accessed items by get/set. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertTrue($cache->set('d', 'D')); $this->assertTrue($cache->set('e', 'E')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); /** @var cache_phpunit_application $cache */ $cache = cache::make('phpunit', 'accelerated2'); $this->assertInstanceOf('cache_phpunit_application', $cache); // Check that the array holds the last accessed items by get/set. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertTrue($cache->set('d', 'D')); $this->assertTrue($cache->set('e', 'E')); // Current keys in the array: c, d, e. $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertEquals('A', $cache->get('a')); // Current keys in the array: d, e, a. $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('c')); // Current keys in the array: d, e, a. $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c'))); // Current keys in the array: e, a, c. $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('d')); $cache = cache::make('phpunit', 'accelerated3'); $this->assertInstanceOf('cache_phpunit_application', $cache); // Check that the array holds the last accessed items by get/set. $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('b', 'B')); $this->assertTrue($cache->set('c', 'C')); $this->assertTrue($cache->set('d', 'D')); $this->assertTrue($cache->set('e', 'E')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertFalse($cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertTrue($cache->set('b', 'B2')); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b')); $this->assertFalse($cache->phpunit_static_acceleration_get('c')); $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3'))); $this->assertFalse($cache->phpunit_static_acceleration_get('a')); $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b')); $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c')); $this->assertFalse($cache->phpunit_static_acceleration_get('d')); $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); $cache = cache::make('phpunit', 'accelerated4'); $this->assertInstanceOf('cache_phpunit_application', $cache); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertTrue($cache->set('a', 'A')); $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); $this->assertEquals('A', $cache->get('a')); // Setting simpledata to false objects are cloned when retrieving data. $cache = cache::make('phpunit', 'simpledataarea1'); $notreallysimple = new stdClass(); $notreallysimple->name = 'a'; $cache->set('a', $notreallysimple); $returnedinstance1 = $cache->get('a'); $returnedinstance2 = $cache->get('a'); $returnedinstance1->name = 'b'; $this->assertEquals('a', $returnedinstance2->name); // Setting simpledata to true we assume that data does not contain references. $cache = cache::make('phpunit', 'simpledataarea2'); $notreallysimple = new stdClass(); $notreallysimple->name = 'a'; $cache->set('a', $notreallysimple); $returnedinstance1 = $cache->get('a'); $returnedinstance2 = $cache->get('a'); $returnedinstance1->name = 'b'; $this->assertEquals('b', $returnedinstance2->name); }
public function test_performance_debug() { global $CFG; $this->resetAfterTest(true); $CFG->perfdebug = 15; $instance = cache_config_testing::instance(); $applicationid = 'phpunit/applicationperf'; $instance->phpunit_add_definition($applicationid, array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'applicationperf')); $sessionid = 'phpunit/sessionperf'; $instance->phpunit_add_definition($sessionid, array('mode' => cache_store::MODE_SESSION, 'component' => 'phpunit', 'area' => 'sessionperf')); $requestid = 'phpunit/requestperf'; $instance->phpunit_add_definition($requestid, array('mode' => cache_store::MODE_REQUEST, 'component' => 'phpunit', 'area' => 'requestperf')); $application = cache::make('phpunit', 'applicationperf'); $session = cache::make('phpunit', 'sessionperf'); $request = cache::make('phpunit', 'requestperf'); // Check that no stats are recorded for these definitions yet. $stats = cache_helper::get_stats(); $this->assertArrayNotHasKey($applicationid, $stats); $this->assertArrayHasKey($sessionid, $stats); // Session cache sets a key on construct. $this->assertArrayNotHasKey($requestid, $stats); // Check that stores register misses. $this->assertFalse($application->get('missMe')); $this->assertFalse($application->get('missMe')); $this->assertFalse($session->get('missMe')); $this->assertFalse($session->get('missMe')); $this->assertFalse($session->get('missMe')); $this->assertFalse($request->get('missMe')); $this->assertFalse($request->get('missMe')); $this->assertFalse($request->get('missMe')); $this->assertFalse($request->get('missMe')); $endstats = cache_helper::get_stats(); $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['misses']); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits']); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets']); $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['misses']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits']); $this->assertEquals(1, $endstats[$sessionid]['stores']['cachestore_session']['sets']); $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['misses']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets']); $startstats = cache_helper::get_stats(); // Check that stores register sets. $this->assertTrue($application->set('setMe1', 1)); $this->assertTrue($application->set('setMe2', 2)); $this->assertTrue($session->set('setMe1', 1)); $this->assertTrue($session->set('setMe2', 2)); $this->assertTrue($session->set('setMe3', 3)); $this->assertTrue($request->set('setMe1', 1)); $this->assertTrue($request->set('setMe2', 2)); $this->assertTrue($request->set('setMe3', 3)); $this->assertTrue($request->set('setMe4', 4)); $endstats = cache_helper::get_stats(); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] - $startstats[$applicationid]['stores']['cachestore_file']['misses']); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits'] - $startstats[$applicationid]['stores']['cachestore_file']['hits']); $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['sets'] - $startstats[$applicationid]['stores']['cachestore_file']['sets']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] - $startstats[$sessionid]['stores']['cachestore_session']['misses']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits'] - $startstats[$sessionid]['stores']['cachestore_session']['hits']); $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['sets'] - $startstats[$sessionid]['stores']['cachestore_session']['sets']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] - $startstats[$requestid]['stores']['cachestore_static']['misses']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits'] - $startstats[$requestid]['stores']['cachestore_static']['hits']); $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['sets'] - $startstats[$requestid]['stores']['cachestore_static']['sets']); $startstats = cache_helper::get_stats(); // Check that stores register hits. $this->assertEquals($application->get('setMe1'), 1); $this->assertEquals($application->get('setMe2'), 2); $this->assertEquals($session->get('setMe1'), 1); $this->assertEquals($session->get('setMe2'), 2); $this->assertEquals($session->get('setMe3'), 3); $this->assertEquals($request->get('setMe1'), 1); $this->assertEquals($request->get('setMe2'), 2); $this->assertEquals($request->get('setMe3'), 3); $this->assertEquals($request->get('setMe4'), 4); $endstats = cache_helper::get_stats(); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] - $startstats[$applicationid]['stores']['cachestore_file']['misses']); $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] - $startstats[$applicationid]['stores']['cachestore_file']['hits']); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] - $startstats[$applicationid]['stores']['cachestore_file']['sets']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] - $startstats[$sessionid]['stores']['cachestore_session']['misses']); $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] - $startstats[$sessionid]['stores']['cachestore_session']['hits']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] - $startstats[$sessionid]['stores']['cachestore_session']['sets']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] - $startstats[$requestid]['stores']['cachestore_static']['misses']); $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] - $startstats[$requestid]['stores']['cachestore_static']['hits']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] - $startstats[$requestid]['stores']['cachestore_static']['sets']); $startstats = cache_helper::get_stats(); // Check that stores register through get_many. $application->get_many(array('setMe1', 'setMe2')); $session->get_many(array('setMe1', 'setMe2', 'setMe3')); $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4')); $endstats = cache_helper::get_stats(); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] - $startstats[$applicationid]['stores']['cachestore_file']['misses']); $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] - $startstats[$applicationid]['stores']['cachestore_file']['hits']); $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] - $startstats[$applicationid]['stores']['cachestore_file']['sets']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] - $startstats[$sessionid]['stores']['cachestore_session']['misses']); $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] - $startstats[$sessionid]['stores']['cachestore_session']['hits']); $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] - $startstats[$sessionid]['stores']['cachestore_session']['sets']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] - $startstats[$requestid]['stores']['cachestore_static']['misses']); $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] - $startstats[$requestid]['stores']['cachestore_static']['hits']); $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] - $startstats[$requestid]['stores']['cachestore_static']['sets']); }
public function test_ttl() { $config = cache_config_testing::instance(); $config->phpunit_add_definition('phpunit/three', array('mode' => cache_store::MODE_SESSION, 'component' => 'phpunit', 'area' => 'three', 'maxsize' => 3, 'ttl' => 3)); $cachethree = cache::make('phpunit', 'three'); // Make sure that when cache with ttl is full the elements that were added first are deleted first regardless of access time. $cachethree->set('key1', 'value1'); $cachethree->set('key2', 'value2'); $cachethree->set('key3', 'value3'); $cachethree->set('key4', 'value4'); $this->assertFalse($cachethree->get('key1')); $this->assertEquals('value4', $cachethree->get('key4')); $cachethree->set('key5', 'value5'); $this->assertFalse($cachethree->get('key2')); $this->assertEquals('value4', $cachethree->get('key4')); $cachethree->set_many(array('key6' => 'value6', 'key7' => 'value7')); $this->assertEquals(array('key3' => false, 'key4' => false, 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7'), $cachethree->get_many(array('key3', 'key4', 'key5', 'key6', 'key7'))); }
/** * Test setting some definition mappings. */ public function test_set_definition_mappings() { $config = cache_config_testing::instance(true); $config->phpunit_add_definition('phpunit/testdefinition', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'testdefinition')); $config = cache_config_writer::instance(); $this->assertTrue($config->add_store_instance('setdefinitiontest', 'file')); $this->assertInternalType('array', $config->get_definition_by_id('phpunit/testdefinition')); $config->set_definition_mappings('phpunit/testdefinition', array('setdefinitiontest', 'default_application')); try { $config->set_definition_mappings('phpunit/testdefinition', array('something that does not exist')); $this->fail('You should not be able to set a mapping for a store that does not exist.'); } catch (Exception $e) { $this->assertInstanceOf('coding_exception', $e); } try { $config->set_definition_mappings('something/crazy', array('setdefinitiontest')); $this->fail('You should not be able to set a mapping for a definition that does not exist.'); } catch (Exception $e) { $this->assertInstanceOf('coding_exception', $e); } }