X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Flibs%2Fobjectcache%2FWANObjectCacheTest.php;h=7c4c9bf944e7b2a6952e78b0e252ad6e2d7a034b;hp=bff45b575b57d6cd430fd4b4306ad5eccb39f5c5;hb=095c50ade11ccea3b0edc8956ad45dea46d4c18b;hpb=460e68ab3bd1743c3a494fe45ad3b76c66020ce3 diff --git a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php index bff45b575b..7c4c9bf944 100644 --- a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php +++ b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php @@ -318,7 +318,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts ); $this->assertEquals( $value, $v, "Value returned" ); - $this->assertEquals( 0, $wasSet, "Value not regenerated" ); + $this->assertSame( 0, $wasSet, "Value not regenerated" ); $mockWallClock += 1; @@ -584,7 +584,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $asycList[0](); // run the refresh callback $asycList = []; $this->assertEquals( 2, $wasSet, "Value calculated at later time" ); - $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." ); + $this->assertSame( 0, count( $asycList ), "No deferred refreshes added." ); $v = $cache->getWithSetCallback( $key, 300, $func, $opts ); $this->assertEquals( $value, $v, "New value stored" ); @@ -678,13 +678,13 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts ); $this->assertEquals( $value, $v[$keyB], "Value returned" ); $this->assertEquals( 1, $wasSet, "Value regenerated" ); - $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); + $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); $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" ); - $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); + $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); $mockWallClock += 1; @@ -767,8 +767,8 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $localBag = $this->getMockBuilder( HashBagOStuff::class ) ->setMethods( [ 'getMulti' ] )->getMock(); $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [ - WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1', - WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2' + 'WANCache:v:' . 'k1' => 'val-id1', + 'WANCache:v:' . 'k2' => 'val-id2' ] ); $wanCache = new WANObjectCache( [ 'cache' => $localBag ] ); @@ -846,13 +846,13 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts ); $this->assertEquals( $value, $v[$keyB], "Value returned" ); $this->assertEquals( 1, $wasSet, "Value regenerated" ); - $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); + $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); $v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts ); $this->assertEquals( $value, $v[$keyB], "Value returned" ); $this->assertEquals( 1, $wasSet, "Value not regenerated" ); - $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); + $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); $mockWallClock += 1; @@ -964,7 +964,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $this->assertEquals( 1, $calls, 'Value was populated' ); // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly - $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 ); + $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); $checkKeys = [ wfRandomString() ]; // new check keys => force misses $ret = $cache->getWithSetCallback( $key, 30, $func, @@ -1026,7 +1026,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $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 ); + $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] ); $this->assertEquals( $value, $ret ); @@ -1034,7 +1034,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $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 ); + $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] ); $this->assertEquals( $value, $ret ); @@ -1070,7 +1070,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { * @covers WANObjectCache::getWithSetCallback() * @covers WANObjectCache::fetchOrRegenerate() */ - public function testBusyValue() { + public function testBusyValueBasic() { $cache = $this->cache; $key = wfRandomString(); $value = wfRandomString(); @@ -1080,7 +1080,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $cache->setMockTime( $mockWallClock ); $calls = 0; - $func = function () use ( &$calls, $value, $cache, $key ) { + $func = function () use ( &$calls, $value ) { ++$calls; return $value; }; @@ -1092,7 +1092,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $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->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); $checkKeys = [ wfRandomString() ]; // new check keys => force misses $ret = $cache->getWithSetCallback( $key, 30, $func, @@ -1111,20 +1111,66 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' ); $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' ); - $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key ); + $this->internalCache->delete( 'WANCache:m:' . $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' ); $this->assertEquals( 3, $calls, 'Callback was used; saved interim' ); - $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 ); + $this->internalCache->add( 'WANCache:m:' . $key, 1, 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' ); } + public function getBusyValues_Provider() { + $hash = new HashBagOStuff( [] ); + + return [ + [ + function () { + return "Saint Oliver Plunckett"; + }, + 'Saint Oliver Plunckett' + ], + [ 'strlen', 'strlen' ], + [ 'WANObjectCache::newEmpty', 'WANObjectCache::newEmpty' ], + [ [ 'WANObjectCache', 'newEmpty' ], [ 'WANObjectCache', 'newEmpty' ] ], + [ [ $hash, 'getLastError' ], [ $hash, 'getLastError' ] ], + [ [ 1, 2, 3 ], [ 1, 2, 3 ] ] + ]; + } + + /** + * @covers WANObjectCache::getWithSetCallback() + * @covers WANObjectCache::fetchOrRegenerate() + * @dataProvider getBusyValues_Provider + * @param mixed $busyValue + * @param mixed $expected + */ + public function testBusyValueTypes( $busyValue, $expected ) { + $cache = $this->cache; + $key = wfRandomString(); + + $mockWallClock = 1549343530.2053; + $cache->setMockTime( $mockWallClock ); + + $calls = 0; + $func = function () use ( &$calls ) { + ++$calls; + return 418; + }; + + // Acquire a lock to verify that getWithSetCallback uses busyValue properly + $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); + + $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] ); + $this->assertSame( $expected, $ret, 'busyValue used as expected' ); + $this->assertSame( 0, $calls, 'busyValue was used' ); + } + /** * @covers WANObjectCache::getMulti() */ @@ -1208,7 +1254,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { // 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 ( [ $checkAll, $check1, $check2 ] as $checkKey ) { - $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE ); + $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_TTL_NONE ); } $mockWallClock += 0.100; @@ -1302,6 +1348,35 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { } } + /** + * @covers WANObjectCache::get() + * @covers WANObjectCache::processCheckKeys() + */ + public function testCheckKeyHoldoff() { + $cache = $this->cache; + $key = wfRandomString(); + $checkKey = wfRandomString(); + + $mockWallClock = 1549343530.2053; + $cache->setMockTime( $mockWallClock ); + $cache->touchCheckKey( $checkKey, 8 ); + + $mockWallClock += 1; + $cache->set( $key, 1, 60 ); + $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) ); + $this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" ); + + $mockWallClock += 3; + $cache->set( $key, 1, 60 ); + $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) ); + $this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" ); + + $mockWallClock += 10; + $cache->set( $key, 1, 60 ); + $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) ); + $this->assertGreaterThan( 0, $curTTL, "Key not in hold-off due to check key" ); + } + /** * @covers WANObjectCache::delete * @covers WANObjectCache::relayDelete @@ -1330,7 +1405,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" ); $this->cache->set( $key, $value ); - $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE ); + $this->cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE ); $curTTL = null; $v = $this->cache->get( $key, $curTTL ); @@ -1403,7 +1478,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $wasSet = 0; $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts ); $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" ); - $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" ); + $this->assertSame( 0, $wasSet, "Value not regenerated (secondary key)" ); // Clear out the older or unversioned key $cache->delete( $key, 0 ); @@ -1462,10 +1537,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $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 ); + $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); $v = $cache->getWithSetCallback( $key, 60, $func ); $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' ); - $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key ); + $this->internalCache->delete( 'WANCache:m:' . $key ); $cache->useInterimHoldOffCaching( false ); @@ -1482,7 +1557,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $v = $cache->getWithSetCallback( $key, 60, $func ); $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' ); // Lock up the mutex so interim cache is used - $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 ); + $this->internalCache->add( 'WANCache:m:' . $key, 1, 0 ); $v = $cache->getWithSetCallback( $key, 60, $func ); $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' ); } @@ -1548,16 +1623,16 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { // 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 . ( $priorTime - 3 ) + 'WANCache:t:' . $tKey2, + 'PURGED:' . ( $priorTime - 3 ) ); $this->internalCache->set( - WANObjectCache::TIME_KEY_PREFIX . $tKey2, - WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 5 ) + 'WANCache:t:' . $tKey2, + 'PURGED:' . ( $priorTime - 5 ) ); $this->internalCache->set( - WANObjectCache::TIME_KEY_PREFIX . $tKey1, - WANObjectCache::PURGE_VAL_PREFIX . ( $priorTime - 30 ) + 'WANCache:t:' . $tKey1, + 'PURGED:' . ( $priorTime - 30 ) ); $this->cache->set( $key, $value, 30 ); @@ -1584,30 +1659,30 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $badTime = microtime( true ) - 300; $this->internalCache->set( - WANObjectCache::VALUE_KEY_PREFIX . $vKey1, + 'WANCache:v:' . $vKey1, [ - WANObjectCache::FLD_FORMAT_VERSION => WANObjectCache::VERSION, - WANObjectCache::FLD_VALUE => $value, - WANObjectCache::FLD_TTL => 3600, - WANObjectCache::FLD_TIME => $goodTime + 0 => 1, + 1 => $value, + 2 => 3600, + 3 => $goodTime ] ); $this->internalCache->set( - WANObjectCache::VALUE_KEY_PREFIX . $vKey2, + 'WANCache:v:' . $vKey2, [ - WANObjectCache::FLD_FORMAT_VERSION => WANObjectCache::VERSION, - WANObjectCache::FLD_VALUE => $value, - WANObjectCache::FLD_TTL => 3600, - WANObjectCache::FLD_TIME => $badTime + 0 => 1, + 1 => $value, + 2 => 3600, + 3 => $badTime ] ); $this->internalCache->set( - WANObjectCache::TIME_KEY_PREFIX . $tKey1, - WANObjectCache::PURGE_VAL_PREFIX . $goodTime + 'WANCache:t:' . $tKey1, + 'PURGED:' . $goodTime ); $this->internalCache->set( - WANObjectCache::TIME_KEY_PREFIX . $tKey2, - WANObjectCache::PURGE_VAL_PREFIX . $badTime + 'WANCache:t:' . $tKey2, + 'PURGED:' . $badTime ); $this->assertEquals( $value, $this->cache->get( $vKey1 ) ); @@ -1632,10 +1707,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { ->setMethods( [ 'get', 'changeTTL' ] )->getMock(); $backend->expects( $this->once() )->method( 'get' ) ->willReturn( [ - WANObjectCache::FLD_FORMAT_VERSION => WANObjectCache::VERSION, - WANObjectCache::FLD_VALUE => 'value', - WANObjectCache::FLD_TTL => 3600, - WANObjectCache::FLD_TIME => 300, + 0 => 1, + 1 => 'value', + 2 => 3600, + 3 => 300, ] ); $backend->expects( $this->once() )->method( 'changeTTL' ) ->willReturn( false ); @@ -1721,7 +1796,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { ] ); $localBag->expects( $this->once() )->method( 'set' ) - ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX . "test" ); + ->with( "/*/mw-wan/" . 'WANCache:v:' . "test" ); $wanCache->delete( 'test' ); } @@ -1737,7 +1812,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { ] ); $localBag->expects( $this->once() )->method( 'set' ) - ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" ); + ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" ); $wanCache->touchCheckKey( 'test' ); } @@ -1753,7 +1828,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { ] ); $localBag->expects( $this->once() )->method( 'delete' ) - ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" ); + ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" ); $wanCache->resetCheckKey( 'test' ); } @@ -1906,6 +1981,8 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { [ 'domain:page:5', 'page' ], [ 'domain:main-key', 'main-key' ], [ 'domain:page:history', 'page' ], + // Regression test for T232907 + [ 'domain:foo-bar-1.2:abc:v2', 'foo-bar-1_2' ], [ 'missingdomainkey', 'missingdomainkey' ] ]; }