objectcache: simplify WAN cache unwrap() method by removing FLG_STALE
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 22aa667..a044372 100644 (file)
@@ -30,9 +30,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                parent::setUp();
 
                $this->cache = new WANObjectCache( [
-                       'cache' => new HashBagOStuff(),
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => new HashBagOStuff()
                ] );
 
                $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
@@ -197,8 +195,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $priorAsOf = null;
                $wasSet = 0;
                $func = function ( $old, &$ttl, &$opts, $asOf )
-               use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
-               {
+               use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) {
                        ++$wasSet;
                        $priorValue = $old;
                        $priorAsOf = $asOf;
@@ -206,6 +203,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        return $value;
                };
 
+               $mockWallClock = 1549343530.2053;
+               $priorTime = $mockWallClock; // reference time
+               $cache->setMockTime( $mockWallClock );
+
                $wasSet = 0;
                $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
                $this->assertEquals( $value, $v, "Value returned" );
@@ -224,10 +225,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 0, $wasSet, "Value not regenerated" );
 
-               $mockWallClock = microtime( true );
-               $priorTime = $mockWallClock; // reference time
-               $cache->setMockTime( $mockWallClock );
-
                $mockWallClock += 1;
 
                $wasSet = 0;
@@ -285,7 +282,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        return 'xxx' . $wasSet;
                };
 
-               $mockWallClock = microtime( true );
+               $mockWallClock = 1549343530.2053;
                $priorTime = $mockWallClock; // reference time
 
                $wasSet = 0;
@@ -351,7 +348,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
                $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
 
-               // Change of refresh increase to unity as staleness approaches graceTTL
+               // Chance of refresh increase to unity as staleness approaches graceTTL
                $mockWallClock += $cache::TTL_WEEK; // 8 days of being stale
                $v = $cache->getWithSetCallback(
                        $key,
@@ -365,6 +362,65 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
        }
 
+       /**
+        * @dataProvider getWithSetCallback_provider
+        * @covers WANObjectCache::getWithSetCallback()
+        * @covers WANObjectCache::doGetWithSetCallback()
+        * @param array $extOpts
+        * @param bool $versioned
+        */
+       function testGetWithSetcallback_touched( array $extOpts, $versioned ) {
+               $cache = $this->cache;
+
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
+               $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
+               use ( &$wasSet ) {
+                       ++$wasSet;
+
+                       return 'xxx' . $wasSet;
+               };
+
+               $key = wfRandomString();
+               $wasSet = 0;
+               $touched = null;
+               $touchedCallback = function () use ( &$touched ) {
+                       return $touched;
+               };
+               $v = $cache->getWithSetCallback(
+                       $key,
+                       $cache::TTL_INDEFINITE,
+                       $checkFunc,
+                       [ 'touchedCallback' => $touchedCallback ] + $extOpts
+               );
+               $mockWallClock += 60;
+               $v = $cache->getWithSetCallback(
+                       $key,
+                       $cache::TTL_INDEFINITE,
+                       $checkFunc,
+                       [ 'touchedCallback' => $touchedCallback ] + $extOpts
+               );
+               $this->assertEquals( 'xxx1', $v, "Value was computed once" );
+               $this->assertEquals( 1, $wasSet, "Value was computed once" );
+
+               $touched = $mockWallClock - 10;
+               $v = $cache->getWithSetCallback(
+                       $key,
+                       $cache::TTL_INDEFINITE,
+                       $checkFunc,
+                       [ 'touchedCallback' => $touchedCallback ] + $extOpts
+               );
+               $v = $cache->getWithSetCallback(
+                       $key,
+                       $cache::TTL_INDEFINITE,
+                       $checkFunc,
+                       [ 'touchedCallback' => $touchedCallback ] + $extOpts
+               );
+               $this->assertEquals( 'xxx2', $v, "Value was recomputed once" );
+               $this->assertEquals( 2, $wasSet, "Value was recomputed once" );
+       }
+
        public static function getWithSetCallback_provider() {
                return [
                        [ [], false ],
@@ -382,8 +438,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                };
 
                $cache = new NearExpiringWANObjectCache( [
-                       'cache'        => new HashBagOStuff(),
-                       'pool'         => 'empty',
+                       'cache'        => new HashBagOStuff()
                ] );
 
                $wasSet = 0;
@@ -410,11 +465,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                };
                $cache = new NearExpiringWANObjectCache( [
                        'cache'        => new HashBagOStuff(),
-                       'pool'         => 'empty',
                        'asyncHandler' => $asyncHandler
                ] );
 
-               $mockWallClock = microtime( true );
+               $mockWallClock = 1549343530.2053;
                $priorTime = $mockWallClock; // reference time
                $cache->setMockTime( $mockWallClock );
 
@@ -442,8 +496,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( $value, $v, "New value stored" );
 
                $cache = new PopularityRefreshingWANObjectCache( [
-                       'cache'   => new HashBagOStuff(),
-                       'pool'    => 'empty'
+                       'cache'   => new HashBagOStuff()
                ] );
 
                $mockWallClock = $priorTime;
@@ -515,6 +568,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        return "@$id$";
                };
 
+               $mockWallClock = 1549343530.2053;
+               $priorTime = $mockWallClock; // reference time
+               $cache->setMockTime( $mockWallClock );
+
                $wasSet = 0;
                $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
                $value = "@3353$";
@@ -544,10 +601,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 1, $wasSet, "Value not regenerated" );
                $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
 
-               $mockWallClock = microtime( true );
-               $priorTime = $mockWallClock; // reference time
-               $cache->setMockTime( $mockWallClock );
-
                $mockWallClock += 1;
 
                $wasSet = 0;
