X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Flibs%2Fobjectcache%2FHashBagOStuffTest.php;h=fce09ae58db04510fe5f91d0b006ca8c8596939d;hb=e3bd13db0c285f312e31bb1b7271af4628cca80c;hp=5344e2d51c1f3192c535345cd10c6e129266650a;hpb=c7fc73286d39a2ca848ee00a588fbccd23a850d3;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/libs/objectcache/HashBagOStuffTest.php b/tests/phpunit/includes/libs/objectcache/HashBagOStuffTest.php index 5344e2d51c..fce09ae58d 100644 --- a/tests/phpunit/includes/libs/objectcache/HashBagOStuffTest.php +++ b/tests/phpunit/includes/libs/objectcache/HashBagOStuffTest.php @@ -5,8 +5,49 @@ */ class HashBagOStuffTest extends PHPUnit_Framework_TestCase { - public function testEvictionOrder() { - $cache = new HashBagOStuff( array( 'maxKeys' => 10 ) ); + public function testDelete() { + $cache = new HashBagOStuff(); + for ( $i = 0; $i < 10; $i++ ) { + $cache->set( "key$i", 1 ); + $this->assertEquals( 1, $cache->get( "key$i" ) ); + $cache->delete( "key$i" ); + $this->assertEquals( false, $cache->get( "key$i" ) ); + } + } + + public function testClear() { + $cache = new HashBagOStuff(); + for ( $i = 0; $i < 10; $i++ ) { + $cache->set( "key$i", 1 ); + $this->assertEquals( 1, $cache->get( "key$i" ) ); + } + $cache->clear(); + for ( $i = 0; $i < 10; $i++ ) { + $this->assertEquals( false, $cache->get( "key$i" ) ); + } + } + + public function testExpire() { + $cache = new HashBagOStuff(); + $cacheInternal = TestingAccessWrapper::newFromObject( $cache ); + $cache->set( 'foo', 1 ); + $cache->set( 'bar', 1, 10 ); + $cache->set( 'baz', 1, -10 ); + + $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' ); + // 2 seconds tolerance + $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 ); + $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 ); + + $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' ); + $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' ); + } + + /** + * Ensure maxKeys eviction prefers keeping new keys. + */ + public function testEvictionAdd() { + $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] ); for ( $i = 0; $i < 10; $i++ ) { $cache->set( "key$i", 1 ); $this->assertEquals( 1, $cache->get( "key$i" ) ); @@ -17,4 +58,51 @@ class HashBagOStuffTest extends PHPUnit_Framework_TestCase { $this->assertEquals( false, $cache->get( "key" . $i - 10 ) ); } } + + /** + * Ensure maxKeys eviction prefers recently set keys + * even if the keys pre-exist. + */ + public function testEvictionSet() { + $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] ); + + foreach ( [ 'foo', 'bar', 'baz' ] as $key ) { + $cache->set( $key, 1 ); + } + + // Set existing key + $cache->set( 'foo', 1 ); + + // Add a 4th key (beyond the allowed maximum) + $cache->set( 'quux', 1 ); + + // Foo's life should have been extended over Bar + foreach ( [ 'foo', 'baz', 'quux' ] as $key ) { + $this->assertEquals( 1, $cache->get( $key ), "Kept $key" ); + } + $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' ); + } + + /** + * Ensure maxKeys eviction prefers recently retrieved keys (LRU). + */ + public function testEvictionGet() { + $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] ); + + foreach ( [ 'foo', 'bar', 'baz' ] as $key ) { + $cache->set( $key, 1 ); + } + + // Get existing key + $cache->get( 'foo', 1 ); + + // Add a 4th key (beyond the allowed maximum) + $cache->set( 'quux', 1 ); + + // Foo's life should have been extended over Bar + foreach ( [ 'foo', 'baz', 'quux' ] as $key ) { + $this->assertEquals( 1, $cache->get( $key ), "Kept $key" ); + } + $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' ); + } }