Merge "Improve Doxygen template used by mwdocgen.php"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / HashBagOStuffTest.php
index 39b84e1..c4db0cf 100644 (file)
@@ -5,6 +5,9 @@
  */
 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
 
+       /**
+        * @covers HashBagOStuff::delete
+        */
        public function testDelete() {
                $cache = new HashBagOStuff();
                for ( $i = 0; $i < 10; $i++ ) {
@@ -15,6 +18,9 @@ class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
                }
        }
 
+       /**
+        * @covers HashBagOStuff::clear
+        */
        public function testClear() {
                $cache = new HashBagOStuff();
                for ( $i = 0; $i < 10; $i++ ) {
@@ -27,19 +33,10 @@ class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
                }
        }
 
-       public function testEvictionOrder() {
-               $cache = new HashBagOStuff( array( 'maxKeys' => 10 ) );
-               for ( $i = 0; $i < 10; $i++ ) {
-                       $cache->set( "key$i", 1 );
-                       $this->assertEquals( 1, $cache->get( "key$i" ) );
-               }
-               for ( $i = 10; $i < 20; $i++ ) {
-                       $cache->set( "key$i", 1 );
-                       $this->assertEquals( 1, $cache->get( "key$i" ) );
-                       $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
-               }
-       }
-
+       /**
+        * @covers HashBagOStuff::doGet
+        * @covers HashBagOStuff::expire
+        */
        public function testExpire() {
                $cache = new HashBagOStuff();
                $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
@@ -56,10 +53,36 @@ class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
                $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
        }
 
-       public function testKeyOrder() {
-               $cache = new HashBagOStuff( array( 'maxKeys' => 3 ) );
+       /**
+        * Ensure maxKeys eviction prefers keeping new keys.
+        *
+        * @covers HashBagOStuff::__construct
+        * @covers HashBagOStuff::set
+        */
+       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" ) );
+               }
+               for ( $i = 10; $i < 20; $i++ ) {
+                       $cache->set( "key$i", 1 );
+                       $this->assertEquals( 1, $cache->get( "key$i" ) );
+                       $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
+               }
+       }
+
+       /**
+        * Ensure maxKeys eviction prefers recently set keys
+        * even if the keys pre-exist.
+        *
+        * @covers HashBagOStuff::__construct
+        * @covers HashBagOStuff::set
+        */
+       public function testEvictionSet() {
+               $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
 
-               foreach ( array( 'foo', 'bar', 'baz' ) as $key ) {
+               foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
                        $cache->set( $key, 1 );
                }
 
@@ -70,7 +93,34 @@ class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
                $cache->set( 'quux', 1 );
 
                // Foo's life should have been extended over Bar
-               foreach ( array( 'foo', 'baz', 'quux' ) as $key ) {
+               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).
+        *
+        * @covers HashBagOStuff::__construct
+        * @covers HashBagOStuff::doGet
+        * @covers HashBagOStuff::hasKey
+        */
+       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' );