@@ -636,7 +689,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
                        WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
                ] );
-               $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
+               $wanCache = new WANObjectCache( [ 'cache' => $localBag ] );
 
                // Warm the process cache
                $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
@@ -688,6 +741,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        return $newValues;
                };
 
+               $mockWallClock = 1549343530.2053;
+               $priorTime = $mockWallClock; // reference time
+               $cache->setMockTime( $mockWallClock );
+
                $wasSet = 0;
                $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
                $value = "@3353$";
@@ -715,10 +772,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 1, $wasSet, "Value not regenerated" );
                $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
 
-               $mockWallClock = microtime( true );
-               $priorTime = $mockWallClock; // reference time
-               $cache->setMockTime( $mockWallClock );
-
                $mockWallClock += 1;
 
                $wasSet = 0;
@@ -858,30 +911,65 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        public function testLockTSESlow() {
                $cache = $this->cache;
                $key = wfRandomString();
+               $key2 = wfRandomString();
                $value = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $priorTime = $mockWallClock;
+               $cache->setMockTime( $mockWallClock );
+
                $calls = 0;
-               $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
+               $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $priorTime ) {
                        ++$calls;
-                       $setOpts['since'] = microtime( true ) - 10;
-                       // Immediately kill any mutex rather than waiting a second
-                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+                       $setOpts['since'] = $priorTime - 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;
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
+
                // 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( 3, $calls, 'Callback was not used (mutex not acquired)' );
+
+               $calls = 0;
+               $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $priorTime ) {
+                       ++$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)' );
        }
 
        /**
@@ -897,8 +985,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $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;
                };
 
@@ -952,6 +1038,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $key2 = wfRandomString();
                $key3 = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $priorTime = $mockWallClock; // reference time
+               $cache->setMockTime( $mockWallClock );
+
                $cache->set( $key1, $value1, 5 );
                $cache->set( $key2, $value2, 10 );
 
@@ -969,10 +1059,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $cKey1 = wfRandomString();
                $cKey2 = wfRandomString();
 
-               $mockWallClock = microtime( true );
-               $priorTime = $mockWallClock; // reference time
-               $cache->setMockTime( $mockWallClock );
-
                $mockWallClock += 1;
 
                $curTTLs = [];
@@ -1016,7 +1102,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $value1 = wfRandomString();
                $value2 = wfRandomString();
 
-               $mockWallClock = microtime( true );
+               $mockWallClock = 1549343530.2053;
                $cache->setMockTime( $mockWallClock );
 
                // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
@@ -1304,7 +1390,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $cache = $this->cache;
                $key = wfRandomString();
 
-               $mockWallClock = microtime( true );
+               $mockWallClock = 1549343530.2053;
                $priorTime = $mockWallClock; // reference time
                $cache->setMockTime( $mockWallClock );
 
@@ -1347,18 +1433,22 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $tKey2 = wfRandomString();
                $value = 'meow';
 
+               $mockWallClock = 1549343530.2053;
+               $priorTime = $mockWallClock; // reference time
+               $this->cache->setMockTime( $mockWallClock );
+
                // Two check keys are newer (given hold-off) than $key, another is older
                $this->internalCache->set(
                        WANObjectCache::TIME_KEY_PREFIX . $tKey2,
-                       WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
+                       WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 3 )
                );
                $this->internalCache->set(
                        WANObjectCache::TIME_KEY_PREFIX . $tKey2,
-                       WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
+                       WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 5 )
                );
                $this->internalCache->set(
                        WANObjectCache::TIME_KEY_PREFIX . $tKey1,
-                       WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
+                       WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 30 )
                );
                $this->cache->set( $key, $value, 30 );
 
@@ -1442,9 +1532,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->willReturn( false );
 
                $wanCache = new WANObjectCache( [
-                       'cache' => $backend,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => $backend
                ] );
 
                $isStale = null;
@@ -1494,8 +1582,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $localBag->expects( $this->never() )->method( 'delete' );
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1520,8 +1606,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'set' ] )->getMock();
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1538,8 +1622,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'set' ] )->getMock();
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1556,8 +1638,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'delete' ] )->getMock();
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1571,7 +1651,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
        public function testEpoch() {
                $bag = new HashBagOStuff();
-               $cache = new WANObjectCache( [ 'cache' => $bag, 'pool' => 'testcache-hash' ] );
+               $cache = new WANObjectCache( [ 'cache' => $bag ] );
                $key = $cache->makeGlobalKey( 'The whole of the Law' );
 
                $now = microtime( true );
@@ -1587,7 +1667,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
                $cache = new WANObjectCache( [
                        'cache' => $bag,
-                       'pool' => 'testcache-hash',
                        'epoch' => $now - 3600
                ] );
                $cache->setMockTime( $now );
@@ -1598,7 +1677,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $now += 30;
                $cache = new WANObjectCache( [
                        'cache' => $bag,
-                       'pool' => 'testcache-hash',
                        'epoch' => $now + 3600
                ] );
                $cache->setMockTime( $now );
@@ -1684,9 +1762,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->willReturn( 'special' );
 
                $wanCache = new WANObjectCache( [
-                       'cache' => $backend,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => $backend
                ] );
 
                $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
@@ -1702,9 +1778,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->willReturn( 'special' );
 
                $wanCache = new WANObjectCache( [
-                       'cache' => $backend,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => $backend
                ] );
 
                $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
@@ -1725,9 +1799,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
         */
        public function testStatsKeyClass( $key, $class ) {
                $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
-                       'cache' => new HashBagOStuff,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => new HashBagOStuff
                ] ) );
 
                $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );