MediaWiki  1.27.2
BagOStuffTest.php
Go to the documentation of this file.
1 <?php
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 
18  } else {
19  // no type defined - use simple hash
20  $this->cache = new HashBagOStuff;
21  }
22 
23  $this->cache->delete( wfMemcKey( 'test' ) );
24  }
25 
30  public function testMakeKey() {
31  $cache = ObjectCache::newFromId( 'hash' );
32 
33  $localKey = $cache->makeKey( 'first', 'second', 'third' );
34  $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
35 
36  $this->assertStringMatchesFormat(
37  '%Sfirst%Ssecond%Sthird%S',
38  $localKey,
39  'Local key interpolates parameters'
40  );
41 
42  $this->assertStringMatchesFormat(
43  'global%Sfirst%Ssecond%Sthird%S',
44  $globalKey,
45  'Global key interpolates parameters and contains global prefix'
46  );
47 
48  $this->assertNotEquals(
49  $localKey,
50  $globalKey,
51  'Local key and global key with same parameters should not be equal'
52  );
53 
54  $this->assertNotEquals(
55  $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
56  $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
57  );
58  }
59 
64  public function testMerge() {
65  $key = wfMemcKey( 'test' );
66 
67  $usleep = 0;
68 
78  $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
79  // let's pretend this is an expensive callback to test concurrent merge attempts
80  usleep( $usleep );
81 
82  if ( $existingValue === false ) {
83  return 'merged';
84  }
85 
86  return $existingValue . 'merged';
87  };
88 
89  // merge on non-existing value
90  $merged = $this->cache->merge( $key, $callback, 0 );
91  $this->assertTrue( $merged );
92  $this->assertEquals( $this->cache->get( $key ), 'merged' );
93 
94  // merge on existing value
95  $merged = $this->cache->merge( $key, $callback, 0 );
96  $this->assertTrue( $merged );
97  $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
98 
99  /*
100  * Test concurrent merges by forking this process, if:
101  * - not manually called with --use-bagostuff
102  * - pcntl_fork is supported by the system
103  * - cache type will correctly support calls over forks
104  */
105  $fork = (bool)$this->getCliArg( 'use-bagostuff' );
106  $fork &= function_exists( 'pcntl_fork' );
107  $fork &= !$this->cache instanceof HashBagOStuff;
108  $fork &= !$this->cache instanceof EmptyBagOStuff;
109  $fork &= !$this->cache instanceof MultiWriteBagOStuff;
110  if ( $fork ) {
111  // callback should take awhile now so that we can test concurrent merge attempts
112  $pid = pcntl_fork();
113  if ( $pid == -1 ) {
114  // can't fork, ignore this test...
115  } elseif ( $pid ) {
116  // wait a little, making sure that the child process is calling merge
117  usleep( 3000 );
118 
119  // attempt a merge - this should fail
120  $merged = $this->cache->merge( $key, $callback, 0, 1 );
121 
122  // merge has failed because child process was merging (and we only attempted once)
123  $this->assertFalse( $merged );
124 
125  // make sure the child's merge is completed and verify
126  usleep( 3000 );
127  $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
128  } else {
129  $this->cache->merge( $key, $callback, 0, 1 );
130 
131  // Note: I'm not even going to check if the merge worked, I'll
132  // compare values in the parent process to test if this merge worked.
133  // I'm just going to exit this child process, since I don't want the
134  // child to output any test results (would be rather confusing to
135  // have test output twice)
136  exit;
137  }
138  }
139  }
140 
144  public function testAdd() {
145  $key = wfMemcKey( 'test' );
146  $this->assertTrue( $this->cache->add( $key, 'test' ) );
147  }
148 
149  public function testGet() {
150  $value = [ 'this' => 'is', 'a' => 'test' ];
151 
152  $key = wfMemcKey( 'test' );
153  $this->cache->add( $key, $value );
154  $this->assertEquals( $this->cache->get( $key ), $value );
155  }
156 
160  public function testGetWithSetCallback() {
161  $key = wfMemcKey( 'test' );
162  $value = $this->cache->getWithSetCallback(
163  $key,
164  30,
165  function () {
166  return 'hello kitty';
167  }
168  );
169 
170  $this->assertEquals( 'hello kitty', $value );
171  $this->assertEquals( $value, $this->cache->get( $key ) );
172  }
173 
177  public function testIncr() {
178  $key = wfMemcKey( 'test' );
179  $this->cache->add( $key, 0 );
180  $this->cache->incr( $key );
181  $expectedValue = 1;
182  $actualValue = $this->cache->get( $key );
183  $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
184  }
185 
189  public function testIncrWithInit() {
190  $key = wfMemcKey( 'test' );
191  $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
192  $this->assertEquals( 3, $val, "Correct init value" );
193 
194  $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
195  $this->assertEquals( 4, $val, "Correct init value" );
196  }
197 
201  public function testGetMulti() {
202  $value1 = [ 'this' => 'is', 'a' => 'test' ];
203  $value2 = [ 'this' => 'is', 'another' => 'test' ];
204  $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
205  $value4 = [ 'another test where chars in key will be encoded' ];
206 
207  $key1 = wfMemcKey( 'test1' );
208  $key2 = wfMemcKey( 'test2' );
209  // internally, MemcachedBagOStuffs will encode to will-%25-encode
210  $key3 = wfMemcKey( 'will-%-encode' );
211  $key4 = wfMemcKey(
212  'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
213  );
214 
215  $this->cache->add( $key1, $value1 );
216  $this->cache->add( $key2, $value2 );
217  $this->cache->add( $key3, $value3 );
218  $this->cache->add( $key4, $value4 );
219 
220  $this->assertEquals(
221  [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
222  $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
223  );
224 
225  // cleanup
226  $this->cache->delete( $key1 );
227  $this->cache->delete( $key2 );
228  $this->cache->delete( $key3 );
229  $this->cache->delete( $key4 );
230  }
231 
235  public function testGetScopedLock() {
236  $key = wfMemcKey( 'test' );
237  $value1 = $this->cache->getScopedLock( $key, 0 );
238  $value2 = $this->cache->getScopedLock( $key, 0 );
239 
240  $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' );
241  $this->assertNull( $value2, 'Duplicate call returned no lock' );
242 
243  unset( $value1 );
244 
245  $value3 = $this->cache->getScopedLock( $key, 0 );
246  $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' );
247  unset( $value3 );
248 
249  $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
250  $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
251 
252  $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' );
253  $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' );
254  }
255 
260  public function testReportDupes() {
261  $logger = $this->getMock( 'Psr\Log\NullLogger' );
262  $logger->expects( $this->once() )
263  ->method( 'warning' )
264  ->with( 'Duplicate get(): "{key}" fetched {count} times', [
265  'key' => 'foo',
266  'count' => 2,
267  ] );
268 
269  $cache = new HashBagOStuff( [
270  'reportDupes' => true,
271  'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
272  'logger' => $logger,
273  ] );
274  $cache->get( 'foo' );
275  $cache->get( 'bar' );
276  $cache->get( 'foo' );
277 
279  }
280 }
static doUpdates($mode= 'run', $type=self::ALL)
Do any deferred updates and clear the list.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$value
testReportDupes()
BagOStuff::__construct BagOStuff::trackDuplicateKeys.
assertType($type, $actual, $message= '')
Asserts the type of the provided value.
testIncrWithInit()
BagOStuff::incrWithInit.
BagOStuff $cache
testGetMulti()
BagOStuff::getMulti.
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
testIncr()
BagOStuff::incr.
testGetWithSetCallback()
BagOStuff::getWithSetCallback.
testMerge()
BagOStuff::merge BagOStuff::mergeViaLock.
A cache class that replicates all writes to multiple child caches.
testGetScopedLock()
BagOStuff::getScopedLock.
A BagOStuff object with no objects in it.
testAdd()
BagOStuff::add.
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
makeKey()
Make a cache key, scoped to this instance's keyspace.
Definition: BagOStuff.php:728
get($key, $flags=0, $oldFlags=null)
Get an item with the given key.
Definition: BagOStuff.php:173
wfMemcKey()
Make a cache key for the local wiki.
static newFromId($id)
Create a new cache object of the specified type.
testMakeKey()
BagOStuff::makeGlobalKey BagOStuff::makeKeyInternal.
makeKeyInternal($keyspace, $args)
Construct a cache key.
Definition: BagOStuff.php:701
makeGlobalKey()
Make a global cache key.
Definition: BagOStuff.php:717
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310