Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index d9b7e18..593dd45 100644 (file)
@@ -47,18 +47,26 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
         * @param int $ttl
         */
        public function testSetAndGet( $value, $ttl ) {
+               $cache = $this->cache;
+
                $curTTL = null;
                $asOf = null;
-               $key = $this->cache->makeKey( 'x', wfRandomString() );
+               $key = $cache->makeKey( 'x', wfRandomString() );
 
-               $this->cache->get( $key, $curTTL, [], $asOf );
+               $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 );
 
-               $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
+               $cache->set( $key, $value, $cache::TTL_UNCACHEABLE );
+               $cache->get( $key, $curTTL, [], $asOf );
+               $this->assertNull( $curTTL, "Current TTL is null (TTL_UNCACHEABLE)" );
+               $this->assertNull( $asOf, "Current as-of-time is infinite (TTL_UNCACHEABLE)" );
+
+               $cache->set( $key, $value, $ttl );
+
+               $this->assertEquals( $value, $cache->get( $key, $curTTL, [], $asOf ) );
                if ( is_infinite( $ttl ) || $ttl == 0 ) {
                        $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
                } else {
@@ -121,6 +129,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        }
 
        public function testProcessCache() {
+               $mockWallClock = 1549343530.2053;
+               $this->cache->setMockTime( $mockWallClock );
+
                $hit = 0;
                $callback = function () use ( &$hit ) {
                        ++$hit;
@@ -154,18 +165,28 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 6, $hit, "New values cached" );
 
                foreach ( $keys as $i => $key ) {
+                       // Should not evict from process cache
                        $this->cache->delete( $key );
+                       $mockWallClock += 0.001; // cached values will be newer than tombstone
+                       // Get into cache (specific process cache group)
                        $this->cache->getWithSetCallback(
                                $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
                }
-               $this->assertEquals( 9, $hit, "Values evicted" );
+               $this->assertEquals( 9, $hit, "Values evicted by delete()" );
 
-               $key = reset( $keys );
                // Get into cache (default process cache group)
+               $key = reset( $keys );
+               $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
+               $this->assertEquals( 9, $hit, "Value recently interim-cached" );
+
+               $mockWallClock += 0.2; // interim key not brand new
+               $this->cache->clearProcessCache();
                $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
-               $this->assertEquals( 10, $hit, "Value calculated" );
+               $this->assertEquals( 10, $hit, "Value calculated (interim key not recent and reset)" );
                $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
-               $this->assertEquals( 10, $hit, "Value cached" );
+               $this->assertEquals( 10, $hit, "Value process cached" );
+
+               $mockWallClock += 0.2; // interim key not brand new
                $outerCallback = function () use ( &$callback, $key ) {
                        $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
 
@@ -179,7 +200,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        /**
         * @dataProvider getWithSetCallback_provider
         * @covers WANObjectCache::getWithSetCallback()
-        * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::fetchOrRegenerate()
         * @param array $extOpts
         * @param bool $versioned
         */
@@ -240,7 +261,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $t2 = $cache->getCheckKeyTime( $cKey2 );
                $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
 
-               $mockWallClock += 0.01;
+               $mockWallClock += 0.2; // interim key is not brand new and check keys have past values
                $priorTime = $mockWallClock; // reference time
                $wasSet = 0;
                $v = $cache->getWithSetCallback(
@@ -255,11 +276,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
                $curTTL = null;
                $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
-               if ( $versioned ) {
-                       $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
-               } else {
-                       $this->assertEquals( $value, $v, "Value returned" );
-               }
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
 
                $wasSet = 0;
@@ -365,7 +382,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        /**
         * @dataProvider getWithSetCallback_provider
         * @covers WANObjectCache::getWithSetCallback()
-        * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::fetchOrRegenerate()
         * @param array $extOpts
         * @param bool $versioned
         */
@@ -437,9 +454,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        return $value;
                };
 
-               $cache = new NearExpiringWANObjectCache( [
-                       'cache'        => new HashBagOStuff()
-               ] );
+               $cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] );
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
 
                $wasSet = 0;
                $key = wfRandomString();
@@ -447,6 +464,8 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value calculated" );
+
+               $mockWallClock += 0.2; // interim key is not brand new
                $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
                $this->assertEquals( 2, $wasSet, "Value re-calculated" );
 
@@ -529,15 +548,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 2, $wasSet, "Value re-calculated" );
        }
 
