Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / MultiWriteBagOStuffTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class MultiWriteBagOStuffTest extends MediaWikiTestCase {
7 /** @var HashBagOStuff */
8 private $cache1;
9 /** @var HashBagOStuff */
10 private $cache2;
11 /** @var MultiWriteBagOStuff */
12 private $cache;
13
14 protected function setUp() {
15 parent::setUp();
16
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'
23 ] );
24 }
25
26 /**
27 * @covers MultiWriteBagOStuff::set
28 * @covers MultiWriteBagOStuff::doWrite
29 */
30 public function testSetImmediate() {
31 $key = wfRandomString();
32 $value = wfRandomString();
33 $this->cache->set( $key, $value );
34
35 // Set in tier 1
36 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
37 // Set in tier 2
38 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
39 }
40
41 /**
42 * @covers MultiWriteBagOStuff
43 */
44 public function testSyncMerge() {
45 $key = wfRandomString();
46 $value = wfRandomString();
47 $func = function () use ( $value ) {
48 return $value;
49 };
50
51 // XXX: DeferredUpdates bound to transactions in CLI mode
52 $dbw = wfGetDB( DB_MASTER );
53 $dbw->begin();
54 $this->cache->merge( $key, $func );
55
56 // Set in tier 1
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' );
60
61 $dbw->commit();
62
63 // Set in tier 2
64 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
65
66 $key = wfRandomString();
67
68 $dbw->begin();
69 $this->cache->merge( $key, $func, 0, 1, BagOStuff::WRITE_SYNC );
70
71 // Set in tier 1
72 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
73 // Also set in tier 2
74 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
75
76 $dbw->commit();
77 }
78
79 /**
80 * @covers MultiWriteBagOStuff::set
81 */
82 public function testSetDelayed() {
83 $key = wfRandomString();
84 $value = wfRandomString();
85
86 // XXX: DeferredUpdates bound to transactions in CLI mode
87 $dbw = wfGetDB( DB_MASTER );
88 $dbw->begin();
89 $this->cache->set( $key, $value );
90
91 // Set in tier 1
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' );
95
96 $dbw->commit();
97
98 // Set in tier 2
99 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
100 }
101 }