6 class MultiWriteBagOStuffTest
extends MediaWikiTestCase
{
7 /** @var HashBagOStuff */
9 /** @var HashBagOStuff */
11 /** @var MultiWriteBagOStuff */
14 protected function setUp() {
17 $this->cache1
= new HashBagOStuff();
18 $this->cache2
= new HashBagOStuff();
19 $this->cache
= new MultiWriteBagOStuff( [
20 'caches' => [ $this->cache1
, $this->cache2
],
21 'replication' => 'async',
22 'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
27 * @covers MultiWriteBagOStuff::set
28 * @covers MultiWriteBagOStuff::doWrite
30 public function testSetImmediate() {
31 $key = wfRandomString();
32 $value = wfRandomString();
33 $this->cache
->set( $key, $value );
36 $this->assertEquals( $value, $this->cache1
->get( $key ), 'Written to tier 1' );
38 $this->assertEquals( $value, $this->cache2
->get( $key ), 'Written to tier 2' );
42 * @covers MultiWriteBagOStuff
44 public function testSyncMerge() {
45 $key = wfRandomString();
46 $value = wfRandomString();
47 $func = function () use ( $value ) {
51 // XXX: DeferredUpdates bound to transactions in CLI mode
52 $dbw = wfGetDB( DB_MASTER
);
54 $this->cache
->merge( $key, $func );
57 $this->assertEquals( $value, $this->cache1
->get( $key ), 'Written to tier 1' );
58 // Not yet set in tier 2
59 $this->assertEquals( false, $this->cache2
->get( $key ), 'Not written to tier 2' );
64 $this->assertEquals( $value, $this->cache2
->get( $key ), 'Written to tier 2' );
66 $key = wfRandomString();
69 $this->cache
->merge( $key, $func, 0, 1, BagOStuff
::WRITE_SYNC
);
72 $this->assertEquals( $value, $this->cache1
->get( $key ), 'Written to tier 1' );
74 $this->assertEquals( $value, $this->cache2
->get( $key ), 'Written to tier 2' );
80 * @covers MultiWriteBagOStuff::set
82 public function testSetDelayed() {
83 $key = wfRandomString();
84 $value = wfRandomString();
86 // XXX: DeferredUpdates bound to transactions in CLI mode
87 $dbw = wfGetDB( DB_MASTER
);
89 $this->cache
->set( $key, $value );
92 $this->assertEquals( $value, $this->cache1
->get( $key ), 'Written to tier 1' );
93 // Not yet set in tier 2
94 $this->assertEquals( false, $this->cache2
->get( $key ), 'Not written to tier 2' );
99 $this->assertEquals( $value, $this->cache2
->get( $key ), 'Written to tier 2' );
103 * @covers MultiWriteBagOStuff::makeKey
105 public function testMakeKey() {
106 $cache1 = $this->getMockBuilder( HashBagOStuff
::class )
107 ->setMethods( [ 'makeKey' ] )->getMock();
108 $cache1->expects( $this->once() )->method( 'makeKey' )
109 ->willReturn( 'special' );
111 $cache2 = $this->getMockBuilder( HashBagOStuff
::class )
112 ->setMethods( [ 'makeKey' ] )->getMock();
113 $cache2->expects( $this->never() )->method( 'makeKey' );
115 $cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
116 $this->assertSame( 'special', $cache->makeKey( 'a', 'b' ) );
120 * @covers MultiWriteBagOStuff::makeGlobalKey
122 public function testMakeGlobalKey() {
123 $cache1 = $this->getMockBuilder( HashBagOStuff
::class )
124 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
125 $cache1->expects( $this->once() )->method( 'makeGlobalKey' )
126 ->willReturn( 'special' );
128 $cache2 = $this->getMockBuilder( HashBagOStuff
::class )
129 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
130 $cache2->expects( $this->never() )->method( 'makeGlobalKey' );
132 $cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
134 $this->assertSame( 'special', $cache->makeGlobalKey( 'a', 'b' ) );