Debian Codegraph @

tests/phpunit/includes/libs/objectcache/MultiWriteBagOStuffTest.php

raw symbols SCIP
1 <?php
2
3 use Wikimedia\LightweightObjectStore\StorageAwareness;
4 use Wikimedia\ObjectCache\HashBagOStuff;
5 use Wikimedia\ObjectCache\MultiWriteBagOStuff;
6 use Wikimedia\TestingAccessWrapper;
7
8 /**
9 * @covers \Wikimedia\ObjectCache\MultiWriteBagOStuff
10 * @group BagOStuff
11 * @group Database
12 */
13 class MultiWriteBagOStuffTest extends MediaWikiIntegrationTestCase {
14 /** @var HashBagOStuff */
15 private $cache1;
16 /** @var HashBagOStuff */
17 private $cache2;
18 /** @var MultiWriteBagOStuff */
19 private $cache;
20
21 protected function setUp(): void {
22 parent::setUp();
23
24 $this->cache1 = new HashBagOStuff();
25 $this->cache2 = new HashBagOStuff();
26 $this->cache = new MultiWriteBagOStuff( [
27 'caches' => [ $this->cache1, $this->cache2 ],
28 'replication' => 'async',
29 'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
30 ] );
31 }
32
33 public function testSet() {
34 $key = 'key';
35 $value = 'value';
36 $this->cache->set( $key, $value );
37
38 // Set in tier 1
39 $this->assertSame( $value, $this->cache1->get( $key ), 'Written to tier 1' );
40 // Set in tier 2
41 $this->assertSame( $value, $this->cache2->get( $key ), 'Written to tier 2' );
42 }
43
44 public function testAdd() {
45 $key = 'key';
46 $value = 'value';
47 $ok = $this->cache->add( $key, $value );
48
49 $this->assertTrue( $ok );
50 // Set in tier 1
51 $this->assertSame( $value, $this->cache1->get( $key ), 'Written to tier 1' );
52 // Set in tier 2
53 $this->assertSame( $value, $this->cache2->get( $key ), 'Written to tier 2' );
54 }
55
56 public function testSyncMergeAsync() {
57 $key = 'keyA';
58 $value = 'value';
59 $func = static function () use ( $value ) {
60 return $value;
61 };
62
63 // XXX: DeferredUpdates bound to transactions in CLI mode
64 $dbw = $this->getDb();
65 $dbw->begin();
66 $this->cache->merge( $key, $func );
67
68 // Set in tier 1
69 $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
70 // Not yet set in tier 2
71 $this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
72
73 $dbw->commit();
74
75 // Set in tier 2
76 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
77 }
78
79 public function testSyncMergeSync() {
80 // Like setUp() but without 'async'
81 $cache1 = new HashBagOStuff();
82 $cache2 = new HashBagOStuff();
83 $cache = new MultiWriteBagOStuff( [
84 'caches' => [ $cache1, $cache2 ]
85 ] );
86 $value = 'value';
87 $func = static function () use ( $value ) {
88 return $value;
89 };
90
91 $key = 'keyB';
92
93 $dbw = $this->getDb();
94 $dbw->begin();
95 $cache->merge( $key, $func );
96
97 // Set in tier 1
98 $this->assertEquals( $value, $cache1->get( $key ), 'Written to tier 1' );
99 // Immediately set in tier 2
100 $this->assertEquals( $value, $cache2->get( $key ), 'Written to tier 2' );
101
102 $dbw->commit();
103 }
104
105 public function testSetDelayed() {
106 $key = 'key';
107 $value = (object)[ 'v' => 'saved value' ];
108 $expectValue = clone $value;
109
110 // XXX: DeferredUpdates bound to transactions in CLI mode
111 $dbw = $this->getDb();
112 $dbw->begin();
113 $this->cache->set( $key, $value );
114
115 // Test that later changes to $value don't affect the saved value (e.g. T168040)
116 $value->v = 'other value';
117
118 // Set in tier 1
119 $this->assertEquals( $expectValue, $this->cache1->get( $key ), 'Written to tier 1' );
120 // Not yet set in tier 2
121 $this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
122
123 $dbw->commit();
124
125 // Set in tier 2
126 $this->assertEquals( $expectValue, $this->cache2->get( $key ), 'Written to tier 2' );
127 }
128
129 public function testMakeKey() {
130 $cache1 = $this->getMockBuilder( HashBagOStuff::class )
131 ->onlyMethods( [ 'makeKey' ] )->getMock();
132 $cache1->expects( $this->never() )->method( 'makeKey' );
133
134 $cache2 = $this->getMockBuilder( HashBagOStuff::class )
135 ->onlyMethods( [ 'makeKey' ] )->getMock();
136 $cache2->expects( $this->never() )->method( 'makeKey' );
137
138 $cache = new MultiWriteBagOStuff( [
139 'keyspace' => 'generic',
140 'caches' => [ $cache1, $cache2 ]
141 ] );
142
143 $this->assertSame( 'generic:a:b', $cache->makeKey( 'a', 'b' ) );
144 }
145
146 public function testConvertGenericKey() {
147 $cache1 = new class extends HashBagOStuff {
148 protected function makeKeyInternal( $keyspace, $components ) {
149 return $keyspace . ':short-one-way';
150 }
151
152 protected function requireConvertGenericKey(): bool {
153 return true;
154 }
155 };
156 $cache2 = new class extends HashBagOStuff {
157 protected function makeKeyInternal( $keyspace, $components ) {
158 return $keyspace . ':short-another-way';
159 }
160
161 protected function requireConvertGenericKey(): bool {
162 return true;
163 }
164 };
165
166 $cache = new MultiWriteBagOStuff( [
167 'caches' => [ $cache1, $cache2 ]
168 ] );
169 $key = $cache->makeKey( 'a', 'b' );
170 $cache->set( $key, 'my_value' );
171
172 $this->assertSame(
173 'local:a:b',
174 $key
175 );
176 $this->assertSame(
177 [ 'local:short-one-way' ],
178 array_keys( TestingAccessWrapper::newFromObject( $cache1 )->bag ),
179 'key gets re-encoded for first backend'
180 );
181 $this->assertSame(
182 [ 'local:short-another-way' ],
183 array_keys( TestingAccessWrapper::newFromObject( $cache2 )->bag ),
184 'key gets re-encoded for second backend'
185 );
186 }
187
188 public function testMakeGlobalKey() {
189 $cache1 = $this->getMockBuilder( HashBagOStuff::class )
190 ->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
191 $cache1->expects( $this->never() )->method( 'makeGlobalKey' );
192
193 $cache2 = $this->getMockBuilder( HashBagOStuff::class )
194 ->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
195 $cache2->expects( $this->never() )->method( 'makeGlobalKey' );
196
197 $cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
198
199 $this->assertSame( 'global:a:b', $cache->makeGlobalKey( 'a', 'b' ) );
200 }
201
202 public function testDuplicateStoreAdd() {
203 $bag = new HashBagOStuff();
204 $cache = new MultiWriteBagOStuff( [
205 'caches' => [ $bag, $bag ],
206 ] );
207
208 $this->assertTrue( $cache->add( 'key', 1, 30 ) );
209 }
210
211 public function testIncrWithInit() {
212 $key = $this->cache->makeKey( 'key' );
213 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
214 $this->assertSame( 3, $val, "Correct init value" );
215
216 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
217 $this->assertSame( 4, $val, "Correct init value" );
218 $this->cache->delete( $key );
219
220 $val = $this->cache->incrWithInit( $key, 0, 5 );
221 $this->assertSame( 5, $val, "Correct init value" );
222 }
223
224 public function testErrorHandling() {
225 $t1Cache = $this->createPartialMock( HashBagOStuff::class, [ 'set' ] );
226 $t1CacheWrapper = TestingAccessWrapper::newFromObject( $t1Cache );
227 $t1CacheNextError = StorageAwareness::ERR_NONE;
228 $t1Cache->method( 'set' )
229 ->willReturnCallback( static function () use ( $t1CacheWrapper, &$t1CacheNextError ) {
230 if ( $t1CacheNextError !== StorageAwareness::ERR_NONE ) {
231 $t1CacheWrapper->setLastError( $t1CacheNextError );
232
233 return false;
234 }
235
236 return true;
237 } );
238 $t2Cache = $this->createPartialMock( HashBagOStuff::class, [ 'set' ] );
239 $t2CacheWrapper = TestingAccessWrapper::newFromObject( $t2Cache );
240 $t2CacheNextError = StorageAwareness::ERR_NONE;
241 $t2Cache->method( 'set' )
242 ->willReturnCallback( static function () use ( $t2CacheWrapper, &$t2CacheNextError ) {
243 if ( $t2CacheNextError !== StorageAwareness::ERR_NONE ) {
244 $t2CacheWrapper->setLastError( $t2CacheNextError );
245
246 return false;
247 }
248
249 return true;
250 } );
251 $cache = new MultiWriteBagOStuff( [
252 'keyspace' => 'repl_local',
253 'caches' => [ $t1Cache, $t2Cache ]
254 ] );
255 $cacheWrapper = TestingAccessWrapper::newFromObject( $cache );
256 $key = 'a:key';
257
258 $wp1 = $cache->watchErrors();
259 $cache->set( $key, 'value', 3600 );
260 $this->assertSame( StorageAwareness::ERR_NONE, $t1Cache->getLastError() );
261 $this->assertSame( StorageAwareness::ERR_NONE, $t2Cache->getLastError() );
262 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError() );
263 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp1 ) );
264
265 $t1CacheNextError = StorageAwareness::ERR_NO_RESPONSE;
266 $t2CacheNextError = StorageAwareness::ERR_UNREACHABLE;
267
268 $cache->set( $key, 'value', 3600 );
269 $this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
270 $this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
271 $this->assertSame( $t2CacheNextError, $cache->getLastError() );
272 $this->assertSame( $t2CacheNextError, $cache->getLastError( $wp1 ) );
273
274 $t1CacheNextError = StorageAwareness::ERR_NO_RESPONSE;
275 $t2CacheNextError = StorageAwareness::ERR_UNEXPECTED;
276
277 $wp2 = $cache->watchErrors();
278 $cache->set( $key, 'value', 3600 );
279 $wp3 = $cache->watchErrors();
280 $this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
281 $this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
282 $this->assertSame( $t2CacheNextError, $cache->getLastError( $wp2 ) );
283 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp3 ) );
284
285 $cacheWrapper->setLastError( StorageAwareness::ERR_UNEXPECTED );
286 $wp4 = $cache->watchErrors();
287 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError() );
288 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp1 ) );
289 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp2 ) );
290 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp3 ) );
291 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp4 ) );
292 $this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
293 $this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
294 }
295 }