wiki.techinc.nl/tests/phpunit/includes/objectcache/MultiWriteBagOStuffTest.php
Aaron Schulz 5c8ef13306 Add WRITE_SYNC flag to BagOStuff::set()/merge()
* This blocks on writing to all replicas
  and returns false if any failed.
* This is useful if ChronologyProtector is to work across
  domains by having the writes go everywhere so that later
  reads will see them (and be local at the same time).
* Redundant doc comments were also removed.

Change-Id: I9ed098d563c64dba605e7809bc96731da3b3e79d
2015-10-22 01:44:09 +00:00

91 lines
2.4 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( array(
'caches' => array( $this->cache1, $this->cache2 ),
'replication' => 'async',
'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
) );
}
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' );
}
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();
}
public function testSetDelayed() {
$key = wfRandomString();
$value = wfRandomString();
// XXX: DeferredUpdates bound to transactions in CLI mode
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$this->cache->set( $key, $value );
// 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' );
}
}