Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / libs / objectcache / MultiWriteBagOStuffTest.php
blob786ff508a37dff5c5553cc82ac33fbea19bca15e
1 <?php
3 use Wikimedia\LightweightObjectStore\StorageAwareness;
4 use Wikimedia\ObjectCache\HashBagOStuff;
5 use Wikimedia\ObjectCache\MultiWriteBagOStuff;
6 use Wikimedia\TestingAccessWrapper;
8 /**
9 * @covers \Wikimedia\ObjectCache\MultiWriteBagOStuff
10 * @group BagOStuff
11 * @group Database
13 class MultiWriteBagOStuffTest extends MediaWikiIntegrationTestCase {
14 /** @var HashBagOStuff */
15 private $cache1;
16 /** @var HashBagOStuff */
17 private $cache2;
18 /** @var MultiWriteBagOStuff */
19 private $cache;
21 protected function setUp(): void {
22 parent::setUp();
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 ] );
33 public function testSet() {
34 $key = 'key';
35 $value = 'value';
36 $this->cache->set( $key, $value );
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' );
44 public function testAdd() {
45 $key = 'key';
46 $value = 'value';
47 $ok = $this->cache->add( $key, $value );
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' );
56 public function testSyncMergeAsync() {
57 $key = 'keyA';
58 $value = 'value';
59 $func = static function () use ( $value ) {
60 return $value;
63 // XXX: DeferredUpdates bound to transactions in CLI mode
64 $dbw = $this->getDb();
65 $dbw->begin();
66 $this->cache->merge( $key, $func );
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' );
73 $dbw->commit();
74 $this->runDeferredUpdates();
76 // Set in tier 2
77 $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
80 public function testSyncMergeSync() {
81 // Like setUp() but without 'async'
82 $cache1 = new HashBagOStuff();
83 $cache2 = new HashBagOStuff();
84 $cache = new MultiWriteBagOStuff( [
85 'caches' => [ $cache1, $cache2 ]
86 ] );
87 $value = 'value';
88 $func = static function () use ( $value ) {
89 return $value;
92 $key = 'keyB';
94 $dbw = $this->getDb();
95 $dbw->begin();
96 $cache->merge( $key, $func );
98 // Set in tier 1
99 $this->assertEquals( $value, $cache1->get( $key ), 'Written to tier 1' );
100 // Immediately set in tier 2
101 $this->assertEquals( $value, $cache2->get( $key ), 'Written to tier 2' );
103 $dbw->commit();
104 $this->runDeferredUpdates();
107 public function testSetDelayed() {
108 $key = 'key';
109 $value = (object)[ 'v' => 'saved value' ];
110 $expectValue = clone $value;
112 // XXX: DeferredUpdates bound to transactions in CLI mode
113 $dbw = $this->getDb();
114 $dbw->begin();
115 $this->cache->set( $key, $value );
117 // Test that later changes to $value don't affect the saved value (e.g. T168040)
118 $value->v = 'other value';
120 // Set in tier 1
121 $this->assertEquals( $expectValue, $this->cache1->get( $key ), 'Written to tier 1' );
122 // Not yet set in tier 2
123 $this->assertFalse( $this->cache2->get( $key ), 'Not written to tier 2' );
125 $dbw->commit();
126 $this->runDeferredUpdates();
128 // Set in tier 2
129 $this->assertEquals( $expectValue, $this->cache2->get( $key ), 'Written to tier 2' );
132 public function testMakeKey() {
133 $cache1 = $this->getMockBuilder( HashBagOStuff::class )
134 ->onlyMethods( [ 'makeKey' ] )->getMock();
135 $cache1->expects( $this->never() )->method( 'makeKey' );
137 $cache2 = $this->getMockBuilder( HashBagOStuff::class )
138 ->onlyMethods( [ 'makeKey' ] )->getMock();
139 $cache2->expects( $this->never() )->method( 'makeKey' );
141 $cache = new MultiWriteBagOStuff( [
142 'keyspace' => 'generic',
143 'caches' => [ $cache1, $cache2 ]
144 ] );
146 $this->assertSame( 'generic:a:b', $cache->makeKey( 'a', 'b' ) );
149 public function testConvertGenericKey() {
150 $cache1 = new class extends HashBagOStuff {
151 protected function makeKeyInternal( $keyspace, $components ) {
152 return $keyspace . ':short-one-way';
155 protected function requireConvertGenericKey(): bool {
156 return true;
159 $cache2 = new class extends HashBagOStuff {
160 protected function makeKeyInternal( $keyspace, $components ) {
161 return $keyspace . ':short-another-way';
164 protected function requireConvertGenericKey(): bool {
165 return true;
169 $cache = new MultiWriteBagOStuff( [
170 'caches' => [ $cache1, $cache2 ]
171 ] );
172 $key = $cache->makeKey( 'a', 'b' );
173 $cache->set( $key, 'my_value' );
175 $this->assertSame(
176 'local:a:b',
177 $key
179 $this->assertSame(
180 [ 'local:short-one-way' ],
181 array_keys( TestingAccessWrapper::newFromObject( $cache1 )->bag ),
182 'key gets re-encoded for first backend'
184 $this->assertSame(
185 [ 'local:short-another-way' ],
186 array_keys( TestingAccessWrapper::newFromObject( $cache2 )->bag ),
187 'key gets re-encoded for second backend'
191 public function testMakeGlobalKey() {
192 $cache1 = $this->getMockBuilder( HashBagOStuff::class )
193 ->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
194 $cache1->expects( $this->never() )->method( 'makeGlobalKey' );
196 $cache2 = $this->getMockBuilder( HashBagOStuff::class )
197 ->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
198 $cache2->expects( $this->never() )->method( 'makeGlobalKey' );
200 $cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
202 $this->assertSame( 'global:a:b', $cache->makeGlobalKey( 'a', 'b' ) );
205 public function testDuplicateStoreAdd() {
206 $bag = new HashBagOStuff();
207 $cache = new MultiWriteBagOStuff( [
208 'caches' => [ $bag, $bag ],
209 ] );
211 $this->assertTrue( $cache->add( 'key', 1, 30 ) );
214 public function testIncrWithInit() {
215 $key = $this->cache->makeKey( 'key' );
216 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
217 $this->assertSame( 3, $val, "Correct init value" );
219 $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
220 $this->assertSame( 4, $val, "Correct init value" );
221 $this->cache->delete( $key );
223 $val = $this->cache->incrWithInit( $key, 0, 5 );
224 $this->assertSame( 5, $val, "Correct init value" );
227 public function testErrorHandling() {
228 $t1Cache = $this->createPartialMock( HashBagOStuff::class, [ 'set' ] );
229 $t1CacheWrapper = TestingAccessWrapper::newFromObject( $t1Cache );
230 $t1CacheNextError = StorageAwareness::ERR_NONE;
231 $t1Cache->method( 'set' )
232 ->willReturnCallback( static function () use ( $t1CacheWrapper, &$t1CacheNextError ) {
233 if ( $t1CacheNextError !== StorageAwareness::ERR_NONE ) {
234 $t1CacheWrapper->setLastError( $t1CacheNextError );
236 return false;
239 return true;
240 } );
241 $t2Cache = $this->createPartialMock( HashBagOStuff::class, [ 'set' ] );
242 $t2CacheWrapper = TestingAccessWrapper::newFromObject( $t2Cache );
243 $t2CacheNextError = StorageAwareness::ERR_NONE;
244 $t2Cache->method( 'set' )
245 ->willReturnCallback( static function () use ( $t2CacheWrapper, &$t2CacheNextError ) {
246 if ( $t2CacheNextError !== StorageAwareness::ERR_NONE ) {
247 $t2CacheWrapper->setLastError( $t2CacheNextError );
249 return false;
252 return true;
253 } );
254 $cache = new MultiWriteBagOStuff( [
255 'keyspace' => 'repl_local',
256 'caches' => [ $t1Cache, $t2Cache ]
257 ] );
258 $cacheWrapper = TestingAccessWrapper::newFromObject( $cache );
259 $key = 'a:key';
261 $wp1 = $cache->watchErrors();
262 $cache->set( $key, 'value', 3600 );
263 $this->assertSame( StorageAwareness::ERR_NONE, $t1Cache->getLastError() );
264 $this->assertSame( StorageAwareness::ERR_NONE, $t2Cache->getLastError() );
265 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError() );
266 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp1 ) );
268 $t1CacheNextError = StorageAwareness::ERR_NO_RESPONSE;
269 $t2CacheNextError = StorageAwareness::ERR_UNREACHABLE;
271 $cache->set( $key, 'value', 3600 );
272 $this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
273 $this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
274 $this->assertSame( $t2CacheNextError, $cache->getLastError() );
275 $this->assertSame( $t2CacheNextError, $cache->getLastError( $wp1 ) );
277 $t1CacheNextError = StorageAwareness::ERR_NO_RESPONSE;
278 $t2CacheNextError = StorageAwareness::ERR_UNEXPECTED;
280 $wp2 = $cache->watchErrors();
281 $cache->set( $key, 'value', 3600 );
282 $wp3 = $cache->watchErrors();
283 $this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
284 $this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );
285 $this->assertSame( $t2CacheNextError, $cache->getLastError( $wp2 ) );
286 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp3 ) );
288 $cacheWrapper->setLastError( StorageAwareness::ERR_UNEXPECTED );
289 $wp4 = $cache->watchErrors();
290 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError() );
291 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp1 ) );
292 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp2 ) );
293 $this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp3 ) );
294 $this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp4 ) );
295 $this->assertSame( $t1CacheNextError, $t1Cache->getLastError() );
296 $this->assertSame( $t2CacheNextError, $t2Cache->getLastError() );