Add WRITE_SYNC flag to BagOStuff::set()/merge()
[lhc/web/wiklou.git] / tests / phpunit / includes / 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( array(
20 'caches' => array( $this->cache1, $this->cache2 ),
21 'replication' => 'async',
22 'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
23 ) );
24 }
25
26 public function testSetImmediate() {
27 $key = wfRandomString();
28 $value = wfRandomString();
29 $this->cache->set( $key, $value );
30
31 // Set in tier 1
32 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
33 // Set in tier 2
34 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
35 }
36
37 public function testSyncMerge() {
38 $key = wfRandomString();
39 $value = wfRandomString();
40 $func = function () use ( $value ) {
41 return $value;
42 };
43
44 // XXX: DeferredUpdates bound to transactions in CLI mode
45 $dbw = wfGetDB( DB_MASTER );
46 $dbw->begin();
47 $this->cache->merge( $key, $func );
48
49 // Set in tier 1
50 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
51 // Not yet set in tier 2
52 $this->assertEquals( false, $this->cache2->get( $key ), 'Not written to tier 2' );
53
54 $dbw->commit();
55
56 // Set in tier 2
57 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
58
59 $key = wfRandomString();
60
61 $dbw->begin();
62 $this->cache->merge( $key, $func, 0, 1, BagOStuff::WRITE_SYNC );
63
64 // Set in tier 1
65 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
66 // Also set in tier 2
67 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
68
69 $dbw->commit();
70 }
71
72 public function testSetDelayed() {
73 $key = wfRandomString();
74 $value = wfRandomString();
75
76 // XXX: DeferredUpdates bound to transactions in CLI mode
77 $dbw = wfGetDB( DB_MASTER );
78 $dbw->begin();
79 $this->cache->set( $key, $value );
80
81 // Set in tier 1
82 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
83 // Not yet set in tier 2
84 $this->assertEquals( false, $this->cache2->get( $key ), 'Not written to tier 2' );
85
86 $dbw->commit();
87
88 // Set in tier 2
89 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
90 }
91 }