From: Timo Tijhof Date: Wed, 24 Jun 2015 19:08:22 +0000 (+0100) Subject: objectcache: Add tests for ReplicatedBagOStuff X-Git-Tag: 1.31.0-rc.0~10993 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=0d98e9f2fb4aade817ff4e7383434f7582966a46;p=lhc%2Fweb%2Fwiklou.git objectcache: Add tests for ReplicatedBagOStuff 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 --- diff --git a/includes/libs/objectcache/ReplicatedBagOStuff.php b/includes/libs/objectcache/ReplicatedBagOStuff.php index a263a3d357..1b246165f7 100644 --- a/includes/libs/objectcache/ReplicatedBagOStuff.php +++ b/includes/libs/objectcache/ReplicatedBagOStuff.php @@ -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 index 0000000000..a419f5b67a --- /dev/null +++ b/tests/phpunit/includes/objectcache/ReplicatedBagOStuffTest.php @@ -0,0 +1,62 @@ +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 ); + } +}