-       /**
-        * @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
@@ -591,15 +601,16 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $value = "@efef$";
                $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
                $v = $cache->getMultiWithSetCallback(
-                       $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
+                       $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
                $this->assertEquals( $value, $v[$keyB], "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated" );
-               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
+               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
+
                $v = $cache->getMultiWithSetCallback(
-                       $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
+                       $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
                $this->assertEquals( $value, $v[$keyB], "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value not regenerated" );
-               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
+               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
 
                $mockWallClock += 1;
 
@@ -634,11 +645,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
                $curTTL = null;
                $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
-               if ( $versioned ) {
-                       $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
-               } else {
-                       $this->assertEquals( $value, $v, "Value returned" );
-               }
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
 
                $wasSet = 0;
@@ -765,12 +772,13 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
                $this->assertEquals( $value, $v[$keyB], "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value regenerated" );
-               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
+               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
+
                $v = $cache->getMultiWithUnionSetCallback(
                        $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
                $this->assertEquals( $value, $v[$keyB], "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value not regenerated" );
-               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
+               $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
 
                $mockWallClock += 1;
 
@@ -803,11 +811,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
                $curTTL = null;
                $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
-               if ( $versioned ) {
-                       $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
-               } else {
-                       $this->assertEquals( $value, $v, "Value returned" );
-               }
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
 
                $wasSet = 0;
@@ -865,13 +869,16 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
        /**
         * @covers WANObjectCache::getWithSetCallback()
-        * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::fetchOrRegenerate()
         */
        public function testLockTSE() {
                $cache = $this->cache;
                $key = wfRandomString();
                $value = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $calls = 0;
                $func = function () use ( &$calls, $value, $cache, $key ) {
                        ++$calls;
@@ -892,6 +899,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 1, $calls, 'Callback was not used' );
 
                $cache->delete( $key );
+               $mockWallClock += 0.001; // cached values will be newer than tombstone
                $ret = $cache->getWithSetCallback( $key, 30, $func,
                        [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
                $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
@@ -905,41 +913,88 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
        /**
         * @covers WANObjectCache::getWithSetCallback()
-        * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::fetchOrRegenerate()
         * @covers WANObjectCache::set()
         */
        public function testLockTSESlow() {
                $cache = $this->cache;
                $key = wfRandomString();
+               $key2 = wfRandomString();
                $value = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $calls = 0;
-               $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
+               $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, &$mockWallClock ) {
                        ++$calls;
-                       $setOpts['since'] = microtime( true ) - 10;
-                       // Immediately kill any mutex rather than waiting a second
-                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+                       $setOpts['since'] = $mockWallClock - 10;
                        return $value;
                };
 
-               // Value should be marked as stale due to snapshot lag
+               // Value should be given a low logical TTL due to snapshot lag
                $curTTL = null;
-               $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
                $this->assertEquals( $value, $ret );
                $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
-               $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
+               $this->assertEquals( 1, $curTTL, 'Value has reduced logical TTL', 0.01 );
                $this->assertEquals( 1, $calls, 'Value was generated' );
 
+               $mockWallClock += 2; // low logical TTL expired
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback was not used (interim value used)' );
+
+               $mockWallClock += 2; // low logical TTL expired
                // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
                $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
-               $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
                $this->assertEquals( $value, $ret );
-               $this->assertEquals( 1, $calls, 'Callback was not used' );
+               $this->assertEquals( 2, $calls, 'Callback was not used (mutex not acquired)' );
+
+               $mockWallClock += 301; // physical TTL expired
+               // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
+               $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 3, $calls, 'Callback was used (mutex not acquired, not in cache)' );
+
+               $calls = 0;
+               $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
+                       ++$calls;
+                       $setOpts['lag'] = 15;
+                       return $value;
+               };
+
+               // Value should be given a low logical TTL due to replication lag
+               $curTTL = null;
+               $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( $value, $cache->get( $key2, $curTTL ), 'Value was populated' );
+               $this->assertEquals( 30, $curTTL, 'Value has reduced logical TTL', 0.01 );
+               $this->assertEquals( 1, $calls, 'Value was generated' );
+
+               $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 1, $calls, 'Callback was used (not expired)' );
+
+               $mockWallClock += 31;
+
+               $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback was used (mutex acquired)' );
        }
 
        /**
         * @covers WANObjectCache::getWithSetCallback()
-        * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::fetchOrRegenerate()
         */
        public function testBusyValue() {
                $cache = $this->cache;
@@ -947,11 +1002,12 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $value = wfRandomString();
                $busyValue = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $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;
                };
 
