Merge "Remove perf tracking code that was moved to WikimediaEvents in Ib300af5c"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 881ace2..0b2df61 100644 (file)
@@ -2,10 +2,22 @@
 
 use Wikimedia\TestingAccessWrapper;
 
+/**
+ * @covers WANObjectCache::wrap
+ * @covers WANObjectCache::unwrap
+ * @covers WANObjectCache::worthRefreshExpiring
+ * @covers WANObjectCache::worthRefreshPopular
+ * @covers WANObjectCache::isValid
+ * @covers WANObjectCache::getWarmupKeyMisses
+ * @covers WANObjectCache::prefixCacheKeys
+ * @covers WANObjectCache::getProcessCache
+ * @covers WANObjectCache::getNonProcessCachedKeys
+ * @covers WANObjectCache::getRawKeysForWarmup
+ */
 class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
        /** @var WANObjectCache */
        private $cache;
-       /**@var BagOStuff */
+       /** @var BagOStuff */
        private $internalCache;
 
        protected function setUp() {
@@ -259,10 +271,20 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
                ];
        }
 
+       /**
+        * @covers WANObjectCache::getWithSetCallback()
+        * @covers WANObjectCache::doGetWithSetCallback()
+        */
+       public function testGetWithSetCallback_invalidCallback() {
+               $this->setExpectedException( InvalidArgumentException::class );
+               $this->cache->getWithSetCallback( 'key', 30, 'invalid callback' );
+       }
+
        /**
         * @dataProvider getMultiWithSetCallback_provider
-        * @covers WANObjectCache::getMultiWithSetCallback()
-        * @covers WANObjectCache::makeMultiKeys()
+        * @covers WANObjectCache::getMultiWithSetCallback
+        * @covers WANObjectCache::makeMultiKeys
+        * @covers WANObjectCache::getMulti
         * @param array $extOpts
         * @param bool $versioned
         */
@@ -618,6 +640,7 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
        /**
         * @covers WANObjectCache::getWithSetCallback()
         * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::set()
         */
        public function testLockTSESlow() {
                $cache = $this->cache;
@@ -871,7 +894,9 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
        }
 
        /**
-        * @covers WANObjectCache::delete()
+        * @covers WANObjectCache::delete
+        * @covers WANObjectCache::relayDelete
+        * @covers WANObjectCache::relayPurge
         */
        public function testDelete() {
                $key = wfRandomString();
@@ -911,6 +936,8 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
 
        /**
         * @dataProvider getWithSetCallback_versions_provider
+        * @covers WANObjectCache::getWithSetCallback()
+        * @covers WANObjectCache::doGetWithSetCallback()
         * @param array $extOpts
         * @param bool $versioned
         */
@@ -975,9 +1002,11 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
        }
 
        /**
-        * @covers WANObjectCache::touchCheckKey()
-        * @covers WANObjectCache::resetCheckKey()
-        * @covers WANObjectCache::getCheckKeyTime()
+        * @covers WANObjectCache::touchCheckKey
+        * @covers WANObjectCache::resetCheckKey
+        * @covers WANObjectCache::getCheckKeyTime
+        * @covers WANObjectCache::makePurgeValue
+        * @covers WANObjectCache::parsePurgeValue
         */
        public function testTouchKeys() {
                $key = wfRandomString();
@@ -1100,6 +1129,34 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
                $this->assertTrue( $tBad2 );
        }
 
+       /**
+        * @covers WANObjectCache::reap()
+        */
+       public function testReap_fail() {
+               $backend = $this->getMockBuilder( EmptyBagOStuff::class )
+                       ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
+               $backend->expects( $this->once() )->method( 'get' )
+                       ->willReturn( [
+                               WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
+                               WANObjectCache::FLD_VALUE => 'value',
+                               WANObjectCache::FLD_TTL => 3600,
+                               WANObjectCache::FLD_TIME => 300,
+                       ] );
+               $backend->expects( $this->once() )->method( 'changeTTL' )
+                       ->willReturn( false );
+
+               $wanCache = new WANObjectCache( [
+                       'cache' => $backend,
+                       'pool' => 'testcache-hash',
+                       'relayer' => new EventRelayerNull( [] )
+               ] );
+
+               $isStale = null;
+               $ret = $wanCache->reap( 'key', 360, $isStale );
+               $this->assertTrue( $isStale, 'value was stale' );
+               $this->assertFalse( $ret, 'changeTTL failed' );
+       }
+
        /**
         * @covers WANObjectCache::set()
         */
@@ -1184,14 +1241,48 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
 
        public static function provideAdaptiveTTL() {
                return [
-                       [ 3600, 900, 30, .2, 720 ],
-                       [ 3600, 500, 30, .2, 500 ],
-                       [ 3600, 86400, 800, .2, 800 ],
-                       [ false, 86400, 800, .2, 800 ],
-                       [ null, 86400, 800, .2, 800 ]
+                       [ 3600, 900, 30, 0.2, 720 ],
+                       [ 3600, 500, 30, 0.2, 500 ],
+                       [ 3600, 86400, 800, 0.2, 800 ],
+                       [ false, 86400, 800, 0.2, 800 ],
+                       [ null, 86400, 800, 0.2, 800 ]
                ];
        }
 
+       /**
+        * @covers WANObjectCache::__construct
+        * @covers WANObjectCache::newEmpty
+        */
+       public function testNewEmpty() {
+               $this->assertInstanceOf(
+                       WANObjectCache::class,
+                       WANObjectCache::newEmpty()
+               );
+       }
+
+       /**
+        * @covers WANObjectCache::setLogger
+        */
+       public function testSetLogger() {
+               $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
+       }
+
+       /**
+        * @covers WANObjectCache::getQoS
+        */
+       public function testGetQoS() {
+               $backend = $this->getMockBuilder( HashBagOStuff::class )
+                       ->setMethods( [ 'getQoS' ] )->getMock();
+               $backend->expects( $this->once() )->method( 'getQoS' )
+                       ->willReturn( BagOStuff::QOS_UNKNOWN );
+               $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
+
+               $this->assertSame(
+                       $wanCache::QOS_UNKNOWN,
+                       $wanCache->getQoS( $wanCache::ATTR_EMULATION )
+               );
+       }
+
        /**
         * @covers WANObjectCache::makeKey
         */