wiki.techinc.nl/tests/phpunit/includes/libs/objectcache/MultiWriteBagOStuffTest.php
Timo Tijhof 460e68ab3b objectcache: Use variadic signature for makeKey()
This should help fix the following issues that various repos
are getting from Phan as of late:

> Call with 5 arg(s) to \BagOStuff::makeKey() which only takes 2 arg(s)
> defined at ../../includes/libs/objectcache/BagOStuff.php:456
> <source="PhanParamTooMany"/>

Bug: T228563
Depends-On: I5cfba063821101325a5a7359e6b8ad71a0fb1b2f
Depends-On: Ifa5b96735376f2fbe3680799f960616ba8d357ff
Change-Id: Ic9df7f3ad7f356c7cbdfe1edfbe35821b931dda6
2019-07-23 20:44:06 +02:00

160 lines
4.3 KiB
PHP

<?php
/**
* @group Database
*/
class MultiWriteBagOStuffTest extends MediaWikiTestCase {
/** @var HashBagOStuff */
private $cache1;
/** @var HashBagOStuff */
private $cache2;
/** @var MultiWriteBagOStuff */
private $cache;
protected function setUp() {
parent::setUp();
$this->cache1 = new HashBagOStuff();
$this->cache2 = new HashBagOStuff();
$this->cache = new MultiWriteBagOStuff( [
'caches' => [ $this->cache1, $this->cache2 ],
'replication' => 'async',
'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
] );
}
/**
* @covers MultiWriteBagOStuff::set
* @covers MultiWriteBagOStuff::doWrite
*/
public function testSetImmediate() {
$key = 'key';
$value = 'value';
$this->cache->set( $key, $value );
// Set in tier 1
$this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
// Set in tier 2
$this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
}
/**
* @covers MultiWriteBagOStuff
*/
public function testSyncMerge() {
$key = 'keyA';
$value = 'value';
$func = function () use ( $value ) {
return $value;
};
// XXX: DeferredUpdates bound to transactions in CLI mode
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$this->cache->merge( $key, $func );
// Set in tier 1
$this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
// Not yet set in tier 2
$this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
$dbw->commit();
// Set in tier 2
$this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
$key = 'keyB';
$dbw->begin();
$this->cache->merge( $key, $func, 0, 1, BagOStuff::WRITE_SYNC );
// Set in tier 1
$this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
// Also set in tier 2
$this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
$dbw->commit();
}
/**
* @covers MultiWriteBagOStuff::set
*/
public function testSetDelayed() {
$key = 'key';
$value = (object)[ 'v' => 'saved value' ];
$expectValue = clone $value;
// XXX: DeferredUpdates bound to transactions in CLI mode
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$this->cache->set( $key, $value );
// Test that later changes to $value don't affect the saved value (e.g. T168040)
$value->v = 'other value';
// Set in tier 1
$this->assertEquals( $expectValue, $this->cache1->get( $key ), 'Written to tier 1' );
// Not yet set in tier 2
$this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
$dbw->commit();
// Set in tier 2
$this->assertEquals( $expectValue, $this->cache2->get( $key ), 'Written to tier 2' );
}
/**
* @covers MultiWriteBagOStuff::makeKey
*/
public function testMakeKey() {
if ( defined( 'HHVM_VERSION' ) ) {
$this->markTestSkipped( 'HHVM Reflection buggy' );
}
$cache1 = $this->getMockBuilder( HashBagOStuff::class )
->setMethods( [ 'makeKey' ] )->getMock();
$cache1->expects( $this->once() )->method( 'makeKey' )
->willReturn( 'special' );
$cache2 = $this->getMockBuilder( HashBagOStuff::class )
->setMethods( [ 'makeKey' ] )->getMock();
$cache2->expects( $this->never() )->method( 'makeKey' );
$cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
$this->assertSame( 'special', $cache->makeKey( 'a', 'b' ) );
}
/**
* @covers MultiWriteBagOStuff::makeGlobalKey
*/
public function testMakeGlobalKey() {
if ( defined( 'HHVM_VERSION' ) ) {
$this->markTestSkipped( 'HHVM Reflection buggy' );
}
$cache1 = $this->getMockBuilder( HashBagOStuff::class )
->setMethods( [ 'makeGlobalKey' ] )->getMock();
$cache1->expects( $this->once() )->method( 'makeGlobalKey' )
->willReturn( 'special' );
$cache2 = $this->getMockBuilder( HashBagOStuff::class )
->setMethods( [ 'makeGlobalKey' ] )->getMock();
$cache2->expects( $this->never() )->method( 'makeGlobalKey' );
$cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
$this->assertSame( 'special', $cache->makeGlobalKey( 'a', 'b' ) );
}
/**
* @covers MultiWriteBagOStuff::add
*/
public function testDuplicateStoreAdd() {
$bag = new HashBagOStuff();
$cache = new MultiWriteBagOStuff( [
'caches' => [ $bag, $bag ],
] );
$this->assertTrue( $cache->add( 'key', 1, 30 ) );
}
}