@@ -959,6 +1015,8 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( $value, $ret );
                $this->assertEquals( 1, $calls, 'Value was populated' );
 
+               $mockWallClock += 0.2; // interim keys not brand new
+
                // Acquire a lock to verify that getWithSetCallback uses busyValue properly
                $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
 
@@ -980,6 +1038,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
 
                $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+               $mockWallClock += 0.001; // cached values will be newer than tombstone
                $ret = $cache->getWithSetCallback( $key, 30, $func,
                        [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
                $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
@@ -1213,7 +1272,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        /**
         * @dataProvider getWithSetCallback_versions_provider
         * @covers WANObjectCache::getWithSetCallback()
-        * @covers WANObjectCache::doGetWithSetCallback()
+        * @covers WANObjectCache::fetchOrRegenerate()
         * @param array $extOpts
         * @param bool $versioned
         */
@@ -1300,6 +1359,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        public function testInterimHoldOffCaching() {
                $cache = $this->cache;
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $value = 'CRL-40-940';
                $wasCalled = 0;
                $func = function () use ( &$wasCalled, $value ) {
@@ -1314,10 +1376,16 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $v = $cache->getWithSetCallback( $key, 60, $func );
                $v = $cache->getWithSetCallback( $key, 60, $func );
                $this->assertEquals( 1, $wasCalled, 'Value cached' );
+
                $cache->delete( $key );
+               $mockWallClock += 0.001; // cached values will be newer than tombstone
                $v = $cache->getWithSetCallback( $key, 60, $func );
                $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
                $v = $cache->getWithSetCallback( $key, 60, $func );
+               $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
+
+               $mockWallClock += 0.2; // interim key not brand new
+               $v = $cache->getWithSetCallback( $key, 60, $func );
                $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
                // Lock up the mutex so interim cache is used
                $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
@@ -1444,7 +1512,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->internalCache->set(
                        WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
                        [
-                               WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
+                               WANObjectCache::FLD_FORMAT_VERSION => WANObjectCache::VERSION,
                                WANObjectCache::FLD_VALUE => $value,
                                WANObjectCache::FLD_TTL => 3600,
                                WANObjectCache::FLD_TIME => $goodTime
@@ -1453,7 +1521,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->internalCache->set(
                        WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
                        [
-                               WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
+                               WANObjectCache::FLD_FORMAT_VERSION => WANObjectCache::VERSION,
                                WANObjectCache::FLD_VALUE => $value,
                                WANObjectCache::FLD_TTL => 3600,
                                WANObjectCache::FLD_TIME => $badTime
@@ -1490,7 +1558,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
                $backend->expects( $this->once() )->method( 'get' )
                        ->willReturn( [
-                               WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
+                               WANObjectCache::FLD_FORMAT_VERSION => WANObjectCache::VERSION,
                                WANObjectCache::FLD_VALUE => 'value',
                                WANObjectCache::FLD_TTL => 3600,
                                WANObjectCache::FLD_TIME => 300,
@@ -1762,14 +1830,14 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
        /**
         * @dataProvider statsKeyProvider
-        * @covers WANObjectCache::determineKeyClass
+        * @covers WANObjectCache::determineKeyClassForStats
         */
        public function testStatsKeyClass( $key, $class ) {
                $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
                        'cache' => new HashBagOStuff
                ] ) );
 
-               $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
+               $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
        }
 }