Merge "Add support for Argon2 password hashing"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 662bb96..d9b7e18 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;
@@ -952,6 +1005,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 +1026,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 +1069,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 +1357,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 +1400,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 +1499,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 +1549,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 +1573,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 +1589,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 +1605,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'
@@ -1569,6 +1616,42 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $wanCache->resetCheckKey( 'test' );
        }
 
+       public function testEpoch() {
+               $bag = new HashBagOStuff();
+               $cache = new WANObjectCache( [ 'cache' => $bag ] );
+               $key = $cache->makeGlobalKey( 'The whole of the Law' );
+
+               $now = microtime( true );
+               $cache->setMockTime( $now );
+
+               $cache->set( $key, 'Do what thou Wilt' );
+               $cache->touchCheckKey( $key );
+
+               $then = $now;
+               $now += 30;
+               $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
+               $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
+
+               $cache = new WANObjectCache( [
+                       'cache' => $bag,
+                       'epoch' => $now - 3600
+               ] );
+               $cache->setMockTime( $now );
+
+               $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
+               $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
+
+               $now += 30;
+               $cache = new WANObjectCache( [
+                       'cache' => $bag,
+                       'epoch' => $now + 3600
+               ] );
+               $cache->setMockTime( $now );
+
+               $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
+               $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
+       }
+
        /**
         * @dataProvider provideAdaptiveTTL
         * @covers WANObjectCache::adaptiveTTL()
@@ -1646,9 +1729,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' ) );
@@ -1664,9 +1745,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' ) );
@@ -1687,9 +1766,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 ) );