MediaWiki  1.33.0
BagOStuffTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\ScopedCallback;
4 
11  private $cache;
12 
13  const TEST_KEY = 'test';
14 
15  protected function setUp() {
16  parent::setUp();
17 
18  // type defined through parameter
19  if ( $this->getCliArg( 'use-bagostuff' ) !== null ) {
20  $name = $this->getCliArg( 'use-bagostuff' );
21 
23  } else {
24  // no type defined - use simple hash
25  $this->cache = new HashBagOStuff;
26  }
27 
28  $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) );
29  $this->cache->delete( $this->cache->makeKey( self::TEST_KEY ) . ':lock' );
30  }
31 
36  public function testMakeKey() {
37  $cache = ObjectCache::newFromId( 'hash' );
38 
39  $localKey = $cache->makeKey( 'first', 'second', 'third' );
40  $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
41 
42  $this->assertStringMatchesFormat(
43  '%Sfirst%Ssecond%Sthird%S',
44  $localKey,
45  'Local key interpolates parameters'
46  );
47 
48  $this->assertStringMatchesFormat(
49  'global%Sfirst%Ssecond%Sthird%S',
50  $globalKey,
51  'Global key interpolates parameters and contains global prefix'
52  );
53 
54  $this->assertNotEquals(
55  $localKey,
56  $globalKey,
57  'Local key and global key with same parameters should not be equal'
58  );
59 
60  $this->assertNotEquals(
61  $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
62  $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
63  );
64  }
65 
70  public function testMerge() {
71  $key = $this->cache->makeKey( self::TEST_KEY );
72 
73  $calls = 0;
74  $casRace = false; // emulate a race
75  $callback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$calls, &$casRace ) {
76  ++$calls;
77  if ( $casRace ) {
78  // Uses CAS instead?
79  $cache->set( $key, 'conflict', 5 );
80  }
81 
82  return ( $oldVal === false ) ? 'merged' : $oldVal . 'merged';
83  };
84 
85  // merge on non-existing value
86  $merged = $this->cache->merge( $key, $callback, 5 );
87  $this->assertTrue( $merged );
88  $this->assertEquals( 'merged', $this->cache->get( $key ) );
89 
90  // merge on existing value
91  $merged = $this->cache->merge( $key, $callback, 5 );
92  $this->assertTrue( $merged );
93  $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
94 
95  $calls = 0;
96  $casRace = true;
97  $this->assertFalse(
98  $this->cache->merge( $key, $callback, 5, 1 ),
99  'Non-blocking merge (CAS)'
100  );
101  if ( $this->cache instanceof MultiWriteBagOStuff ) {
102  $wrapper = \Wikimedia\TestingAccessWrapper::newFromObject( $this->cache );
103  $n = count( $wrapper->caches );
104  } else {
105  $n = 1;
106  }
107  $this->assertEquals( $n, $calls );
108  }
109 
113  public function testChangeTTL() {
114  $key = $this->cache->makeKey( self::TEST_KEY );
115  $value = 'meow';
116 
117  $this->cache->add( $key, $value, 5 );
118  $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
119  $this->assertEquals( $this->cache->get( $key ), $value );
120  $this->cache->delete( $key );
121  $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
122  }
123 
127  public function testAdd() {
128  $key = $this->cache->makeKey( self::TEST_KEY );
129  $this->assertTrue( $this->cache->add( $key, 'test', 5 ) );
130  }
131 
135  public function testGet() {
136  $value = [ 'this' => 'is', 'a' => 'test' ];
137 
138  $key = $this->cache->makeKey( self::TEST_KEY );
139  $this->cache->add( $key, $value, 5 );
140  $this->assertEquals( $this->cache->get( $key ), $value );
141  }
142 
148  public function testGetWithSetCallback() {
149  $key = $this->cache->makeKey( self::TEST_KEY );
150  $value = $this->cache->getWithSetCallback(
151  $key,
152  30,
153  function () {
154  return 'hello kitty';
155  }
156  );
157 
158  $this->assertEquals( 'hello kitty', $value );
159  $this->assertEquals( $value, $this->cache->get( $key ) );
160  }
161 
165  public function testIncr() {
166  $key = $this->cache->makeKey( self::TEST_KEY );
167  $this->cache->add( $key, 0, 5 );
168  $this->cache->incr( $key );
169  $expectedValue = 1;
170  $actualValue = $this->cache->get( $key );
171  $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
172  }
173 
177  public function testIncrWithInit() {
178  $key = $this->cache->makeKey( self::TEST_KEY );
179  $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
180  $this->assertEquals( 3, $val, "Correct init value" );
181 
182  $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
183  $this->assertEquals( 4, $val, "Correct init value" );
184  }
185 
189  public function testGetMulti() {
190  $value1 = [ 'this' => 'is', 'a' => 'test' ];
191  $value2 = [ 'this' => 'is', 'another' => 'test' ];
192  $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
193  $value4 = [ 'another test where chars in key will be encoded' ];
194 
195  $key1 = $this->cache->makeKey( 'test-1' );
196  $key2 = $this->cache->makeKey( 'test-2' );
197  // internally, MemcachedBagOStuffs will encode to will-%25-encode
198  $key3 = $this->cache->makeKey( 'will-%-encode' );
199  $key4 = $this->cache->makeKey(
200  'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
201  );
202 
203  // cleanup
204  $this->cache->delete( $key1 );
205  $this->cache->delete( $key2 );
206  $this->cache->delete( $key3 );
207  $this->cache->delete( $key4 );
208 
209  $this->cache->add( $key1, $value1, 5 );
210  $this->cache->add( $key2, $value2, 5 );
211  $this->cache->add( $key3, $value3, 5 );
212  $this->cache->add( $key4, $value4, 5 );
213 
214  $this->assertEquals(
215  [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
216  $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
217  );
218 
219  // cleanup
220  $this->cache->delete( $key1 );
221  $this->cache->delete( $key2 );
222  $this->cache->delete( $key3 );
223  $this->cache->delete( $key4 );
224  }
225 
230  public function testSetDeleteMulti() {
231  $map = [
232  $this->cache->makeKey( 'test-1' ) => 'Siberian',
233  $this->cache->makeKey( 'test-2' ) => [ 'Huskies' ],
234  $this->cache->makeKey( 'test-3' ) => [ 'are' => 'the' ],
235  $this->cache->makeKey( 'test-4' ) => (object)[ 'greatest' => 'animal' ],
236  $this->cache->makeKey( 'test-5' ) => 4,
237  $this->cache->makeKey( 'test-6' ) => 'ever'
238  ];
239 
240  $this->cache->setMulti( $map, 5 );
241  $this->assertEquals(
242  $map,
243  $this->cache->getMulti( array_keys( $map ) )
244  );
245 
246  $this->assertTrue( $this->cache->deleteMulti( array_keys( $map ), 5 ) );
247 
248  $this->assertEquals(
249  [],
250  $this->cache->getMulti( array_keys( $map ) )
251  );
252  }
253 
257  public function testGetScopedLock() {
258  $key = $this->cache->makeKey( self::TEST_KEY );
259  $value1 = $this->cache->getScopedLock( $key, 0 );
260  $value2 = $this->cache->getScopedLock( $key, 0 );
261 
262  $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
263  $this->assertNull( $value2, 'Duplicate call returned no lock' );
264 
265  unset( $value1 );
266 
267  $value3 = $this->cache->getScopedLock( $key, 0 );
268  $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
269  unset( $value3 );
270 
271  $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
272  $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
273 
274  $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
275  $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
276  }
277 
282  public function testReportDupes() {
283  $logger = $this->createMock( Psr\Log\NullLogger::class );
284  $logger->expects( $this->once() )
285  ->method( 'warning' )
286  ->with( 'Duplicate get(): "{key}" fetched {count} times', [
287  'key' => 'foo',
288  'count' => 2,
289  ] );
290 
291  $cache = new HashBagOStuff( [
292  'reportDupes' => true,
293  'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
294  'logger' => $logger,
295  ] );
296  $cache->get( 'foo' );
297  $cache->get( 'bar' );
298  $cache->get( 'foo' );
299 
301  }
302 
307  public function testLocking() {
308  $key = 'test';
309  $this->assertTrue( $this->cache->lock( $key ) );
310  $this->assertFalse( $this->cache->lock( $key ) );
311  $this->assertTrue( $this->cache->unlock( $key ) );
312 
313  $key2 = 'test2';
314  $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
315  $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
316  $this->assertTrue( $this->cache->unlock( $key2 ) );
317  $this->assertTrue( $this->cache->unlock( $key2 ) );
318  }
319 }
BagOStuffTest\setUp
setUp()
Definition: BagOStuffTest.php:15
ObjectCache\newFromId
static newFromId( $id)
Create a new cache object of the specified type.
Definition: ObjectCache.php:122
BagOStuffTest\testGetMulti
testGetMulti()
BagOStuff::getMulti.
Definition: BagOStuffTest.php:189
BagOStuffTest\testLocking
testLocking()
BagOStuff::lock() BagOStuff::unlock()
Definition: BagOStuffTest.php:307
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
captcha-old.count
count
Definition: captcha-old.py:249
BagOStuffTest\testGetScopedLock
testGetScopedLock()
BagOStuff::getScopedLock.
Definition: BagOStuffTest.php:257
BagOStuffTest\testIncrWithInit
testIncrWithInit()
BagOStuff::incrWithInit.
Definition: BagOStuffTest.php:177
MediaWikiTestCase\getCliArg
getCliArg( $offset)
Definition: MediaWikiTestCase.php:1954
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:58
BagOStuffTest\TEST_KEY
const TEST_KEY
Definition: BagOStuffTest.php:13
BagOStuff\makeKey
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Definition: BagOStuff.php:774
BagOStuffTest\testChangeTTL
testChangeTTL()
BagOStuff::changeTTL.
Definition: BagOStuffTest.php:113
BagOStuffTest
Definition: BagOStuffTest.php:9
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
BagOStuffTest\testMerge
testMerge()
BagOStuff::merge BagOStuff::mergeViaCas.
Definition: BagOStuffTest.php:70
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
BagOStuffTest\testReportDupes
testReportDupes()
BagOStuff::__construct BagOStuff::trackDuplicateKeys.
Definition: BagOStuffTest.php:282
BagOStuffTest\testMakeKey
testMakeKey()
BagOStuff::makeGlobalKey BagOStuff::makeKeyInternal.
Definition: BagOStuffTest.php:36
BagOStuff\get
get( $key, $flags=0)
Get an item with the given key.
Definition: BagOStuff.php:180
BagOStuffTest\testSetDeleteMulti
testSetDeleteMulti()
BagOStuff::setMulti BagOStuff::deleteMulti.
Definition: BagOStuffTest.php:230
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
BagOStuffTest\testIncr
testIncr()
BagOStuff::incr.
Definition: BagOStuffTest.php:165
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
BagOStuffTest\testGet
testGet()
BagOStuff::get.
Definition: BagOStuffTest.php:135
$value
$value
Definition: styleTest.css.php:49
BagOStuff\makeGlobalKey
makeGlobalKey( $class, $component=null)
Make a global cache key.
Definition: BagOStuff.php:762
MultiWriteBagOStuff
A cache class that replicates all writes to multiple child caches.
Definition: MultiWriteBagOStuff.php:35
BagOStuffTest\testGetWithSetCallback
testGetWithSetCallback()
BagOStuff::get BagOStuff::set BagOStuff::getWithSetCallback.
Definition: BagOStuffTest.php:148
DeferredUpdates\doUpdates
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
Definition: DeferredUpdates.php:133
BagOStuffTest\$cache
BagOStuff $cache
Definition: BagOStuffTest.php:11
BagOStuff\set
set( $key, $value, $exptime=0, $flags=0)
Set an item.
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
BagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $args)
Construct a cache key.
Definition: BagOStuff.php:746
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
MediaWikiTestCase\assertType
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
Definition: MediaWikiTestCase.php:2177
BagOStuffTest\testAdd
testAdd()
BagOStuff::add.
Definition: BagOStuffTest.php:127