X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Flibs%2Fobjectcache%2FWANObjectCacheTest.php;h=592f72a09ea715af1e58a9e8f3c522e042204b08;hb=1772c4fe347cc7fc133e5fbb7f01d1e2ddfc812f;hp=934d49a8b20e6bb970d2572c335f0c814aa16ad4;hpb=50495b4f4e356882dd464685cc6f32b415371233;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php index 934d49a8b2..592f72a09e 100644 --- a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php +++ b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php @@ -275,6 +275,66 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase { ]; } + public function testPreemtiveRefresh() { + $value = 'KatCafe'; + $wasSet = 0; + $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, $value ) + { + ++$wasSet; + return $value; + }; + + $cache = new NearExpiringWANObjectCache( [ + 'cache' => new HashBagOStuff(), + 'pool' => 'empty' + ] ); + + $wasSet = 0; + $key = wfRandomString(); + $opts = [ 'lowTTL' => 30 ]; + $v = $cache->getWithSetCallback( $key, 20, $func, $opts ); + $this->assertEquals( $value, $v, "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value calculated" ); + $v = $cache->getWithSetCallback( $key, 20, $func, $opts ); + $this->assertEquals( 2, $wasSet, "Value re-calculated" ); + + $wasSet = 0; + $key = wfRandomString(); + $opts = [ 'lowTTL' => 1 ]; + $v = $cache->getWithSetCallback( $key, 30, $func, $opts ); + $this->assertEquals( $value, $v, "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value calculated" ); + $v = $cache->getWithSetCallback( $key, 30, $func, $opts ); + $this->assertEquals( 1, $wasSet, "Value cached" ); + + $cache = new PopularityRefreshingWANObjectCache( [ + 'cache' => new HashBagOStuff(), + 'pool' => 'empty' + ] ); + + $now = microtime( true ); // reference time + $wasSet = 0; + $key = wfRandomString(); + $opts = [ 'hotTTR' => 900 ]; + $v = $cache->getWithSetCallback( $key, 60, $func, $opts ); + $this->assertEquals( $value, $v, "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value calculated" ); + $cache->setTime( $now + 30 ); + $v = $cache->getWithSetCallback( $key, 60, $func, $opts ); + $this->assertEquals( 1, $wasSet, "Value cached" ); + + $wasSet = 0; + $key = wfRandomString(); + $opts = [ 'hotTTR' => 10 ]; + $cache->setTime( $now ); + $v = $cache->getWithSetCallback( $key, 60, $func, $opts ); + $this->assertEquals( $value, $v, "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value calculated" ); + $cache->setTime( $now + 30 ); + $v = $cache->getWithSetCallback( $key, 60, $func, $opts ); + $this->assertEquals( 2, $wasSet, "Value re-calculated" ); + } + /** * @covers WANObjectCache::getWithSetCallback() * @covers WANObjectCache::doGetWithSetCallback() @@ -1346,3 +1406,44 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) ); } } + +class TimeAdjustableHashBagOStuff extends HashBagOStuff { + private $timeOverride = 0; + + public function setTime( $time ) { + $this->timeOverride = $time; + } + + protected function getCurrentTime() { + return $this->timeOverride ?: parent::getCurrentTime(); + } +} + +class TimeAdjustableWANObjectCache extends WANObjectCache { + private $timeOverride = 0; + + public function setTime( $time ) { + $this->timeOverride = $time; + if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) { + $this->cache->setTime( $time ); + } + } + + protected function getCurrentTime() { + return $this->timeOverride ?: parent::getCurrentTime(); + } +} + +class NearExpiringWANObjectCache extends TimeAdjustableWANObjectCache { + const CLOCK_SKEW = 1; + + protected function worthRefreshExpiring( $curTTL, $lowTTL ) { + return ( ( $curTTL + self::CLOCK_SKEW ) < $lowTTL ); + } +} + +class PopularityRefreshingWANObjectCache extends TimeAdjustableWANObjectCache { + protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) { + return ( ( $now - $asOf ) > $timeTillRefresh ); + } +}