Merge "Tell users that js/css subpages are public"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 266e025..35005f5 100644 (file)
@@ -33,17 +33,26 @@ class WANObjectCacheTest extends MediaWikiTestCase {
         * @param integer $ttl
         */
        public function testSetAndGet( $value, $ttl ) {
+               $curTTL = null;
+               $asOf = null;
                $key = wfRandomString();
+
+               $this->cache->get( $key, $curTTL, [], $asOf );
+               $this->assertNull( $curTTL, "Current TTL is null" );
+               $this->assertNull( $asOf, "Current as-of-time is infinite" );
+
+               $t = microtime( true );
                $this->cache->set( $key, $value, $ttl );
 
-               $curTTL = null;
-               $this->assertEquals( $value, $this->cache->get( $key, $curTTL ) );
+               $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
                if ( is_infinite( $ttl ) || $ttl == 0 ) {
                        $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
                } else {
                        $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
                        $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
                }
+               $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
+               $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
        }
 
        public static function provideSetAndGet() {
@@ -97,10 +106,13 @@ class WANObjectCacheTest extends MediaWikiTestCase {
        }
 
        /**
+        * @dataProvider getWithSetCallback_provider
         * @covers WANObjectCache::getWithSetCallback()
         * @covers WANObjectCache::doGetWithSetCallback()
+        * @param array $extOpts
+        * @param bool $versioned
         */
-       public function testGetWithSetCallback() {
+       public function testGetWithSetCallback( array $extOpts, $versioned ) {
                $cache = $this->cache;
 
                $key = wfRandomString();
@@ -116,7 +128,7 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                };
 
                $wasSet = 0;
-               $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
+               $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated" );
 
@@ -129,15 +141,16 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $v = $cache->getWithSetCallback( $key, 30, $func, [
                        'lowTTL' => 0,
                        'lockTSE' => 5,
-               ] );
+               ] + $extOpts );
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 0, $wasSet, "Value not regenerated" );
 
                $priorTime = microtime( true );
                usleep( 1 );
                $wasSet = 0;
-               $v = $cache->getWithSetCallback( $key, 30, $func,
-                       [ 'checkKeys' => [ $cKey1, $cKey2 ] ] );
+               $v = $cache->getWithSetCallback(
+                       $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
+               );
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
                $t1 = $cache->getCheckKeyTime( $cKey1 );
@@ -147,8 +160,9 @@ class WANObjectCacheTest extends MediaWikiTestCase {
 
                $priorTime = microtime( true );
                $wasSet = 0;
-               $v = $cache->getWithSetCallback( $key, 30, $func,
-                       [ 'checkKeys' => [ $cKey1, $cKey2 ] ] );
+               $v = $cache->getWithSetCallback(
+                       $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
+               );
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
                $t1 = $cache->getCheckKeyTime( $cKey1 );
@@ -158,19 +172,30 @@ class WANObjectCacheTest extends MediaWikiTestCase {
 
                $curTTL = null;
                $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
-               $this->assertEquals( $value, $v, "Value returned" );
+               if ( $versioned ) {
+                       $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
+               } else {
+                       $this->assertEquals( $value, $v, "Value returned" );
+               }
                $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
 
                $wasSet = 0;
                $key = wfRandomString();
-               $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] );
+               $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
                $this->assertEquals( $value, $v, "Value returned" );
                $cache->delete( $key );
-               $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] );
+               $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
                $this->assertEquals( $value, $v, "Value still returned after deleted" );
                $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
        }
 
