Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index c2be911..7053fc1 100644 (file)
@@ -17,6 +17,9 @@ use Wikimedia\TestingAccessWrapper;
  * @covers WANObjectCache::setInterimValue
  */
 class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
+
+       use MediaWikiCoversValidator;
+
        /** @var TimeAdjustableWANObjectCache */
        private $cache;
        /** @var BagOStuff */
@@ -370,15 +373,15 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
        public function testPreemtiveRefresh() {
                $value = 'KatCafe';
                $wasSet = 0;
-               $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, $value )
+               $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
                {
                        ++$wasSet;
                        return $value;
                };
 
                $cache = new NearExpiringWANObjectCache( [
-                       'cache'   => new HashBagOStuff(),
-                       'pool'    => 'empty'
+                       'cache'        => new HashBagOStuff(),
+                       'pool'         => 'empty',
                ] );
 
                $wasSet = 0;
@@ -399,12 +402,50 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
                $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
                $this->assertEquals( 1, $wasSet, "Value cached" );
 
+               $asycList = [];
+               $asyncHandler = function ( $callback ) use ( &$asycList ) {
+                       $asycList[] = $callback;
+               };
+               $cache = new NearExpiringWANObjectCache( [
+                       'cache'        => new TimeAdjustableHashBagOStuff(),
+                       'pool'         => 'empty',
+                       'asyncHandler' => $asyncHandler
+               ] );
+
+               $now = microtime( true ); // reference time
+               $cache->setTime( $now );
+
+               $wasSet = 0;
+               $key = wfRandomString();
+               $checkKey = wfRandomString();
+               $opts = [ 'lowTTL' => 100 ];
+               $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
+               $this->assertEquals( $value, $v, "Value returned" );
+               $this->assertEquals( 1, $wasSet, "Value calculated" );
+               $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
+               $this->assertEquals( 1, $wasSet, "Cached value used" );
+               $this->assertEquals( $v, $value, "Value cached" );
+
+               $cache->setTime( $now + 250 );
+               $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
+               $this->assertEquals( $value, $v, "Value returned" );
+               $this->assertEquals( 1, $wasSet, "Stale value used" );
+               $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
+               $value = 'NewCatsInTown'; // change callback return value
+               $asycList[0](); // run the refresh callback
+               $asycList = [];
+               $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
+               $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
+               $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
+               $this->assertEquals( $value, $v, "New value stored" );
+
                $cache = new PopularityRefreshingWANObjectCache( [
-                       'cache'   => new HashBagOStuff(),
+                       'cache'   => new TimeAdjustableHashBagOStuff(),
                        'pool'    => 'empty'
                ] );
 
-               $now = microtime( true ); // reference time
+               $cache->setTime( $now );
+
                $wasSet = 0;
                $key = wfRandomString();
                $opts = [ 'hotTTR' => 900 ];
@@ -424,6 +465,7 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
                $this->assertEquals( 1, $wasSet, "Value calculated" );
                $cache->setTime( $now + 30 );
                $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
+               $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 2, $wasSet, "Value re-calculated" );
        }
 
@@ -1241,6 +1283,7 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
         * @covers WANObjectCache::touchCheckKey
         * @covers WANObjectCache::resetCheckKey
         * @covers WANObjectCache::getCheckKeyTime
+        * @covers WANObjectCache::getMultiCheckKeyTime
         * @covers WANObjectCache::makePurgeValue
         * @covers WANObjectCache::parsePurgeValue
         */