Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 76bfea2..076bf52 100644 (file)
@@ -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;
                };
@@ -1125,6 +1125,52 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $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()
         */
@@ -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
@@ -1865,6 +1940,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
         * @covers WANObjectCache::makeKey
         */
        public function testMakeKey() {
+               if ( defined( 'HHVM_VERSION' ) ) {
+                       $this->markTestSkipped( 'HHVM Reflection buggy' );
+               }
+
                $backend = $this->getMockBuilder( HashBagOStuff::class )
                        ->setMethods( [ 'makeKey' ] )->getMock();
                $backend->expects( $this->once() )->method( 'makeKey' )
@@ -1881,6 +1960,10 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
         * @covers WANObjectCache::makeGlobalKey
         */
        public function testMakeGlobalKey() {
+               if ( defined( 'HHVM_VERSION' ) ) {
+                       $this->markTestSkipped( 'HHVM Reflection buggy' );
+               }
+
                $backend = $this->getMockBuilder( HashBagOStuff::class )
                        ->setMethods( [ 'makeGlobalKey' ] )->getMock();
                $backend->expects( $this->once() )->method( 'makeGlobalKey' )
@@ -1898,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' ]
                ];
        }