objectcache: Add tests for ReplicatedBagOStuff
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 24 Jun 2015 19:08:22 +0000 (20:08 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 24 Jun 2015 19:17:18 +0000 (20:17 +0100)
Fixed PHP runtime warnings:
> Declaration of ReplicatedBagOStuff::getMulti() should be compatible with BagOStuff.
> Declaration of ReplicatedBagOStuff::decr() should be compatible with BagOStuff.

Change-Id: Icf1a0bf2c30408c4a5bef2de0b69ae2162b234d5

includes/libs/objectcache/ReplicatedBagOStuff.php
tests/phpunit/includes/objectcache/ReplicatedBagOStuffTest.php [new file with mode: 0644]

index a263a3d..1b24616 100644 (file)
@@ -53,7 +53,8 @@ class ReplicatedBagOStuff extends BagOStuff {
                if ( !isset( $params['writeFactory'] ) ) {
                        throw new InvalidArgumentException(
                                __METHOD__ . ': the "writeFactory" parameter is required' );
-               } elseif ( !isset( $params['readFactory'] ) ) {
+               }
+               if ( !isset( $params['readFactory'] ) ) {
                        throw new InvalidArgumentException(
                                __METHOD__ . ': the "readFactory" parameter is required' );
                }
@@ -75,7 +76,7 @@ class ReplicatedBagOStuff extends BagOStuff {
                return $this->readStore->get( $key, $casToken );
        }
 
-       public function getMulti( $keys ) {
+       public function getMulti( array $keys ) {
                return $this->readStore->getMulti( $keys );
        }
 
@@ -95,8 +96,8 @@ class ReplicatedBagOStuff extends BagOStuff {
                return $this->writeStore->incr( $key, $value );
        }
 
-       public function decr( $key ) {
-               return $this->writeStore->decr( $key );
+       public function decr( $key, $value = 1 ) {
+               return $this->writeStore->decr( $key, $value );
        }
 
        public function lock( $key, $timeout = 6, $expiry = 6 ) {
diff --git a/tests/phpunit/includes/objectcache/ReplicatedBagOStuffTest.php b/tests/phpunit/includes/objectcache/ReplicatedBagOStuffTest.php
new file mode 100644 (file)
index 0000000..a419f5b
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+class ReplicatedBagOStuffTest extends MediaWikiTestCase {
+       /** @var HashBagOStuff */
+       private $writeCache;
+       /** @var HashBagOStuff */
+       private $readCache;
+       /** @var ReplicatedBagOStuff */
+       private $cache;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->writeCache = new HashBagOStuff();
+               $this->readCache = new HashBagOStuff();
+               $this->cache = new ReplicatedBagOStuff( array(
+                       'writeFactory' => $this->writeCache,
+                       'readFactory' => $this->readCache,
+               ) );
+       }
+
+       /**
+        * @covers ReplicatedBagOStuff::set
+        */
+       public function testSet() {
+               $key = wfRandomString();
+               $value = wfRandomString();
+               $this->cache->set( $key, $value );
+
+               // Write to master.
+               $this->assertEquals( $this->writeCache->get( $key ), $value );
+               // Don't write to slave. Replication is deferred to backend.
+               $this->assertEquals( $this->readCache->get( $key ), false );
+       }
+
+       /**
+        * @covers ReplicatedBagOStuff::get
+        */
+       public function testGet() {
+               $key = wfRandomString();
+
+               $write = wfRandomString();
+               $this->writeCache->set( $key, $write );
+               $read = wfRandomString();
+               $this->readCache->set( $key, $read );
+
+               // Read from slave.
+               $this->assertEquals( $this->cache->get( $key ), $read );
+       }
+
+       /**
+        * @covers ReplicatedBagOStuff::get
+        */
+       public function testGetAbsent() {
+               $key = wfRandomString();
+               $value = wfRandomString();
+               $this->writeCache->set( $key, $value );
+
+               // Don't read from master. No failover if value is absent.
+               $this->assertEquals( $this->cache->get( $key ), false );
+       }
+}