3 use Wikimedia\LightweightObjectStore\StorageAwareness
;
4 use Wikimedia\ObjectCache\HashBagOStuff
;
5 use Wikimedia\ObjectCache\MultiWriteBagOStuff
;
6 use Wikimedia\TestingAccessWrapper
;
9 * @covers \Wikimedia\ObjectCache\MultiWriteBagOStuff
13 class MultiWriteBagOStuffTest
extends MediaWikiIntegrationTestCase
{
14 /** @var HashBagOStuff */
16 /** @var HashBagOStuff */
18 /** @var MultiWriteBagOStuff */
21 protected function setUp(): void
{
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'
33 public function testSet() {
36 $this->cache
->set( $key, $value );
39 $this->assertSame( $value, $this->cache1
->get( $key ), 'Written to tier 1' );
41 $this->assertSame( $value, $this->cache2
->get( $key ), 'Written to tier 2' );
44 public function testAdd() {
47 $ok = $this->cache
->add( $key, $value );
49 $this->assertTrue( $ok );
51 $this->assertSame( $value, $this->cache1
->get( $key ), 'Written to tier 1' );
53 $this->assertSame( $value, $this->cache2
->get( $key ), 'Written to tier 2' );
56 public function testSyncMergeAsync() {
59 $func = static function () use ( $value ) {
63 // XXX: DeferredUpdates bound to transactions in CLI mode
64 $dbw = $this->getDb();
66 $this->cache
->merge( $key, $func );
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' );
74 $this->runDeferredUpdates();
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 ]
88 $func = static function () use ( $value ) {
94 $dbw = $this->getDb();
96 $cache->merge( $key, $func );
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' );
104 $this->runDeferredUpdates();
107 public function testSetDelayed() {
109 $value = (object)[ 'v' => 'saved value' ];
110 $expectValue = clone $value;
112 // XXX: DeferredUpdates bound to transactions in CLI mode
113 $dbw = $this->getDb();
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';
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' );
126 $this->runDeferredUpdates();
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 ]
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 {
159 $cache2 = new class extends HashBagOStuff
{
160 protected function makeKeyInternal( $keyspace, $components ) {
161 return $keyspace . ':short-another-way';
164 protected function requireConvertGenericKey(): bool {
169 $cache = new MultiWriteBagOStuff( [
170 'caches' => [ $cache1, $cache2 ]
172 $key = $cache->makeKey( 'a', 'b' );
173 $cache->set( $key, 'my_value' );
180 [ 'local:short-one-way' ],
181 array_keys( TestingAccessWrapper
::newFromObject( $cache1 )->bag
),
182 'key gets re-encoded for first backend'
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 ],
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 );
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 );
254 $cache = new MultiWriteBagOStuff( [
255 'keyspace' => 'repl_local',
256 'caches' => [ $t1Cache, $t2Cache ]
258 $cacheWrapper = TestingAccessWrapper
::newFromObject( $cache );
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() );