X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Flibs%2Fobjectcache%2FWANObjectCacheTest.php;h=aa46c966ad4622f31d7429d651bd0f904002eca0;hb=43ff2a83b53e82193174fa78d9f300be6f5c79c7;hp=99b959b3e2e4bfee5dad6bc9b801c508f648a3d1;hpb=382b485b63110c11b8302b08c04b05c8afec4ff9;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php index 99b959b3e2..aa46c966ad 100644 --- a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php +++ b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php @@ -1,6 +1,6 @@ getCliArg( 'use-wanobjectcache' ) ) { - $name = $this->getCliArg( 'use-wanobjectcache' ); - - $this->cache = ObjectCache::getWANInstance( $name ); - } else { - $this->cache = new WANObjectCache( [ - 'cache' => new HashBagOStuff(), - 'pool' => 'testcache-hash', - 'relayer' => new EventRelayerNull( [] ) - ] ); - } + $this->cache = new WANObjectCache( [ + 'cache' => new HashBagOStuff(), + 'pool' => 'testcache-hash', + 'relayer' => new EventRelayerNull( [] ) + ] ); $wanCache = TestingAccessWrapper::newFromObject( $this->cache ); + /** @noinspection PhpUndefinedFieldInspection */ $this->internalCache = $wanCache->cache; } @@ -29,13 +24,14 @@ class WANObjectCacheTest extends MediaWikiTestCase { * @dataProvider provideSetAndGet * @covers WANObjectCache::set() * @covers WANObjectCache::get() + * @covers WANObjectCache::makeKey() * @param mixed $value * @param integer $ttl */ public function testSetAndGet( $value, $ttl ) { $curTTL = null; $asOf = null; - $key = wfRandomString(); + $key = $this->cache->makeKey( 'x', wfRandomString() ); $this->cache->get( $key, $curTTL, [], $asOf ); $this->assertNull( $curTTL, "Current TTL is null" ); @@ -71,9 +67,10 @@ class WANObjectCacheTest extends MediaWikiTestCase { /** * @covers WANObjectCache::get() + * @covers WANObjectCache::makeGlobalKey() */ public function testGetNotExists() { - $key = wfRandomString(); + $key = $this->cache->makeGlobalKey( 'y', wfRandomString(), 'p' ); $curTTL = null; $value = $this->cache->get( $key, $curTTL ); @@ -144,6 +141,19 @@ class WANObjectCacheTest extends MediaWikiTestCase { $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] ); } $this->assertEquals( 9, $hit, "Values evicted" ); + + $key = reset( $keys ); + // Get into cache + $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); + $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); + $this->assertEquals( 10, $hit, "Value cached" ); + $outerCallback = function () use ( &$callback, $key ) { + $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); + + return 43 + $v; + }; + $this->cache->getWithSetCallback( $key, 100, $outerCallback ); + $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" ); } /** @@ -165,7 +175,7 @@ class WANObjectCacheTest extends MediaWikiTestCase { $priorAsOf = null; $wasSet = 0; $func = function( $old, &$ttl, &$opts, $asOf ) - use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) + use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) { ++$wasSet; $priorValue = $old; @@ -188,9 +198,9 @@ class WANObjectCacheTest extends MediaWikiTestCase { $wasSet = 0; $v = $cache->getWithSetCallback( $key, 30, $func, [ - 'lowTTL' => 0, - 'lockTSE' => 5, - ] + $extOpts ); + 'lowTTL' => 0, + 'lockTSE' => 5, + ] + $extOpts ); $this->assertEquals( $value, $v, "Value returned" ); $this->assertEquals( 0, $wasSet, "Value not regenerated" ); @@ -203,7 +213,7 @@ class WANObjectCacheTest extends MediaWikiTestCase { $this->assertEquals( $value, $v, "Value returned" ); $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" ); $this->assertEquals( $value, $priorValue, "Has prior value" ); - $this->assertType( 'float', $priorAsOf, "Has prior value" ); + $this->assertInternalType( 'float', $priorAsOf, "Has prior value" ); $t1 = $cache->getCheckKeyTime( $cKey1 ); $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' ); $t2 = $cache->getCheckKeyTime( $cKey2 ); @@ -247,6 +257,150 @@ class WANObjectCacheTest extends MediaWikiTestCase { ]; } + /** + * @dataProvider getMultiWithSetCallback_provider + * @covers WANObjectCache::getMultiWithSetCallback() + * @covers WANObjectCache::makeMultiKeys() + * @param array $extOpts + * @param bool $versioned + */ + public function testGetMultiWithSetCallback( array $extOpts, $versioned ) { + $cache = $this->cache; + + $keyA = wfRandomString(); + $keyB = wfRandomString(); + $keyC = wfRandomString(); + $cKey1 = wfRandomString(); + $cKey2 = wfRandomString(); + + $priorValue = null; + $priorAsOf = null; + $wasSet = 0; + $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use ( + &$wasSet, &$priorValue, &$priorAsOf + ) { + ++$wasSet; + $priorValue = $old; + $priorAsOf = $asOf; + $ttl = 20; // override with another value + return "@$id$"; + }; + + $wasSet = 0; + $keyedIds = new ArrayIterator( [ $keyA => 3353 ] ); + $value = "@3353$"; + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts ); + $this->assertEquals( $value, $v[$keyA], "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value regenerated" ); + $this->assertFalse( $priorValue, "No prior value" ); + $this->assertNull( $priorAsOf, "No prior value" ); + + $curTTL = null; + $cache->get( $keyA, $curTTL ); + $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' ); + $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' ); + + $wasSet = 0; + $value = "@efef$"; + $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] ); + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts ); + $this->assertEquals( $value, $v[$keyB], "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value regenerated" ); + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts ); + $this->assertEquals( $value, $v[$keyB], "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value not regenerated" ); + + $priorTime = microtime( true ); + usleep( 1 ); + $wasSet = 0; + $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] ); + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts + ); + $this->assertEquals( $value, $v[$keyB], "Value returned" ); + $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" ); + $this->assertEquals( $value, $priorValue, "Has prior value" ); + $this->assertInternalType( 'float', $priorAsOf, "Has prior value" ); + $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 ); + $value = "@43636$"; + $wasSet = 0; + $keyedIds = new ArrayIterator( [ $keyC => 43636 ] ); + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts + ); + $this->assertEquals( $value, $v[$keyC], "Value returned" ); + $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; + $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->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" ); + + $wasSet = 0; + $key = wfRandomString(); + $keyedIds = new ArrayIterator( [ $key => 242424 ] ); + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts ); + $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" ); + $cache->delete( $key ); + $keyedIds = new ArrayIterator( [ $key => 242424 ] ); + $v = $cache->getMultiWithSetCallback( + $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts ); + $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" ); + $this->assertEquals( 1, $wasSet, "Value process cached while deleted" ); + + $calls = 0; + $ids = [ 1, 2, 3, 4, 5, 6 ]; + $keyFunc = function ( $id, WANObjectCache $wanCache ) { + return $wanCache->makeKey( 'test', $id ); + }; + $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc ); + $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) { + ++$calls; + + return "val-{$id}"; + }; + $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc ); + + $this->assertEquals( + [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ], + array_values( $values ), + "Correct values in correct order" + ); + $this->assertEquals( + array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ), + array_keys( $values ), + "Correct keys in correct order" + ); + $this->assertEquals( count( $ids ), $calls ); + + $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc ); + $this->assertEquals( count( $ids ), $calls, "Values cached" ); + } + + public static function getMultiWithSetCallback_provider() { + return [ + [ [], false ], + [ [ 'version' => 1 ], true ] + ]; + } + /** * @covers WANObjectCache::getWithSetCallback() * @covers WANObjectCache::doGetWithSetCallback() @@ -777,9 +931,14 @@ class WANObjectCacheTest extends MediaWikiTestCase { /** * @dataProvider provideAdaptiveTTL * @covers WANObjectCache::adaptiveTTL() + * @param float|int $ago + * @param int $maxTTL + * @param int $minTTL + * @param float $factor + * @param int $adaptiveTTL */ public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) { - $mtime = is_int( $ago ) ? time() - $ago : $ago; + $mtime = $ago ? time() - $ago : $ago; $margin = 5; $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );