Switch to librarized version of TestingAccessWrapper
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 4e455f7..72effd7 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 
-class WANObjectCacheTest extends MediaWikiTestCase {
+use Wikimedia\TestingAccessWrapper;
+
+class WANObjectCacheTest extends PHPUnit_Framework_TestCase  {
        /** @var WANObjectCache */
        private $cache;
        /**@var BagOStuff */
@@ -9,17 +11,11 @@ class WANObjectCacheTest extends MediaWikiTestCase {
        protected function setUp() {
                parent::setUp();
 
-               if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
-                       $name = $this->getCliArg( 'use-wanobjectcache' );
-
-                       $this->cache = ObjectCache::getWANInstance( $name );
-               } else {
-                       $this->cache = new WANObjectCache( [
-                               'cache' => new HashBagOStuff(),
-                               'pool' => 'testcache-hash',
-                               'relayer' => new EventRelayerNull( [] )
-                       ] );
-               }
+               $this->cache = new WANObjectCache( [
+                       'cache' => new HashBagOStuff(),
+                       'pool' => 'testcache-hash',
+                       'relayer' => new EventRelayerNull( [] )
+               ] );
 
                $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
                /** @noinspection PhpUndefinedFieldInspection */
@@ -147,6 +143,19 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                                $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
                }
                $this->assertEquals( 9, $hit, "Values evicted" );
+
+               $key = reset( $keys );
+               // Get into cache
+               $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
+               $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
+               $this->assertEquals( 10, $hit, "Value cached" );
+               $outerCallback = function () use ( &$callback, $key ) {
+                       $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
+
+                       return 43 + $v;
+               };
+               $this->cache->getWithSetCallback( $key, 100, $outerCallback );
+               $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
        }
 
        /**
@@ -206,7 +215,7 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
                $this->assertEquals( $value, $priorValue, "Has prior value" );
-               $this->assertType( 'float', $priorAsOf, "Has prior value" );
+               $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
                $t1 = $cache->getCheckKeyTime( $cKey1 );
                $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
                $t2 = $cache->getCheckKeyTime( $cKey2 );
@@ -316,7 +325,7 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->assertEquals( $value, $v[$keyB], "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
                $this->assertEquals( $value, $priorValue, "Has prior value" );
-               $this->assertType( 'float', $priorAsOf, "Has prior value" );
+               $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
                $t1 = $cache->getCheckKeyTime( $cKey1 );
                $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
                $t2 = $cache->getCheckKeyTime( $cKey2 );
@@ -865,6 +874,62 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
        }
 
+       /**
+        * @covers WANObjectCache::reap()
+        * @covers WANObjectCache::reapCheckKey()
+        */
+       public function testReap() {
+               $vKey1 = wfRandomString();
+               $vKey2 = wfRandomString();
+               $tKey1 = wfRandomString();
+               $tKey2 = wfRandomString();
+               $value = 'moo';
+
+               $knownPurge = time() - 60;
+               $goodTime = microtime( true ) - 5;
+               $badTime = microtime( true ) - 300;
+
+               $this->internalCache->set(
+                       WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
+                       [
+                               WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
+                               WANObjectCache::FLD_VALUE => $value,
+                               WANObjectCache::FLD_TTL => 3600,
+                               WANObjectCache::FLD_TIME => $goodTime
+                       ]
+               );
+               $this->internalCache->set(
+                       WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
+                       [
+                               WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
+                               WANObjectCache::FLD_VALUE => $value,
+                               WANObjectCache::FLD_TTL => 3600,
+                               WANObjectCache::FLD_TIME => $badTime
+                       ]
+               );
+               $this->internalCache->set(
+                       WANObjectCache::TIME_KEY_PREFIX . $tKey1,
+                       WANObjectCache::PURGE_VAL_PREFIX . $goodTime
+               );
+               $this->internalCache->set(
+                       WANObjectCache::TIME_KEY_PREFIX . $tKey2,
+                       WANObjectCache::PURGE_VAL_PREFIX . $badTime
+               );
+
+               $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
+               $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
+               $this->cache->reap( $vKey1, $knownPurge, $bad1 );
+               $this->cache->reap( $vKey2, $knownPurge, $bad2 );
+
+               $this->assertFalse( $bad1 );
+               $this->assertTrue( $bad2 );
+
+               $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
+               $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
+               $this->assertFalse( $tBad1 );
+               $this->assertTrue( $tBad2 );
+       }
+
        /**
         * @covers WANObjectCache::set()
         */
@@ -900,7 +965,8 @@ class WANObjectCacheTest extends MediaWikiTestCase {
        }
 
        public function testMcRouterSupport() {
-               $localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
+               $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
+                       ->setMethods( [ 'set', 'delete' ] )->getMock();
                $localBag->expects( $this->never() )->method( 'set' );
                $localBag->expects( $this->never() )->method( 'delete' );
                $wanCache = new WANObjectCache( [
@@ -919,6 +985,8 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
                $wanCache->getWithSetCallback( 'p', 30, $valFunc );
                $wanCache->getCheckKeyTime( 'zzz' );
+               $wanCache->reap( 'x', time() - 300 );
+               $wanCache->reap( 'zzz', time() - 300 );
        }
 
        /**