parent::setUp();
$this->cache = new WANObjectCache( [
- 'cache' => new HashBagOStuff(),
- 'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( [] )
+ 'cache' => new HashBagOStuff()
] );
$wanCache = TestingAccessWrapper::newFromObject( $this->cache );
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" );
$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;
return 'xxx' . $wasSet;
};
- $mockWallClock = microtime( true );
+ $mockWallClock = 1549343530.2053;
$priorTime = $mockWallClock; // reference time
$wasSet = 0;
function testGetWithSetcallback_touched( array $extOpts, $versioned ) {
$cache = $this->cache;
- $mockWallClock = microtime( true );
+ $mockWallClock = 1549343530.2053;
$cache->setMockTime( $mockWallClock );
$checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
};
$cache = new NearExpiringWANObjectCache( [
- 'cache' => new HashBagOStuff(),
- 'pool' => 'empty',
+ 'cache' => new HashBagOStuff()
] );
$wasSet = 0;
};
$cache = new NearExpiringWANObjectCache( [
'cache' => new HashBagOStuff(),
- 'pool' => 'empty',
'asyncHandler' => $asyncHandler
] );
- $mockWallClock = microtime( true );
+ $mockWallClock = 1549343530.2053;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
$this->assertEquals( $value, $v, "New value stored" );
$cache = new PopularityRefreshingWANObjectCache( [
- 'cache' => new HashBagOStuff(),
- 'pool' => 'empty'
+ 'cache' => new HashBagOStuff()
] );
$mockWallClock = $priorTime;
return "@$id$";
};
+ $mockWallClock = 1549343530.2053;
+ $priorTime = $mockWallClock; // reference time
+ $cache->setMockTime( $mockWallClock );
+
$wasSet = 0;
$keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
$value = "@3353$";
$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;
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' ] );
return $newValues;
};
+ $mockWallClock = 1549343530.2053;
+ $priorTime = $mockWallClock; // reference time
+ $cache->setMockTime( $mockWallClock );
+
$wasSet = 0;
$keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
$value = "@3353$";
$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;
public function testLockTSESlow() {
$cache = $this->cache;
$key = wfRandomString();
+ $key2 = wfRandomString();
$value = wfRandomString();
+ $mockWallClock = 1549343530.2053;
+ $priorTime = $mockWallClock;
+ $cache->setMockTime( $mockWallClock );
+
$calls = 0;
- $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
+ $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $priorTime ) {
++$calls;
- $setOpts['since'] = microtime( true ) - 10;
- // Immediately kill any mutex rather than waiting a second
- $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+ $setOpts['since'] = $priorTime - 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;
+
+ $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+ $this->assertEquals( $value, $ret );
+ $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
+
// 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( 3, $calls, 'Callback was not used (mutex not acquired)' );
+
+ $calls = 0;
+ $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $priorTime ) {
+ ++$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)' );
}
/**
$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;
};
$key2 = wfRandomString();
$key3 = wfRandomString();
+ $mockWallClock = 1549343530.2053;
+ $priorTime = $mockWallClock; // reference time
+ $cache->setMockTime( $mockWallClock );
+
$cache->set( $key1, $value1, 5 );
$cache->set( $key2, $value2, 10 );
$cKey1 = wfRandomString();
$cKey2 = wfRandomString();
- $mockWallClock = microtime( true );
- $priorTime = $mockWallClock; // reference time
- $cache->setMockTime( $mockWallClock );
-
$mockWallClock += 1;
$curTTLs = [];
$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
$cache = $this->cache;
$key = wfRandomString();
- $mockWallClock = microtime( true );
+ $mockWallClock = 1549343530.2053;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
$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 );
->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' ) );
*/
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 ) );