* @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 {
}
public function testProcessCache() {
+ $mockWallClock = 1549343530.2053;
+ $this->cache->setMockTime( $mockWallClock );
+
$hit = 0;
$callback = function () use ( &$hit ) {
++$hit;
$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 ] );
/**
* @dataProvider getWithSetCallback_provider
* @covers WANObjectCache::getWithSetCallback()
- * @covers WANObjectCache::doGetWithSetCallback()
+ * @covers WANObjectCache::fetchOrRegenerate()
* @param array $extOpts
* @param bool $versioned
*/
$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(
$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;
/**
* @dataProvider getWithSetCallback_provider
* @covers WANObjectCache::getWithSetCallback()
- * @covers WANObjectCache::doGetWithSetCallback()
+ * @covers WANObjectCache::fetchOrRegenerate()
* @param array $extOpts
* @param bool $versioned
*/
return $value;
};
- $cache = new NearExpiringWANObjectCache( [
- 'cache' => new HashBagOStuff()
- ] );
+ $cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] );
+ $mockWallClock = 1549343530.2053;
+ $cache->setMockTime( $mockWallClock );
$wasSet = 0;
$key = wfRandomString();
$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" );
$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
$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;
$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;
$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;
$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;
/**
* @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;
$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' );
/**
* @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;
$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;
};
$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 );
$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' );
/**
* @dataProvider getWithSetCallback_versions_provider
* @covers WANObjectCache::getWithSetCallback()
- * @covers WANObjectCache::doGetWithSetCallback()
+ * @covers WANObjectCache::fetchOrRegenerate()
* @param array $extOpts
* @param bool $versioned
*/
public function testInterimHoldOffCaching() {
$cache = $this->cache;
+ $mockWallClock = 1549343530.2053;
+ $cache->setMockTime( $mockWallClock );
+
$value = 'CRL-40-940';
$wasCalled = 0;
$func = function () use ( &$wasCalled, $value ) {
$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 );
$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
$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
->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,
/**
* @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 ) );
}
}