2015-01-27 19:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class WANObjectCacheTest extends MediaWikiTestCase {
|
|
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
private $cache;
|
2015-10-17 22:45:08 +00:00
|
|
|
/**@var BagOStuff */
|
|
|
|
|
private $internalCache;
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
|
|
|
|
|
$name = $this->getCliArg( 'use-wanobjectcache' );
|
|
|
|
|
|
|
|
|
|
$this->cache = ObjectCache::getWANInstance( $name );
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->cache = new WANObjectCache( [
|
2015-10-07 23:21:11 +00:00
|
|
|
'cache' => new HashBagOStuff(),
|
|
|
|
|
'pool' => 'testcache-hash',
|
2016-02-17 09:09:32 +00:00
|
|
|
'relayer' => new EventRelayerNull( [] )
|
|
|
|
|
] );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
2015-10-07 23:21:11 +00:00
|
|
|
|
|
|
|
|
$wanCache = TestingAccessWrapper::newFromObject( $this->cache );
|
|
|
|
|
$this->internalCache = $wanCache->cache;
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-16 21:48:47 +00:00
|
|
|
* @dataProvider provideSetAndGet
|
2015-01-27 19:56:44 +00:00
|
|
|
* @covers WANObjectCache::set()
|
|
|
|
|
* @covers WANObjectCache::get()
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param integer $ttl
|
|
|
|
|
*/
|
|
|
|
|
public function testSetAndGet( $value, $ttl ) {
|
2016-05-12 04:07:23 +00:00
|
|
|
$curTTL = null;
|
|
|
|
|
$asOf = null;
|
2015-01-27 19:56:44 +00:00
|
|
|
$key = wfRandomString();
|
2016-05-12 04:07:23 +00:00
|
|
|
|
|
|
|
|
$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 );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->cache->set( $key, $value, $ttl );
|
|
|
|
|
|
2016-05-12 04:07:23 +00:00
|
|
|
$this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
|
2015-01-27 19:56:44 +00:00
|
|
|
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" );
|
|
|
|
|
}
|
2016-05-12 04:07:23 +00:00
|
|
|
$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" );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:48:47 +00:00
|
|
|
public static function provideSetAndGet() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 14141, 3 ],
|
|
|
|
|
[ 3535.666, 3 ],
|
|
|
|
|
[ [], 3 ],
|
|
|
|
|
[ null, 3 ],
|
|
|
|
|
[ '0', 3 ],
|
|
|
|
|
[ (object)[ 'meow' ], 3 ],
|
|
|
|
|
[ INF, 3 ],
|
|
|
|
|
[ '', 3 ],
|
|
|
|
|
[ 'pizzacat', INF ],
|
|
|
|
|
];
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:48:47 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::get()
|
|
|
|
|
*/
|
2015-01-27 19:56:44 +00:00
|
|
|
public function testGetNotExists() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$curTTL = null;
|
|
|
|
|
$value = $this->cache->get( $key, $curTTL );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $value, "Non-existing key has false value" );
|
|
|
|
|
$this->assertNull( $curTTL, "Non-existing key has null current TTL" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:48:47 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::set()
|
|
|
|
|
*/
|
2015-01-27 19:56:44 +00:00
|
|
|
public function testSetOver() {
|
|
|
|
|
$key = wfRandomString();
|
2015-06-17 20:01:00 +00:00
|
|
|
for ( $i = 0; $i < 3; ++$i ) {
|
2015-01-27 19:56:44 +00:00
|
|
|
$value = wfRandomString();
|
2015-06-16 19:06:19 +00:00
|
|
|
$this->cache->set( $key, $value, 3 );
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $this->cache->get( $key ), $value );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:48:47 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::set()
|
|
|
|
|
*/
|
2015-09-30 01:14:48 +00:00
|
|
|
public function testStaleSet() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
|
2015-09-30 01:14:48 +00:00
|
|
|
|
|
|
|
|
$this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
2016-05-12 04:07:23 +00:00
|
|
|
* @dataProvider getWithSetCallback_provider
|
2015-01-27 19:56:44 +00:00
|
|
|
* @covers WANObjectCache::getWithSetCallback()
|
2015-11-16 21:48:47 +00:00
|
|
|
* @covers WANObjectCache::doGetWithSetCallback()
|
2016-05-12 04:07:23 +00:00
|
|
|
* @param array $extOpts
|
|
|
|
|
* @param bool $versioned
|
2015-01-27 19:56:44 +00:00
|
|
|
*/
|
2016-05-12 04:07:23 +00:00
|
|
|
public function testGetWithSetCallback( array $extOpts, $versioned ) {
|
2015-01-27 19:56:44 +00:00
|
|
|
$cache = $this->cache;
|
|
|
|
|
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
$cKey1 = wfRandomString();
|
|
|
|
|
$cKey2 = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$wasSet = 0;
|
2015-05-11 17:03:55 +00:00
|
|
|
$func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
|
|
|
|
|
++$wasSet;
|
|
|
|
|
$ttl = 20; // override with another value
|
|
|
|
|
return $value;
|
|
|
|
|
};
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
$wasSet = 0;
|
2016-05-12 04:07:23 +00:00
|
|
|
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
|
2015-10-07 08:55:39 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value returned" );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals( 1, $wasSet, "Value regenerated" );
|
|
|
|
|
|
2015-05-11 17:03:55 +00:00
|
|
|
$curTTL = null;
|
2015-10-07 08:55:39 +00:00
|
|
|
$cache->get( $key, $curTTL );
|
2015-05-11 17:03:55 +00:00
|
|
|
$this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
|
|
|
|
|
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
$wasSet = 0;
|
2016-02-17 09:09:32 +00:00
|
|
|
$v = $cache->getWithSetCallback( $key, 30, $func, [
|
2015-10-08 00:17:13 +00:00
|
|
|
'lowTTL' => 0,
|
|
|
|
|
'lockTSE' => 5,
|
2016-05-12 04:07:23 +00:00
|
|
|
] + $extOpts );
|
2015-10-07 08:55:39 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value returned" );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals( 0, $wasSet, "Value not regenerated" );
|
|
|
|
|
|
|
|
|
|
$priorTime = microtime( true );
|
|
|
|
|
usleep( 1 );
|
|
|
|
|
$wasSet = 0;
|
2016-05-12 04:07:23 +00:00
|
|
|
$v = $cache->getWithSetCallback(
|
|
|
|
|
$key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
|
|
|
|
|
);
|
2015-10-07 08:55:39 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value returned" );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
|
|
|
|
|
$t1 = $cache->getCheckKeyTime( $cKey1 );
|
|
|
|
|
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
|
|
|
|
|
$t2 = $cache->getCheckKeyTime( $cKey2 );
|
|
|
|
|
$this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
|
|
|
|
|
|
|
|
|
|
$priorTime = microtime( true );
|
|
|
|
|
$wasSet = 0;
|
2016-05-12 04:07:23 +00:00
|
|
|
$v = $cache->getWithSetCallback(
|
|
|
|
|
$key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
|
|
|
|
|
);
|
2015-10-07 08:55:39 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value returned" );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
|
|
|
|
|
$t1 = $cache->getCheckKeyTime( $cKey1 );
|
|
|
|
|
$this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
|
|
|
|
|
$t2 = $cache->getCheckKeyTime( $cKey2 );
|
|
|
|
|
$this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
|
|
|
|
|
|
|
|
|
|
$curTTL = null;
|
2016-02-17 09:09:32 +00:00
|
|
|
$v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
|
2016-05-12 04:07:23 +00:00
|
|
|
if ( $versioned ) {
|
|
|
|
|
$this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( $value, $v, "Value returned" );
|
|
|
|
|
}
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
|
2015-10-07 08:55:39 +00:00
|
|
|
|
|
|
|
|
$wasSet = 0;
|
|
|
|
|
$key = wfRandomString();
|
2016-05-12 04:07:23 +00:00
|
|
|
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
|
2015-10-07 08:55:39 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value returned" );
|
|
|
|
|
$cache->delete( $key );
|
2016-05-12 04:07:23 +00:00
|
|
|
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
|
2015-10-07 08:55:39 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value still returned after deleted" );
|
|
|
|
|
$this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 04:07:23 +00:00
|
|
|
public static function getWithSetCallback_provider() {
|
|
|
|
|
return [
|
|
|
|
|
[ [], false ],
|
|
|
|
|
[ [ 'version' => 1 ], true ]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 23:21:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::getWithSetCallback()
|
2015-11-16 21:48:47 +00:00
|
|
|
* @covers WANObjectCache::doGetWithSetCallback()
|
2015-10-07 23:21:11 +00:00
|
|
|
*/
|
|
|
|
|
public function testLockTSE() {
|
|
|
|
|
$cache = $this->cache;
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$calls = 0;
|
2016-08-12 02:27:50 +00:00
|
|
|
$func = function() use ( &$calls, $value, $cache, $key ) {
|
2015-10-07 23:21:11 +00:00
|
|
|
++$calls;
|
2016-08-12 02:27:50 +00:00
|
|
|
// Immediately kill any mutex rather than waiting a second
|
|
|
|
|
$cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
|
2015-10-07 23:21:11 +00:00
|
|
|
return $value;
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
|
2015-10-07 23:21:11 +00:00
|
|
|
$this->assertEquals( $value, $ret );
|
|
|
|
|
$this->assertEquals( 1, $calls, 'Value was populated' );
|
|
|
|
|
|
|
|
|
|
// Acquire a lock to verify that getWithSetCallback uses lockTSE properly
|
2016-08-12 02:27:50 +00:00
|
|
|
$this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
|
2016-06-15 16:40:34 +00:00
|
|
|
|
|
|
|
|
$checkKeys = [ wfRandomString() ]; // new check keys => force misses
|
|
|
|
|
$ret = $cache->getWithSetCallback( $key, 30, $func,
|
|
|
|
|
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
|
2016-07-22 05:15:21 +00:00
|
|
|
$this->assertEquals( $value, $ret, 'Old value used' );
|
2015-10-07 23:21:11 +00:00
|
|
|
$this->assertEquals( 1, $calls, 'Callback was not used' );
|
2016-06-15 16:40:34 +00:00
|
|
|
|
|
|
|
|
$cache->delete( $key );
|
|
|
|
|
$ret = $cache->getWithSetCallback( $key, 30, $func,
|
2016-07-22 05:15:21 +00:00
|
|
|
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
|
|
|
|
|
$this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
|
|
|
|
|
$this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
|
2016-06-15 16:40:34 +00:00
|
|
|
|
|
|
|
|
$ret = $cache->getWithSetCallback( $key, 30, $func,
|
|
|
|
|
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
|
2016-07-22 05:15:21 +00:00
|
|
|
$this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
|
2016-06-15 16:40:34 +00:00
|
|
|
$this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
|
2015-10-07 23:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-23 02:36:59 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::getWithSetCallback()
|
2015-11-16 21:48:47 +00:00
|
|
|
* @covers WANObjectCache::doGetWithSetCallback()
|
2015-10-23 02:36:59 +00:00
|
|
|
*/
|
|
|
|
|
public function testLockTSESlow() {
|
|
|
|
|
$cache = $this->cache;
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$calls = 0;
|
2016-08-12 02:27:50 +00:00
|
|
|
$func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
|
2015-10-23 02:36:59 +00:00
|
|
|
++$calls;
|
|
|
|
|
$setOpts['since'] = microtime( true ) - 10;
|
2016-08-12 02:27:50 +00:00
|
|
|
// Immediately kill any mutex rather than waiting a second
|
|
|
|
|
$cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
|
2015-10-23 02:36:59 +00:00
|
|
|
return $value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Value should be marked as stale due to snapshot lag
|
|
|
|
|
$curTTL = null;
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
|
2015-10-23 02:36:59 +00:00
|
|
|
$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, $calls, 'Value was generated' );
|
|
|
|
|
|
|
|
|
|
// Acquire a lock to verify that getWithSetCallback uses lockTSE properly
|
2016-08-12 02:27:50 +00:00
|
|
|
$this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
|
2015-10-23 02:36:59 +00:00
|
|
|
$this->assertEquals( $value, $ret );
|
|
|
|
|
$this->assertEquals( 1, $calls, 'Callback was not used' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-22 05:15:21 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::getWithSetCallback()
|
|
|
|
|
* @covers WANObjectCache::doGetWithSetCallback()
|
|
|
|
|
*/
|
|
|
|
|
public function testBusyValue() {
|
|
|
|
|
$cache = $this->cache;
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
$busyValue = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$calls = 0;
|
2016-08-12 02:27:50 +00:00
|
|
|
$func = function() use ( &$calls, $value, $cache, $key ) {
|
2016-07-22 05:15:21 +00:00
|
|
|
++$calls;
|
2016-08-12 02:27:50 +00:00
|
|
|
// Immediately kill any mutex rather than waiting a second
|
|
|
|
|
$cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
|
2016-07-22 05:15:21 +00:00
|
|
|
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
|
2016-08-12 02:27:50 +00:00
|
|
|
$this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
|
2016-07-22 05:15:21 +00:00
|
|
|
|
|
|
|
|
$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' );
|
|
|
|
|
|
2016-08-12 02:27:50 +00:00
|
|
|
$this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
|
2016-07-22 05:15:21 +00:00
|
|
|
$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' );
|
|
|
|
|
|
2016-08-12 02:27:50 +00:00
|
|
|
$this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
|
2016-07-22 05:15:21 +00:00
|
|
|
$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' );
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::getMulti()
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMulti() {
|
|
|
|
|
$cache = $this->cache;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$value1 = [ 'this' => 'is', 'a' => 'test' ];
|
|
|
|
|
$value2 = [ 'this' => 'is', 'another' => 'test' ];
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
$key1 = wfRandomString();
|
|
|
|
|
$key2 = wfRandomString();
|
|
|
|
|
$key3 = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$cache->set( $key1, $value1, 5 );
|
|
|
|
|
$cache->set( $key2, $value2, 10 );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$curTTLs = [];
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $key1 => $value1, $key2 => $value2 ],
|
|
|
|
|
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
|
2015-12-04 20:31:01 +00:00
|
|
|
'Result array populated'
|
2015-01-27 19:56:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
|
|
|
|
|
$this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
|
|
|
|
|
$this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
|
|
|
|
|
|
|
|
|
|
$cKey1 = wfRandomString();
|
|
|
|
|
$cKey2 = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$priorTime = microtime( true );
|
|
|
|
|
usleep( 1 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$curTTLs = [];
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $key1 => $value1, $key2 => $value2 ],
|
|
|
|
|
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
|
2015-01-27 19:56:44 +00:00
|
|
|
"Result array populated even with new check keys"
|
|
|
|
|
);
|
|
|
|
|
$t1 = $cache->getCheckKeyTime( $cKey1 );
|
|
|
|
|
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
|
|
|
|
|
$t2 = $cache->getCheckKeyTime( $cKey2 );
|
|
|
|
|
$this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
|
|
|
|
|
$this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
|
|
|
|
|
$this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
|
|
|
|
|
$this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
|
|
|
|
|
|
|
|
|
|
usleep( 1 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$curTTLs = [];
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $key1 => $value1, $key2 => $value2 ],
|
|
|
|
|
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
|
2015-01-27 19:56:44 +00:00
|
|
|
"Result array still populated even with new check keys"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
|
|
|
|
|
$this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
|
|
|
|
|
$this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:41:10 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::getMulti()
|
|
|
|
|
* @covers WANObjectCache::processCheckKeys()
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMultiCheckKeys() {
|
|
|
|
|
$cache = $this->cache;
|
|
|
|
|
|
|
|
|
|
$checkAll = wfRandomString();
|
|
|
|
|
$check1 = wfRandomString();
|
|
|
|
|
$check2 = wfRandomString();
|
|
|
|
|
$check3 = wfRandomString();
|
|
|
|
|
$value1 = wfRandomString();
|
|
|
|
|
$value2 = wfRandomString();
|
|
|
|
|
|
|
|
|
|
// 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.
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
|
2015-12-04 22:22:02 +00:00
|
|
|
$cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
|
2015-11-16 21:41:10 +00:00
|
|
|
}
|
2015-12-04 22:22:02 +00:00
|
|
|
usleep( 100 );
|
2015-11-16 21:41:10 +00:00
|
|
|
|
|
|
|
|
$cache->set( 'key1', $value1, 10 );
|
|
|
|
|
$cache->set( 'key2', $value2, 10 );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$curTTLs = [];
|
|
|
|
|
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
|
2015-11-16 21:41:10 +00:00
|
|
|
'key1' => $check1,
|
|
|
|
|
$checkAll,
|
|
|
|
|
'key2' => $check2,
|
|
|
|
|
'key3' => $check3,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-16 21:41:10 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'key1' => $value1, 'key2' => $value2 ],
|
2015-11-16 21:41:10 +00:00
|
|
|
$result,
|
|
|
|
|
'Initial values'
|
|
|
|
|
);
|
2015-12-04 22:22:02 +00:00
|
|
|
$this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
|
|
|
|
|
$this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
|
|
|
|
|
$this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
|
|
|
|
|
$this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
|
2015-11-16 21:41:10 +00:00
|
|
|
|
|
|
|
|
$cache->touchCheckKey( $check1 );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$curTTLs = [];
|
|
|
|
|
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
|
2015-11-16 21:41:10 +00:00
|
|
|
'key1' => $check1,
|
|
|
|
|
$checkAll,
|
|
|
|
|
'key2' => $check2,
|
|
|
|
|
'key3' => $check3,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-16 21:41:10 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'key1' => $value1, 'key2' => $value2 ],
|
2015-11-16 21:41:10 +00:00
|
|
|
$result,
|
|
|
|
|
'key1 expired by check1, but value still provided'
|
|
|
|
|
);
|
|
|
|
|
$this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
|
2015-12-04 22:22:02 +00:00
|
|
|
$this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
|
2015-11-16 21:41:10 +00:00
|
|
|
|
|
|
|
|
$cache->touchCheckKey( $checkAll );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$curTTLs = [];
|
|
|
|
|
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
|
2015-11-16 21:41:10 +00:00
|
|
|
'key1' => $check1,
|
|
|
|
|
$checkAll,
|
|
|
|
|
'key2' => $check2,
|
|
|
|
|
'key3' => $check3,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-16 21:41:10 +00:00
|
|
|
$this->assertEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'key1' => $value1, 'key2' => $value2 ],
|
2015-11-16 21:41:10 +00:00
|
|
|
$result,
|
|
|
|
|
'All keys expired by checkAll, but value still provided'
|
|
|
|
|
);
|
|
|
|
|
$this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
|
|
|
|
|
$this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 22:22:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::get()
|
|
|
|
|
* @covers WANObjectCache::processCheckKeys()
|
|
|
|
|
*/
|
|
|
|
|
public function testCheckKeyInitHoldoff() {
|
|
|
|
|
$cache = $this->cache;
|
|
|
|
|
|
|
|
|
|
for ( $i = 0; $i < 500; ++$i ) {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$checkKey = wfRandomString();
|
|
|
|
|
// miss, set, hit
|
2016-02-17 09:09:32 +00:00
|
|
|
$cache->get( $key, $curTTL, [ $checkKey ] );
|
2015-12-04 22:22:02 +00:00
|
|
|
$cache->set( $key, 'val', 10 );
|
|
|
|
|
$curTTL = null;
|
2016-02-17 09:09:32 +00:00
|
|
|
$v = $cache->get( $key, $curTTL, [ $checkKey ] );
|
2015-12-04 22:22:02 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'val', $v );
|
|
|
|
|
$this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ( $i = 0; $i < 500; ++$i ) {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$checkKey = wfRandomString();
|
|
|
|
|
// set, hit
|
|
|
|
|
$cache->set( $key, 'val', 10 );
|
|
|
|
|
$curTTL = null;
|
2016-02-17 09:09:32 +00:00
|
|
|
$v = $cache->get( $key, $curTTL, [ $checkKey ] );
|
2015-12-04 22:22:02 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'val', $v );
|
|
|
|
|
$this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::delete()
|
|
|
|
|
*/
|
|
|
|
|
public function testDelete() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
$this->cache->set( $key, $value );
|
|
|
|
|
|
|
|
|
|
$curTTL = null;
|
|
|
|
|
$v = $this->cache->get( $key, $curTTL );
|
|
|
|
|
$this->assertEquals( $value, $v, "Key was created with value" );
|
|
|
|
|
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
|
|
|
|
|
|
|
|
|
|
$this->cache->delete( $key );
|
|
|
|
|
|
|
|
|
|
$curTTL = null;
|
|
|
|
|
$v = $this->cache->get( $key, $curTTL );
|
|
|
|
|
$this->assertFalse( $v, "Deleted key has false value" );
|
|
|
|
|
$this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
|
|
|
|
|
|
|
|
|
|
$this->cache->set( $key, $value . 'more' );
|
2015-11-24 05:41:12 +00:00
|
|
|
$v = $this->cache->get( $key, $curTTL );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
|
|
|
|
|
$this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
|
2015-11-24 05:41:12 +00:00
|
|
|
|
|
|
|
|
$this->cache->set( $key, $value );
|
|
|
|
|
$this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
|
|
|
|
|
|
|
|
|
|
$curTTL = null;
|
|
|
|
|
$v = $this->cache->get( $key, $curTTL );
|
|
|
|
|
$this->assertFalse( $v, "Deleted key has false value" );
|
|
|
|
|
$this->assertNull( $curTTL, "Deleted key has null current TTL" );
|
|
|
|
|
|
|
|
|
|
$this->cache->set( $key, $value );
|
|
|
|
|
$v = $this->cache->get( $key, $curTTL );
|
|
|
|
|
$this->assertEquals( $value, $v, "Key was created with value" );
|
|
|
|
|
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-12 04:07:23 +00:00
|
|
|
/**
|
|
|
|
|
* @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 ]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::touchCheckKey()
|
2015-05-28 17:04:19 +00:00
|
|
|
* @covers WANObjectCache::resetCheckKey()
|
2015-01-27 19:56:44 +00:00
|
|
|
* @covers WANObjectCache::getCheckKeyTime()
|
|
|
|
|
*/
|
|
|
|
|
public function testTouchKeys() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
|
2015-05-22 05:46:25 +00:00
|
|
|
$priorTime = microtime( true );
|
2015-10-08 00:17:13 +00:00
|
|
|
usleep( 100 );
|
2015-01-27 19:56:44 +00:00
|
|
|
$t0 = $this->cache->getCheckKeyTime( $key );
|
2015-05-22 05:46:25 +00:00
|
|
|
$this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
$priorTime = microtime( true );
|
2015-10-08 00:17:13 +00:00
|
|
|
usleep( 100 );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->cache->touchCheckKey( $key );
|
|
|
|
|
$t1 = $this->cache->getCheckKeyTime( $key );
|
|
|
|
|
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
|
|
|
|
|
|
|
|
|
|
$t2 = $this->cache->getCheckKeyTime( $key );
|
|
|
|
|
$this->assertEquals( $t1, $t2, 'Check key time did not change' );
|
|
|
|
|
|
2015-10-08 00:17:13 +00:00
|
|
|
usleep( 100 );
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->cache->touchCheckKey( $key );
|
|
|
|
|
$t3 = $this->cache->getCheckKeyTime( $key );
|
|
|
|
|
$this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
|
|
|
|
|
|
|
|
|
|
$t4 = $this->cache->getCheckKeyTime( $key );
|
|
|
|
|
$this->assertEquals( $t3, $t4, 'Check key time did not change' );
|
2015-05-28 17:04:19 +00:00
|
|
|
|
2015-10-08 00:17:13 +00:00
|
|
|
usleep( 100 );
|
2015-05-28 17:04:19 +00:00
|
|
|
$this->cache->resetCheckKey( $key );
|
|
|
|
|
$t5 = $this->cache->getCheckKeyTime( $key );
|
|
|
|
|
$this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
|
|
|
|
|
|
|
|
|
|
$t6 = $this->cache->getCheckKeyTime( $key );
|
|
|
|
|
$this->assertEquals( $t5, $t6, 'Check key time did not change' );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
2015-10-25 20:42:21 +00:00
|
|
|
|
2015-11-17 00:13:37 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::getMulti()
|
|
|
|
|
*/
|
|
|
|
|
public function testGetWithSeveralCheckKeys() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$tKey1 = wfRandomString();
|
|
|
|
|
$tKey2 = wfRandomString();
|
|
|
|
|
$value = 'meow';
|
|
|
|
|
|
|
|
|
|
// 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 )
|
|
|
|
|
);
|
|
|
|
|
$this->internalCache->set(
|
|
|
|
|
WANObjectCache::TIME_KEY_PREFIX . $tKey2,
|
|
|
|
|
WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
|
|
|
|
|
);
|
|
|
|
|
$this->internalCache->set(
|
|
|
|
|
WANObjectCache::TIME_KEY_PREFIX . $tKey1,
|
|
|
|
|
WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
|
|
|
|
|
);
|
|
|
|
|
$this->cache->set( $key, $value, 30 );
|
|
|
|
|
|
|
|
|
|
$curTTL = null;
|
2016-02-17 09:09:32 +00:00
|
|
|
$v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
|
2015-11-17 00:13:37 +00:00
|
|
|
$this->assertEquals( $value, $v, "Value matches" );
|
2015-11-20 22:21:31 +00:00
|
|
|
$this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
|
2015-11-17 00:13:37 +00:00
|
|
|
$this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:48:47 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::set()
|
|
|
|
|
*/
|
2015-10-25 20:42:21 +00:00
|
|
|
public function testSetWithLag() {
|
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
|
|
$key = wfRandomString();
|
2016-02-17 09:09:32 +00:00
|
|
|
$opts = [ 'lag' => 300, 'since' => microtime( true ) ];
|
2015-10-25 20:42:21 +00:00
|
|
|
$this->cache->set( $key, $value, 30, $opts );
|
|
|
|
|
$this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
|
|
|
|
|
|
|
|
|
|
$key = wfRandomString();
|
2016-02-17 09:09:32 +00:00
|
|
|
$opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
|
2015-10-25 20:42:21 +00:00
|
|
|
$this->cache->set( $key, $value, 30, $opts );
|
|
|
|
|
$this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
|
|
|
|
|
|
|
|
|
|
$key = wfRandomString();
|
2016-02-17 09:09:32 +00:00
|
|
|
$opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
|
2015-10-25 20:42:21 +00:00
|
|
|
$this->cache->set( $key, $value, 30, $opts );
|
|
|
|
|
$this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:48:47 +00:00
|
|
|
/**
|
|
|
|
|
* @covers WANObjectCache::set()
|
|
|
|
|
*/
|
2015-10-25 20:42:21 +00:00
|
|
|
public function testWritePending() {
|
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
|
|
$key = wfRandomString();
|
2016-02-17 09:09:32 +00:00
|
|
|
$opts = [ 'pending' => true ];
|
2015-10-25 20:42:21 +00:00
|
|
|
$this->cache->set( $key, $value, 30, $opts );
|
|
|
|
|
$this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
|
|
|
|
|
}
|
2016-08-12 02:27:50 +00:00
|
|
|
|
|
|
|
|
public function testMcRouterSupport() {
|
|
|
|
|
$localBag = $this->getMock( 'EmptyBagOStuff', [ 'set', 'delete' ] );
|
|
|
|
|
$localBag->expects( $this->never() )->method( 'set' );
|
|
|
|
|
$localBag->expects( $this->never() )->method( 'delete' );
|
|
|
|
|
$wanCache = new WANObjectCache( [
|
|
|
|
|
'cache' => $localBag,
|
|
|
|
|
'pool' => 'testcache-hash',
|
|
|
|
|
'relayer' => new EventRelayerNull( [] )
|
|
|
|
|
] );
|
|
|
|
|
$valFunc = function () {
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// None of these should use broadcasting commands (e.g. SET, DELETE)
|
|
|
|
|
$wanCache->get( 'x' );
|
|
|
|
|
$wanCache->get( 'x', $ctl, [ 'check1' ] );
|
|
|
|
|
$wanCache->getMulti( [ 'x', 'y' ] );
|
|
|
|
|
$wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
|
|
|
|
|
$wanCache->getWithSetCallback( 'p', 30, $valFunc );
|
|
|
|
|
$wanCache->getCheckKeyTime( 'zzz' );
|
|
|
|
|
}
|
2015-06-16 13:19:50 +00:00
|
|
|
}
|