$this->cache = ObjectCache::getWANInstance( $name );
} else {
- $this->cache = new WANObjectCache( array(
+ $this->cache = new WANObjectCache( [
'cache' => new HashBagOStuff(),
'pool' => 'testcache-hash',
- 'relayer' => new EventRelayerNull( array() )
- ) );
+ 'relayer' => new EventRelayerNull( [] )
+ ] );
}
$wanCache = TestingAccessWrapper::newFromObject( $this->cache );
* @param integer $ttl
*/
public function testSetAndGet( $value, $ttl ) {
+ $curTTL = null;
+ $asOf = null;
$key = wfRandomString();
+
+ $this->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 );
- $curTTL = null;
- $this->assertEquals( $value, $this->cache->get( $key, $curTTL ) );
+ $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
if ( is_infinite( $ttl ) || $ttl == 0 ) {
$this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
} else {
$this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
$this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
}
+ $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
+ $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
}
public static function provideSetAndGet() {
- return array(
- array( 14141, 3 ),
- array( 3535.666, 3 ),
- array( array(), 3 ),
- array( null, 3 ),
- array( '0', 3 ),
- array( (object)array( 'meow' ), 3 ),
- array( INF, 3 ),
- array( '', 3 ),
- array( 'pizzacat', INF ),
- );
+ return [
+ [ 14141, 3 ],
+ [ 3535.666, 3 ],
+ [ [], 3 ],
+ [ null, 3 ],
+ [ '0', 3 ],
+ [ (object)[ 'meow' ], 3 ],
+ [ INF, 3 ],
+ [ '', 3 ],
+ [ 'pizzacat', INF ],
+ ];
}
/**
public function testStaleSet() {
$key = wfRandomString();
$value = wfRandomString();
- $this->cache->set( $key, $value, 3, array( 'since' => microtime( true ) - 30 ) );
+ $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
$this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
}
/**
+ * @dataProvider getWithSetCallback_provider
* @covers WANObjectCache::getWithSetCallback()
* @covers WANObjectCache::doGetWithSetCallback()
+ * @param array $extOpts
+ * @param bool $versioned
*/
- public function testGetWithSetCallback() {
+ public function testGetWithSetCallback( array $extOpts, $versioned ) {
$cache = $this->cache;
$key = wfRandomString();
};
$wasSet = 0;
- $v = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
+ $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
$this->assertEquals( $value, $v, "Value returned" );
$this->assertEquals( 1, $wasSet, "Value regenerated" );
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
$wasSet = 0;
- $v = $cache->getWithSetCallback( $key, 30, $func, array(
+ $v = $cache->getWithSetCallback( $key, 30, $func, [
'lowTTL' => 0,
'lockTSE' => 5,
- ) );
+ ] + $extOpts );
$this->assertEquals( $value, $v, "Value returned" );
$this->assertEquals( 0, $wasSet, "Value not regenerated" );
$priorTime = microtime( true );
usleep( 1 );
$wasSet = 0;
- $v = $cache->getWithSetCallback( $key, 30, $func,
- array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
+ $v = $cache->getWithSetCallback(
+ $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
+ );
$this->assertEquals( $value, $v, "Value returned" );
$this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$priorTime = microtime( true );
$wasSet = 0;
- $v = $cache->getWithSetCallback( $key, 30, $func,
- array( 'checkKeys' => array( $cKey1, $cKey2 ) ) );
+ $v = $cache->getWithSetCallback(
+ $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
+ );
$this->assertEquals( $value, $v, "Value returned" );
$this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
$curTTL = null;
- $v = $cache->get( $key, $curTTL, array( $cKey1, $cKey2 ) );
- $this->assertEquals( $value, $v, "Value returned" );
+ $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->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
$wasSet = 0;
$key = wfRandomString();
- $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
+ $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertEquals( $value, $v, "Value returned" );
$cache->delete( $key );
- $v = $cache->getWithSetCallback( $key, 30, $func, array( 'pcTTL' => 5 ) );
+ $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertEquals( $value, $v, "Value still returned after deleted" );
$this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
}
+ public static function getWithSetCallback_provider() {
+ return [
+ [ [], false ],
+ [ [ 'version' => 1 ], true ]
+ ];
+ }
+
/**
* @covers WANObjectCache::getWithSetCallback()
* @covers WANObjectCache::doGetWithSetCallback()
return $value;
};
- $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
+ $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
$this->assertEquals( $value, $ret );
$this->assertEquals( 1, $calls, 'Value was populated' );
// Acquire a lock to verify that getWithSetCallback uses lockTSE properly
$this->internalCache->lock( $key, 0 );
- $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
- $this->assertEquals( $value, $ret );
+
+ $checkKeys = [ wfRandomString() ]; // new check keys => force misses
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Old value used' );
$this->assertEquals( 1, $calls, 'Callback was not used' );
+
+ $cache->delete( $key );
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
+ $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
+
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
+ $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
}
/**
// Value should be marked as stale due to snapshot lag
$curTTL = null;
- $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
+ $ret = $cache->getWithSetCallback( $key, 30, $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' );
// Acquire a lock to verify that getWithSetCallback uses lockTSE properly
$this->internalCache->lock( $key, 0 );
- $ret = $cache->getWithSetCallback( $key, 30, $func, array( 'lockTSE' => 5 ) );
+ $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
$this->assertEquals( $value, $ret );
$this->assertEquals( 1, $calls, 'Callback was not used' );
}
+ /**
+ * @covers WANObjectCache::getWithSetCallback()
+ * @covers WANObjectCache::doGetWithSetCallback()
+ */
+ public function testBusyValue() {
+ $cache = $this->cache;
+ $key = wfRandomString();
+ $value = wfRandomString();
+ $busyValue = wfRandomString();
+
+ $calls = 0;
+ $func = function() use ( &$calls, $value ) {
+ ++$calls;
+ return $value;
+ };
+
+ $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
+ $this->assertEquals( $value, $ret );
+ $this->assertEquals( 1, $calls, 'Value was populated' );
+
+ // Acquire a lock to verify that getWithSetCallback uses busyValue properly
+ $this->internalCache->lock( $key, 0 );
+
+ $checkKeys = [ wfRandomString() ]; // new check keys => force misses
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Callback used' );
+ $this->assertEquals( 2, $calls, 'Callback used' );
+
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Old value used' );
+ $this->assertEquals( 2, $calls, 'Callback was not used' );
+
+ $cache->delete( $key ); // no value at all anymore and still locked
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
+ $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
+
+ $this->internalCache->unlock( $key );
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
+ $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
+
+ $this->internalCache->lock( $key, 0 );
+ $ret = $cache->getWithSetCallback( $key, 30, $func,
+ [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
+ $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
+ $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
+ }
+
/**
* @covers WANObjectCache::getMulti()
*/
public function testGetMulti() {
$cache = $this->cache;
- $value1 = array( 'this' => 'is', 'a' => 'test' );
- $value2 = array( 'this' => 'is', 'another' => 'test' );
+ $value1 = [ 'this' => 'is', 'a' => 'test' ];
+ $value2 = [ 'this' => 'is', 'another' => 'test' ];
$key1 = wfRandomString();
$key2 = wfRandomString();
$cache->set( $key1, $value1, 5 );
$cache->set( $key2, $value2, 10 );
- $curTTLs = array();
+ $curTTLs = [];
$this->assertEquals(
- array( $key1 => $value1, $key2 => $value2 ),
- $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs ),
+ [ $key1 => $value1, $key2 => $value2 ],
+ $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
'Result array populated'
);
$priorTime = microtime( true );
usleep( 1 );
- $curTTLs = array();
+ $curTTLs = [];
$this->assertEquals(
- array( $key1 => $value1, $key2 => $value2 ),
- $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
+ [ $key1 => $value1, $key2 => $value2 ],
+ $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
"Result array populated even with new check keys"
);
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
usleep( 1 );
- $curTTLs = array();
+ $curTTLs = [];
$this->assertEquals(
- array( $key1 => $value1, $key2 => $value2 ),
- $cache->getMulti( array( $key1, $key2, $key3 ), $curTTLs, array( $cKey1, $cKey2 ) ),
+ [ $key1 => $value1, $key2 => $value2 ],
+ $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
"Result array still populated even with new check keys"
);
$this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
// Fake initial check key to be set in the past. Otherwise we'd have to sleep for
// several seconds during the test to assert the behaviour.
- foreach ( array( $checkAll, $check1, $check2 ) as $checkKey ) {
+ foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
$cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
}
usleep( 100 );
$cache->set( 'key1', $value1, 10 );
$cache->set( 'key2', $value2, 10 );
- $curTTLs = array();
- $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
+ $curTTLs = [];
+ $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
'key1' => $check1,
$checkAll,
'key2' => $check2,
'key3' => $check3,
- ) );
+ ] );
$this->assertEquals(
- array( 'key1' => $value1, 'key2' => $value2 ),
+ [ 'key1' => $value1, 'key2' => $value2 ],
$result,
'Initial values'
);
$cache->touchCheckKey( $check1 );
- $curTTLs = array();
- $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
+ $curTTLs = [];
+ $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
'key1' => $check1,
$checkAll,
'key2' => $check2,
'key3' => $check3,
- ) );
+ ] );
$this->assertEquals(
- array( 'key1' => $value1, 'key2' => $value2 ),
+ [ 'key1' => $value1, 'key2' => $value2 ],
$result,
'key1 expired by check1, but value still provided'
);
$cache->touchCheckKey( $checkAll );
- $curTTLs = array();
- $result = $cache->getMulti( array( 'key1', 'key2', 'key3' ), $curTTLs, array(
+ $curTTLs = [];
+ $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
'key1' => $check1,
$checkAll,
'key2' => $check2,
'key3' => $check3,
- ) );
+ ] );
$this->assertEquals(
- array( 'key1' => $value1, 'key2' => $value2 ),
+ [ 'key1' => $value1, 'key2' => $value2 ],
$result,
'All keys expired by checkAll, but value still provided'
);
$key = wfRandomString();
$checkKey = wfRandomString();
// miss, set, hit
- $cache->get( $key, $curTTL, array( $checkKey ) );
+ $cache->get( $key, $curTTL, [ $checkKey ] );
$cache->set( $key, 'val', 10 );
$curTTL = null;
- $v = $cache->get( $key, $curTTL, array( $checkKey ) );
+ $v = $cache->get( $key, $curTTL, [ $checkKey ] );
$this->assertEquals( 'val', $v );
$this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
// set, hit
$cache->set( $key, 'val', 10 );
$curTTL = null;
- $v = $cache->get( $key, $curTTL, array( $checkKey ) );
+ $v = $cache->get( $key, $curTTL, [ $checkKey ] );
$this->assertEquals( 'val', $v );
$this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
}
+ /**
+ * @dataProvider getWithSetCallback_versions_provider
+ * @param array $extOpts
+ * @param $versioned
+ */
+ public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
+ $cache = $this->cache;
+
+ $key = wfRandomString();
+ $value = wfRandomString();
+
+ $wasSet = 0;
+ $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
+ ++$wasSet;
+ return $value;
+ };
+
+ // Set the main key (version N if versioned)
+ $wasSet = 0;
+ $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+ $this->assertEquals( $value, $v, "Value returned" );
+ $this->assertEquals( 1, $wasSet, "Value regenerated" );
+ $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+ $this->assertEquals( 1, $wasSet, "Value not regenerated" );
+ // Set the key for version N+1 (if versioned)
+ if ( $versioned ) {
+ $verOpts = [ 'version' => $extOpts['version'] + 1 ];
+
+ $wasSet = 0;
+ $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
+ $this->assertEquals( $value, $v, "Value returned" );
+ $this->assertEquals( 1, $wasSet, "Value regenerated" );
+
+ $wasSet = 0;
+ $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
+ $this->assertEquals( $value, $v, "Value returned" );
+ $this->assertEquals( 0, $wasSet, "Value not regenerated" );
+ }
+
+ $wasSet = 0;
+ $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+ $this->assertEquals( 0, $wasSet, "Value not regenerated" );
+
+ $wasSet = 0;
+ $cache->delete( $key );
+ $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
+ $this->assertEquals( $value, $v, "Value returned" );
+ $this->assertEquals( 1, $wasSet, "Value regenerated" );
+
+ if ( $versioned ) {
+ $wasSet = 0;
+ $verOpts = [ 'version' => $extOpts['version'] + 1 ];
+ $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
+ $this->assertEquals( $value, $v, "Value returned" );
+ $this->assertEquals( 1, $wasSet, "Value regenerated" );
+ }
+ }
+
+ public static function getWithSetCallback_versions_provider() {
+ return [
+ [ [], false ],
+ [ [ 'version' => 1 ], true ]
+ ];
+ }
+
/**
* @covers WANObjectCache::touchCheckKey()
* @covers WANObjectCache::resetCheckKey()
$this->cache->set( $key, $value, 30 );
$curTTL = null;
- $v = $this->cache->get( $key, $curTTL, array( $tKey1, $tKey2 ) );
+ $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
$this->assertEquals( $value, $v, "Value matches" );
$this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
$this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
$value = 1;
$key = wfRandomString();
- $opts = array( 'lag' => 300, 'since' => microtime( true ) );
+ $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
$this->cache->set( $key, $value, 30, $opts );
$this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
$key = wfRandomString();
- $opts = array( 'lag' => 0, 'since' => microtime( true ) - 300 );
+ $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
$this->cache->set( $key, $value, 30, $opts );
$this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
$key = wfRandomString();
- $opts = array( 'lag' => 5, 'since' => microtime( true ) - 5 );
+ $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
$this->cache->set( $key, $value, 30, $opts );
$this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
}
$value = 1;
$key = wfRandomString();
- $opts = array( 'pending' => true );
+ $opts = [ 'pending' => true ];
$this->cache->set( $key, $value, 30, $opts );
$this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
}