Merge "API: Fix 'user_id' field of ApiCSPReport"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
index 4a09a2e..522af43 100644 (file)
@@ -1,10 +1,12 @@
 <?php
 
 use Wikimedia\ScopedCallback;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * @author Matthias Mullie <mmullie@wikimedia.org>
  * @group BagOStuff
+ * @covers BagOStuff
  */
 class BagOStuffTest extends MediaWikiTestCase {
        /** @var BagOStuff */
@@ -30,8 +32,8 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::makeGlobalKey
-        * @covers BagOStuff::makeKeyInternal
+        * @covers MediumSpecificBagOStuff::makeGlobalKey
+        * @covers MediumSpecificBagOStuff::makeKeyInternal
         */
        public function testMakeKey() {
                $cache = ObjectCache::newFromId( 'hash' );
@@ -64,8 +66,8 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::merge
-        * @covers BagOStuff::mergeViaCas
+        * @covers MediumSpecificBagOStuff::merge
+        * @covers MediumSpecificBagOStuff::mergeViaCas
         */
        public function testMerge() {
                $key = $this->cache->makeKey( self::TEST_KEY );
@@ -98,39 +100,101 @@ class BagOStuffTest extends MediaWikiTestCase {
                        $this->cache->merge( $key, $callback, 5, 1 ),
                        'Non-blocking merge (CAS)'
                );
+
                if ( $this->cache instanceof MultiWriteBagOStuff ) {
-                       $wrapper = \Wikimedia\TestingAccessWrapper::newFromObject( $this->cache );
-                       $n = count( $wrapper->caches );
+                       $wrapper = TestingAccessWrapper::newFromObject( $this->cache );
+                       $this->assertEquals( count( $wrapper->caches ), $calls );
                } else {
-                       $n = 1;
+                       $this->assertEquals( 1, $calls );
                }
-               $this->assertEquals( $n, $calls );
        }
 
        /**
-        * @covers BagOStuff::changeTTL
+        * @covers MediumSpecificBagOStuff::changeTTL
         */
        public function testChangeTTL() {
                $key = $this->cache->makeKey( self::TEST_KEY );
                $value = 'meow';
 
                $this->cache->add( $key, $value, 5 );
-               $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
+               $this->assertEquals( $value, $this->cache->get( $key ) );
+               $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
+               $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
+               $this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
                $this->assertEquals( $this->cache->get( $key ), $value );
                $this->cache->delete( $key );
-               $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
+               $this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
+
+               $this->cache->add( $key, $value, 5 );
+               $this->assertTrue( $this->cache->changeTTL( $key, time() - 3600 ) );
+               $this->assertFalse( $this->cache->get( $key ) );
        }
 
        /**
-        * @covers BagOStuff::add
+        * @covers MediumSpecificBagOStuff::changeTTLMulti
+        */
+       public function testChangeTTLMulti() {
+               $key1 = $this->cache->makeKey( 'test-key1' );
+               $key2 = $this->cache->makeKey( 'test-key2' );
+               $key3 = $this->cache->makeKey( 'test-key3' );
+               $key4 = $this->cache->makeKey( 'test-key4' );
+
+               // cleanup
+               $this->cache->delete( $key1 );
+               $this->cache->delete( $key2 );
+               $this->cache->delete( $key3 );
+               $this->cache->delete( $key4 );
+
+               $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 30 );
+               $this->assertFalse( $ok, "No keys found" );
+               $this->assertFalse( $this->cache->get( $key1 ) );
+               $this->assertFalse( $this->cache->get( $key2 ) );
+               $this->assertFalse( $this->cache->get( $key3 ) );
+
+               $ok = $this->cache->setMulti( [ $key1 => 1, $key2 => 2, $key3 => 3 ] );
+
+               $this->assertTrue( $ok, "setMulti() succeeded" );
+               $this->assertEquals(
+                       3,
+                       count( $this->cache->getMulti( [ $key1, $key2, $key3 ] ) ),
+                       "setMulti() succeeded via getMulti() check"
+               );
+
+               $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], 300 );
+               $this->assertTrue( $ok, "TTL bumped for all keys" );
+               $this->assertEquals( 1, $this->cache->get( $key1 ) );
+               $this->assertEquals( 2, $this->cache->get( $key2 ) );
+               $this->assertEquals( 3, $this->cache->get( $key3 ) );
+
+               $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3 ], time() + 86400 );
+               $this->assertTrue( $ok, "Expiry set for all keys" );
+
+               $ok = $this->cache->changeTTLMulti( [ $key1, $key2, $key3, $key4 ], 300 );
+               $this->assertFalse( $ok, "One key missing" );
+
+               $this->assertEquals( 2, $this->cache->incr( $key1 ) );
+               $this->assertEquals( 3, $this->cache->incr( $key2 ) );
+               $this->assertEquals( 4, $this->cache->incr( $key3 ) );
+
+               // cleanup
+               $this->cache->delete( $key1 );
+               $this->cache->delete( $key2 );
+               $this->cache->delete( $key3 );
+               $this->cache->delete( $key4 );
+       }
+
+       /**
+        * @covers MediumSpecificBagOStuff::add
         */
        public function testAdd() {
                $key = $this->cache->makeKey( self::TEST_KEY );
+               $this->assertFalse( $this->cache->get( $key ) );
                $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
+               $this->assertFalse( $this->cache->add( $key, 'test', 5 ) );
        }
 
        /**
-        * @covers BagOStuff::get
+        * @covers MediumSpecificBagOStuff::get
         */
        public function testGet() {
                $value = [ 'this' => 'is', 'a' => 'test' ];
@@ -141,9 +205,9 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::get
-        * @covers BagOStuff::set
-        * @covers BagOStuff::getWithSetCallback
+        * @covers MediumSpecificBagOStuff::get
+        * @covers MediumSpecificBagOStuff::set
+        * @covers MediumSpecificBagOStuff::getWithSetCallback
         */
        public function testGetWithSetCallback() {
                $key = $this->cache->makeKey( self::TEST_KEY );
@@ -160,7 +224,7 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::incr
+        * @covers MediumSpecificBagOStuff::incr
         */
        public function testIncr() {
                $key = $this->cache->makeKey( self::TEST_KEY );
@@ -172,7 +236,7 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::incrWithInit
+        * @covers MediumSpecificBagOStuff::incrWithInit
         */
        public function testIncrWithInit() {
                $key = $this->cache->makeKey( self::TEST_KEY );
@@ -184,7 +248,7 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::getMulti
+        * @covers MediumSpecificBagOStuff::getMulti
         */
        public function testGetMulti() {
                $value1 = [ 'this' => 'is', 'a' => 'test' ];
@@ -224,8 +288,8 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::setMulti
-        * @covers BagOStuff::deleteMulti
+        * @covers MediumSpecificBagOStuff::setMulti
+        * @covers MediumSpecificBagOStuff::deleteMulti
         */
        public function testSetDeleteMulti() {
                $map = [
@@ -237,14 +301,18 @@ class BagOStuffTest extends MediaWikiTestCase {
                        $this->cache->makeKey( 'test-6' ) => 'ever'
                ];
 
-               $this->cache->setMulti( $map, 5 );
+               $this->assertTrue( $this->cache->setMulti( $map ) );
                $this->assertEquals(
                        $map,
                        $this->cache->getMulti( array_keys( $map ) )
                );
 
-               $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ), 5 ) );
+               $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ) ) );
 
+               $this->assertEquals(
+                       [],
+                       $this->cache->getMulti( array_keys( $map ), BagOStuff::READ_LATEST )
+               );
                $this->assertEquals(
                        [],
                        $this->cache->getMulti( array_keys( $map ) )
@@ -252,7 +320,57 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::getScopedLock
+        * @covers MediumSpecificBagOStuff::get
+        * @covers MediumSpecificBagOStuff::getMulti
+        * @covers MediumSpecificBagOStuff::merge
+        * @covers MediumSpecificBagOStuff::delete
+        */
+       public function testSetSegmentable() {
+               $key = $this->cache->makeKey( self::TEST_KEY );
+               $tiny = 418;
+               $small = wfRandomString( 32 );
+               // 64 * 8 * 32768 = 16777216 bytes
+               $big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
+
+               $callback = function ( $cache, $key, $oldValue ) {
+                       return $oldValue . '!';
+               };
+
+               foreach ( [ $tiny, $small, $big ] as $value ) {
+                       $this->cache->set( $key, $value, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
+                       $this->assertEquals( $value, $this->cache->get( $key ) );
+                       $this->assertEquals( $value, $this->cache->getMulti( [ $key ] )[$key] );
+
+                       $this->assertTrue( $this->cache->merge( $key, $callback, 5 ) );
+                       $this->assertEquals( "$value!", $this->cache->get( $key ) );
+                       $this->assertEquals( "$value!", $this->cache->getMulti( [ $key ] )[$key] );
+
+                       $this->assertTrue( $this->cache->deleteMulti( [ $key ] ) );
+                       $this->assertFalse( $this->cache->get( $key ) );
+                       $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
+
+                       $this->cache->set( $key, "@$value", 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
+                       $this->assertEquals( "@$value", $this->cache->get( $key ) );
+                       $this->assertTrue( $this->cache->delete( $key, BagOStuff::WRITE_PRUNE_SEGMENTS ) );
+                       $this->assertFalse( $this->cache->get( $key ) );
+                       $this->assertEquals( [], $this->cache->getMulti( [ $key ] ) );
+               }
+
+               $this->cache->set( $key, 666, 10, BagOStuff::WRITE_ALLOW_SEGMENTS );
+
+               $this->assertEquals( 666, $this->cache->get( $key ) );
+               $this->assertEquals( 667, $this->cache->incr( $key ) );
+               $this->assertEquals( 667, $this->cache->get( $key ) );
+
+               $this->assertEquals( 664, $this->cache->decr( $key, 3 ) );
+               $this->assertEquals( 664, $this->cache->get( $key ) );
+
+               $this->assertTrue( $this->cache->delete( $key ) );
+               $this->assertFalse( $this->cache->get( $key ) );
+       }
+
+       /**
+        * @covers MediumSpecificBagOStuff::getScopedLock
         */
        public function testGetScopedLock() {
                $key = $this->cache->makeKey( self::TEST_KEY );
@@ -276,8 +394,8 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::__construct
-        * @covers BagOStuff::trackDuplicateKeys
+        * @covers MediumSpecificBagOStuff::__construct
+        * @covers MediumSpecificBagOStuff::trackDuplicateKeys
         */
        public function testReportDupes() {
                $logger = $this->createMock( Psr\Log\NullLogger::class );
@@ -301,8 +419,8 @@ class BagOStuffTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers BagOStuff::lock()
-        * @covers BagOStuff::unlock()
+        * @covers MediumSpecificBagOStuff::lock()
+        * @covers MediumSpecificBagOStuff::unlock()
         */
        public function testLocking() {
                $key = 'test';
@@ -316,4 +434,11 @@ class BagOStuffTest extends MediaWikiTestCase {
                $this->assertTrue( $this->cache->unlock( $key2 ) );
                $this->assertTrue( $this->cache->unlock( $key2 ) );
        }
+
+       public function tearDown() {
+               $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
+               $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
+
+               parent::tearDown();
+       }
 }