+       public static function getWithSetCallback_provider() {
+               return [
+                       [ [], false ],
+                       [ [ 'version' => 1 ], true ]
+               ];
+       }
+
        /**
         * @covers WANObjectCache::getWithSetCallback()
         * @covers WANObjectCache::doGetWithSetCallback()
@@ -181,8 +206,10 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $value = wfRandomString();
 
                $calls = 0;
-               $func = function() use ( &$calls, $value ) {
+               $func = function() use ( &$calls, $value, $cache, $key ) {
                        ++$calls;
+                       // Immediately kill any mutex rather than waiting a second
+                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
                        return $value;
                };
 
@@ -191,10 +218,24 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->assertEquals( 1, $calls, 'Value was populated' );
 
                // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
-               $this->internalCache->lock( $key, 0 );
-               $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
-               $this->assertEquals( $value, $ret );
+               $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
+
+               $checkKeys = [ wfRandomString() ]; // new check keys => force misses
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Old value used' );
                $this->assertEquals( 1, $calls, 'Callback was not used' );
+
+               $cache->delete( $key );
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
+               $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
+
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
+               $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
        }
 
        /**
@@ -207,9 +248,11 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $value = wfRandomString();
 
                $calls = 0;
-               $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
+               $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
                        ++$calls;
                        $setOpts['since'] = microtime( true ) - 10;
+                       // Immediately kill any mutex rather than waiting a second
+                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
                        return $value;
                };
 
@@ -222,12 +265,67 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->assertEquals( 1, $calls, 'Value was generated' );
 
                // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
-               $this->internalCache->lock( $key, 0 );
+               $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
                $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
                $this->assertEquals( $value, $ret );
                $this->assertEquals( 1, $calls, 'Callback was not used' );
        }
 
+       /**
+        * @covers WANObjectCache::getWithSetCallback()
+        * @covers WANObjectCache::doGetWithSetCallback()
+        */
+       public function testBusyValue() {
+               $cache = $this->cache;
+               $key = wfRandomString();
+               $value = wfRandomString();
+               $busyValue = wfRandomString();
+
+               $calls = 0;
+               $func = function() use ( &$calls, $value, $cache, $key ) {
+                       ++$calls;
+                       // Immediately kill any mutex rather than waiting a second
+                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+                       return $value;
+               };
+
+               $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 1, $calls, 'Value was populated' );
+
+               // Acquire a lock to verify that getWithSetCallback uses busyValue properly
+               $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
+
+               $checkKeys = [ wfRandomString() ]; // new check keys => force misses
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Callback used' );
+               $this->assertEquals( 2, $calls, 'Callback used' );
+
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Old value used' );
+               $this->assertEquals( 2, $calls, 'Callback was not used' );
+
+               $cache->delete( $key ); // no value at all anymore and still locked
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
+               $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
+
+               $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
+               $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
+
+               $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
+               $ret = $cache->getWithSetCallback( $key, 30, $func,
+                       [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+               $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
+               $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
+       }
+
        /**
         * @covers WANObjectCache::getMulti()
         */
@@ -434,6 +532,71 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
        }
 
+       /**
+        * @dataProvider getWithSetCallback_versions_provider
+        * @param array $extOpts
+        * @param $versioned
+        */
+       public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
+               $cache = $this->cache;
+
+               $key = wfRandomString();
+               $value = wfRandomString();
+
+               $wasSet = 0;
+               $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
+                       ++$wasSet;
+                       return $value;
+               };
+
+               // Set the main key (version N if versioned)
+               $wasSet = 0;
+               $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+               $this->assertEquals( $value, $v, "Value returned" );
+               $this->assertEquals( 1, $wasSet, "Value regenerated" );
+               $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+               $this->assertEquals( 1, $wasSet, "Value not regenerated" );
+               // Set the key for version N+1 (if versioned)
+               if ( $versioned ) {
+                       $verOpts = [ 'version' => $extOpts['version'] + 1 ];
+
+                       $wasSet = 0;
+                       $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
+                       $this->assertEquals( $value, $v, "Value returned" );
+                       $this->assertEquals( 1, $wasSet, "Value regenerated" );
+
+                       $wasSet = 0;
+                       $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
+                       $this->assertEquals( $value, $v, "Value returned" );
+                       $this->assertEquals( 0, $wasSet, "Value not regenerated" );
+               }
+
+               $wasSet = 0;
+               $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+               $this->assertEquals( 0, $wasSet, "Value not regenerated" );
+
+               $wasSet = 0;
+               $cache->delete( $key );
+               $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+               $this->assertEquals( $value, $v, "Value returned" );
+               $this->assertEquals( 1, $wasSet, "Value regenerated" );
+
+               if ( $versioned ) {
+                       $wasSet = 0;
+                       $verOpts = [ 'version' => $extOpts['version'] + 1 ];
+                       $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
+                       $this->assertEquals( $value, $v, "Value returned" );
+                       $this->assertEquals( 1, $wasSet, "Value regenerated" );
+               }
+       }
+
+       public static function getWithSetCallback_versions_provider() {
+               return [
+                       [ [], false ],
+                       [ [ 'version' => 1 ], true ]
+               ];
+       }
+
        /**
         * @covers WANObjectCache::touchCheckKey()
         * @covers WANObjectCache::resetCheckKey()
@@ -537,4 +700,26 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $this->cache->set( $key, $value, 30, $opts );
                $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
        }
+
+       public function testMcRouterSupport() {
+               $localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
+               $localBag->expects( $this->never() )->method( 'set' );
+               $localBag->expects( $this->never() )->method( 'delete' );
+               $wanCache = new WANObjectCache( [
+                       'cache' => $localBag,
+                       'pool' => 'testcache-hash',
+                       'relayer' => new EventRelayerNull( [] )
+               ] );
+               $valFunc = function () {
+                       return 1;
+               };
+
+               // None of these should use broadcasting commands (e.g. SET, DELETE)
+               $wanCache->get( 'x' );
+               $wanCache->get( 'x', $ctl, [ 'check1' ] );
+               $wanCache->getMulti( [ 'x', 'y' ] );
+               $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
+               $wanCache->getWithSetCallback( 'p', 30, $valFunc );
+               $wanCache->getCheckKeyTime( 'zzz' );
+       }
 }