b9fe4903c4a37d6f08dc138f1244bc42bbcb844f
[lhc/web/wiklou.git] / tests / phpunit / includes / objectcache / BagOStuffTest.php
1 <?php
2 /**
3 * @author Matthias Mullie <mmullie@wikimedia.org>
4 * @group BagOStuff
5 */
6 class BagOStuffTest extends MediaWikiTestCase {
7 /** @var BagOStuff */
8 private $cache;
9
10 protected function setUp() {
11 parent::setUp();
12
13 // type defined through parameter
14 if ( $this->getCliArg( 'use-bagostuff' ) ) {
15 $name = $this->getCliArg( 'use-bagostuff' );
16
17 $this->cache = ObjectCache::newFromId( $name );
18 } else {
19 // no type defined - use simple hash
20 $this->cache = new HashBagOStuff;
21 }
22
23 $this->cache->delete( wfMemcKey( 'test' ) );
24 }
25
26 /**
27 * @covers BagOStuff::merge
28 * @covers BagOStuff::mergeViaLock
29 */
30 public function testMerge() {
31 $key = wfMemcKey( 'test' );
32
33 $usleep = 0;
34
35 /**
36 * Callback method: append "merged" to whatever is in cache.
37 *
38 * @param BagOStuff $cache
39 * @param string $key
40 * @param int $existingValue
41 * @use int $usleep
42 * @return int
43 */
44 $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
45 // let's pretend this is an expensive callback to test concurrent merge attempts
46 usleep( $usleep );
47
48 if ( $existingValue === false ) {
49 return 'merged';
50 }
51
52 return $existingValue . 'merged';
53 };
54
55 // merge on non-existing value
56 $merged = $this->cache->merge( $key, $callback, 0 );
57 $this->assertTrue( $merged );
58 $this->assertEquals( $this->cache->get( $key ), 'merged' );
59
60 // merge on existing value
61 $merged = $this->cache->merge( $key, $callback, 0 );
62 $this->assertTrue( $merged );
63 $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
64
65 /*
66 * Test concurrent merges by forking this process, if:
67 * - not manually called with --use-bagostuff
68 * - pcntl_fork is supported by the system
69 * - cache type will correctly support calls over forks
70 */
71 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
72 $fork &= function_exists( 'pcntl_fork' );
73 $fork &= !$this->cache instanceof HashBagOStuff;
74 $fork &= !$this->cache instanceof EmptyBagOStuff;
75 $fork &= !$this->cache instanceof MultiWriteBagOStuff;
76 if ( $fork ) {
77 // callback should take awhile now so that we can test concurrent merge attempts
78 $pid = pcntl_fork();
79 if ( $pid == -1 ) {
80 // can't fork, ignore this test...
81 } elseif ( $pid ) {
82 // wait a little, making sure that the child process is calling merge
83 usleep( 3000 );
84
85 // attempt a merge - this should fail
86 $merged = $this->cache->merge( $key, $callback, 0, 1 );
87
88 // merge has failed because child process was merging (and we only attempted once)
89 $this->assertFalse( $merged );
90
91 // make sure the child's merge is completed and verify
92 usleep( 3000 );
93 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
94 } else {
95 $this->cache->merge( $key, $callback, 0, 1 );
96
97 // Note: I'm not even going to check if the merge worked, I'll
98 // compare values in the parent process to test if this merge worked.
99 // I'm just going to exit this child process, since I don't want the
100 // child to output any test results (would be rather confusing to
101 // have test output twice)
102 exit;
103 }
104 }
105 }
106
107 /**
108 * @covers BagOStuff::add
109 */
110 public function testAdd() {
111 $key = wfMemcKey( 'test' );
112 $this->assertTrue( $this->cache->add( $key, 'test' ) );
113 }
114
115 public function testGet() {
116 $value = array( 'this' => 'is', 'a' => 'test' );
117
118 $key = wfMemcKey( 'test' );
119 $this->cache->add( $key, $value );
120 $this->assertEquals( $this->cache->get( $key ), $value );
121 }
122
123 /**
124 * @covers BagOStuff::getWithSetCallback
125 */
126 public function testGetWithSetCallback() {
127 $key = wfMemcKey( 'test' );
128 $value = $this->cache->getWithSetCallback(
129 $key,
130 30,
131 function () {
132 return 'hello kitty';
133 }
134 );
135
136 $this->assertEquals( 'hello kitty', $value );
137 $this->assertEquals( $value, $this->cache->get( $key ) );
138 }
139
140 /**
141 * @covers BagOStuff::incr
142 */
143 public function testIncr() {
144 $key = wfMemcKey( 'test' );
145 $this->cache->add( $key, 0 );
146 $this->cache->incr( $key );
147 $expectedValue = 1;
148 $actualValue = $this->cache->get( $key );
149 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
150 }
151
152 /**
153 * @covers BagOStuff::getMulti
154 */
155 public function testGetMulti() {
156 $value1 = array( 'this' => 'is', 'a' => 'test' );
157 $value2 = array( 'this' => 'is', 'another' => 'test' );
158 $value3 = array( 'testing a key that may be encoded when sent to cache backend' );
159 $value4 = array( 'another test where chars in key will be encoded' );
160
161 $key1 = wfMemcKey( 'test1' );
162 $key2 = wfMemcKey( 'test2' );
163 // internally, MemcachedBagOStuffs will encode to will-%25-encode
164 $key3 = wfMemcKey( 'will-%-encode' );
165 $key4 = wfMemcKey(
166 'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
167 );
168
169 $this->cache->add( $key1, $value1 );
170 $this->cache->add( $key2, $value2 );
171 $this->cache->add( $key3, $value3 );
172 $this->cache->add( $key4, $value4 );
173
174 $this->assertEquals(
175 array( $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ),
176 $this->cache->getMulti( array( $key1, $key2, $key3, $key4 ) )
177 );
178
179 // cleanup
180 $this->cache->delete( $key1 );
181 $this->cache->delete( $key2 );
182 $this->cache->delete( $key3 );
183 $this->cache->delete( $key4 );
184 }
185
186 /**
187 * @covers BagOStuff::getScopedLock
188 */
189 public function testGetScopedLock() {
190 $key = wfMemcKey( 'test' );
191 $value1 = $this->cache->getScopedLock( $key, 0 );
192 $value2 = $this->cache->getScopedLock( $key, 0 );
193
194 $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' );
195 $this->assertNull( $value2, 'Duplicate call returned no lock' );
196
197 unset( $value1 );
198
199 $value3 = $this->cache->getScopedLock( $key, 0 );
200 $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' );
201 unset( $value3 );
202
203 $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
204 $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
205
206 $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' );
207 $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' );
208 }
209 }