2015-08-21 06:53:52 +00:00
|
|
|
<?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();
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->cache = new MultiWriteBagOStuff( [
|
|
|
|
|
'caches' => [ $this->cache1, $this->cache2 ],
|
2015-10-09 08:01:28 +00:00
|
|
|
'replication' => 'async',
|
|
|
|
|
'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-08-21 06:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-23 06:20:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers MultiWriteBagOStuff::set
|
|
|
|
|
* @covers MultiWriteBagOStuff::doWrite
|
|
|
|
|
*/
|
2015-08-21 06:53:52 +00:00
|
|
|
public function testSetImmediate() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
$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' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 06:20:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers MultiWriteBagOStuff
|
|
|
|
|
*/
|
2015-10-18 22:57:42 +00:00
|
|
|
public function testSyncMerge() {
|
|
|
|
|
$key = wfRandomString();
|
|
|
|
|
$value = wfRandomString();
|
|
|
|
|
$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->assertEquals( false, $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 = wfRandomString();
|
|
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 06:20:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers MultiWriteBagOStuff::set
|
|
|
|
|
*/
|
2015-08-21 06:53:52 +00:00
|
|
|
public function testSetDelayed() {
|
|
|
|
|
$key = wfRandomString();
|
2017-07-07 13:51:00 +00:00
|
|
|
$value = (object)[ 'v' => wfRandomString() ];
|
|
|
|
|
$expectValue = clone $value;
|
2015-08-21 06:53:52 +00:00
|
|
|
|
|
|
|
|
// XXX: DeferredUpdates bound to transactions in CLI mode
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->begin();
|
|
|
|
|
$this->cache->set( $key, $value );
|
|
|
|
|
|
2017-07-07 13:51:00 +00:00
|
|
|
// Test that later changes to $value don't affect the saved value (e.g. T168040)
|
|
|
|
|
$value->v = 'bogus';
|
|
|
|
|
|
2015-08-21 06:53:52 +00:00
|
|
|
// Set in tier 1
|
2017-07-07 13:51:00 +00:00
|
|
|
$this->assertEquals( $expectValue, $this->cache1->get( $key ), 'Written to tier 1' );
|
2015-08-21 06:53:52 +00:00
|
|
|
// Not yet set in tier 2
|
|
|
|
|
$this->assertEquals( false, $this->cache2->get( $key ), 'Not written to tier 2' );
|
|
|
|
|
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
|
|
|
|
|
// Set in tier 2
|
2017-07-07 13:51:00 +00:00
|
|
|
$this->assertEquals( $expectValue, $this->cache2->get( $key ), 'Written to tier 2' );
|
2015-08-21 06:53:52 +00:00
|
|
|
}
|
2017-06-14 17:06:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers MultiWriteBagOStuff::makeKey
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeKey() {
|
|
|
|
|
$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() {
|
|
|
|
|
$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' ) );
|
|
|
|
|
}
|
2015-08-21 06:53:52 +00:00
|
|
|
}
|