parent::setUp();
$this->cache = new WANObjectCache( [
- 'cache' => new HashBagOStuff(),
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] )
+ 'cache' => new HashBagOStuff()
] );
$wanCache = TestingAccessWrapper::newFromObject( $this->cache );
}
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 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 ] );
$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(
return $value;
};
- $cache = new NearExpiringWANObjectCache( [
- 'cache' => new HashBagOStuff(),
- 'pool' => 'empty',
- ] );
+ $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" );
};
$cache = new NearExpiringWANObjectCache( [
'cache' => new HashBagOStuff(),
- 'pool' => 'empty',
'asyncHandler' => $asyncHandler
] );
$this->assertEquals( $value, $v, "New value stored" );
$cache = new PopularityRefreshingWANObjectCache( [
- 'cache' => new HashBagOStuff(),
- 'pool' => 'empty'
+ 'cache' => new HashBagOStuff()
] );
$mockWallClock = $priorTime;
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' ] );
$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' );
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)' );
}
/**
$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' );
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 );
->willReturn( false );
$wanCache = new WANObjectCache( [
- 'cache' => $backend,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] )
+ 'cache' => $backend
] );
$isStale = null;
$localBag->expects( $this->never() )->method( 'delete' );
$wanCache = new WANObjectCache( [
'cache' => $localBag,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] ),
'mcrouterAware' => true,
'region' => 'pmtpa',
'cluster' => 'mw-wan'
->setMethods( [ 'set' ] )->getMock();
$wanCache = new WANObjectCache( [
'cache' => $localBag,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] ),
'mcrouterAware' => true,
'region' => 'pmtpa',
'cluster' => 'mw-wan'
->setMethods( [ 'set' ] )->getMock();
$wanCache = new WANObjectCache( [
'cache' => $localBag,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] ),
'mcrouterAware' => true,
'region' => 'pmtpa',
'cluster' => 'mw-wan'
->setMethods( [ 'delete' ] )->getMock();
$wanCache = new WANObjectCache( [
'cache' => $localBag,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] ),
'mcrouterAware' => true,
'region' => 'pmtpa',
'cluster' => 'mw-wan'
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 );
$cache = new WANObjectCache( [
'cache' => $bag,
- 'pool' => 'testcache-hash',
'epoch' => $now - 3600
] );
$cache->setMockTime( $now );
$now += 30;
$cache = new WANObjectCache( [
'cache' => $bag,
- 'pool' => 'testcache-hash',
'epoch' => $now + 3600
] );
$cache->setMockTime( $now );
->willReturn( 'special' );
$wanCache = new WANObjectCache( [
- 'cache' => $backend,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] )
+ 'cache' => $backend
] );
$this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
->willReturn( 'special' );
$wanCache = new WANObjectCache( [
- 'cache' => $backend,
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] )
+ 'cache' => $backend
] );
$this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
/**
* @dataProvider statsKeyProvider
- * @covers WANObjectCache::determineKeyClass
+ * @covers WANObjectCache::determineKeyClassForStats
*/
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 ) );
+ $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
}
}