MediaWiki  1.31.0
BagOStuffTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\ScopedCallback;
4 
11  private $cache;
12 
13  protected function setUp() {
14  parent::setUp();
15 
16  // type defined through parameter
17  if ( $this->getCliArg( 'use-bagostuff' ) ) {
18  $name = $this->getCliArg( 'use-bagostuff' );
19 
21  } else {
22  // no type defined - use simple hash
23  $this->cache = new HashBagOStuff;
24  }
25 
26  $this->cache->delete( wfMemcKey( 'test' ) );
27  }
28 
33  public function testMakeKey() {
34  $cache = ObjectCache::newFromId( 'hash' );
35 
36  $localKey = $cache->makeKey( 'first', 'second', 'third' );
37  $globalKey = $cache->makeGlobalKey( 'first', 'second', 'third' );
38 
39  $this->assertStringMatchesFormat(
40  '%Sfirst%Ssecond%Sthird%S',
41  $localKey,
42  'Local key interpolates parameters'
43  );
44 
45  $this->assertStringMatchesFormat(
46  'global%Sfirst%Ssecond%Sthird%S',
47  $globalKey,
48  'Global key interpolates parameters and contains global prefix'
49  );
50 
51  $this->assertNotEquals(
52  $localKey,
53  $globalKey,
54  'Local key and global key with same parameters should not be equal'
55  );
56 
57  $this->assertNotEquals(
58  $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
59  $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
60  );
61  }
62 
67  public function testMerge() {
68  $key = wfMemcKey( 'test' );
69 
70  $usleep = 0;
71 
81  $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
82  // let's pretend this is an expensive callback to test concurrent merge attempts
83  usleep( $usleep );
84 
85  if ( $existingValue === false ) {
86  return 'merged';
87  }
88 
89  return $existingValue . 'merged';
90  };
91 
92  // merge on non-existing value
93  $merged = $this->cache->merge( $key, $callback, 0 );
94  $this->assertTrue( $merged );
95  $this->assertEquals( 'merged', $this->cache->get( $key ) );
96 
97  // merge on existing value
98  $merged = $this->cache->merge( $key, $callback, 0 );
99  $this->assertTrue( $merged );
100  $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
101 
102  /*
103  * Test concurrent merges by forking this process, if:
104  * - not manually called with --use-bagostuff
105  * - pcntl_fork is supported by the system
106  * - cache type will correctly support calls over forks
107  */
108  $fork = (bool)$this->getCliArg( 'use-bagostuff' );
109  $fork &= function_exists( 'pcntl_fork' );
110  $fork &= !$this->cache instanceof HashBagOStuff;
111  $fork &= !$this->cache instanceof EmptyBagOStuff;
112  $fork &= !$this->cache instanceof MultiWriteBagOStuff;
113  if ( $fork ) {
114  // callback should take awhile now so that we can test concurrent merge attempts
115  $pid = pcntl_fork();
116  if ( $pid == -1 ) {
117  // can't fork, ignore this test...
118  } elseif ( $pid ) {
119  // wait a little, making sure that the child process is calling merge
120  usleep( 3000 );
121 
122  // attempt a merge - this should fail
123  $merged = $this->cache->merge( $key, $callback, 0, 1 );
124 
125  // merge has failed because child process was merging (and we only attempted once)
126  $this->assertFalse( $merged );
127 
128  // make sure the child's merge is completed and verify
129  usleep( 3000 );
130  $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
131  } else {
132  $this->cache->merge( $key, $callback, 0, 1 );
133 
134  // Note: I'm not even going to check if the merge worked, I'll
135  // compare values in the parent process to test if this merge worked.
136  // I'm just going to exit this child process, since I don't want the
137  // child to output any test results (would be rather confusing to
138  // have test output twice)
139  exit;
140  }
141  }
142  }
143 
147  public function testChangeTTL() {
148  $key = wfMemcKey( 'test' );
149  $value = 'meow';
150 
151  $this->cache->add( $key, $value );
152  $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
153  $this->assertEquals( $this->cache->get( $key ), $value );
154  $this->cache->delete( $key );
155  $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
156  }
157 
161  public function testAdd() {
162  $key = wfMemcKey( 'test' );
163  $this->assertTrue( $this->cache->add( $key, 'test' ) );
164  }
165 
169  public function testGet() {
170  $value = [ 'this' => 'is', 'a' => 'test' ];
171 
172  $key = wfMemcKey( 'test' );
173  $this->cache->add( $key, $value );
174  $this->assertEquals( $this->cache->get( $key ), $value );
175  }
176 
180  public function testGetWithSetCallback() {
181  $key = wfMemcKey( 'test' );
182  $value = $this->cache->getWithSetCallback(
183  $key,
184  30,
185  function () {
186  return 'hello kitty';
187  }
188  );
189 
190  $this->assertEquals( 'hello kitty', $value );
191  $this->assertEquals( $value, $this->cache->get( $key ) );
192  }
193 
197  public function testIncr() {
198  $key = wfMemcKey( 'test' );
199  $this->cache->add( $key, 0 );
200  $this->cache->incr( $key );
201  $expectedValue = 1;
202  $actualValue = $this->cache->get( $key );
203  $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
204  }
205 
209  public function testIncrWithInit() {
210  $key = wfMemcKey( 'test' );
211  $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
212  $this->assertEquals( 3, $val, "Correct init value" );
213 
214  $val = $this->cache->incrWithInit( $key, 0, 1, 3 );
215  $this->assertEquals( 4, $val, "Correct init value" );
216  }
217 
221  public function testGetMulti() {
222  $value1 = [ 'this' => 'is', 'a' => 'test' ];
223  $value2 = [ 'this' => 'is', 'another' => 'test' ];
224  $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
225  $value4 = [ 'another test where chars in key will be encoded' ];
226 
227  $key1 = wfMemcKey( 'test1' );
228  $key2 = wfMemcKey( 'test2' );
229  // internally, MemcachedBagOStuffs will encode to will-%25-encode
230  $key3 = wfMemcKey( 'will-%-encode' );
231  $key4 = wfMemcKey(
232  'flowdb:flow_ref:wiki:by-source:v3:Parser\'s_"broken"_+_(page)_&_grill:testwiki:1:4.7'
233  );
234 
235  $this->cache->add( $key1, $value1 );
236  $this->cache->add( $key2, $value2 );
237  $this->cache->add( $key3, $value3 );
238  $this->cache->add( $key4, $value4 );
239 
240  $this->assertEquals(
241  [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
242  $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
243  );
244 
245  // cleanup
246  $this->cache->delete( $key1 );
247  $this->cache->delete( $key2 );
248  $this->cache->delete( $key3 );
249  $this->cache->delete( $key4 );
250  }
251 
255  public function testGetScopedLock() {
256  $key = wfMemcKey( 'test' );
257  $value1 = $this->cache->getScopedLock( $key, 0 );
258  $value2 = $this->cache->getScopedLock( $key, 0 );
259 
260  $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
261  $this->assertNull( $value2, 'Duplicate call returned no lock' );
262 
263  unset( $value1 );
264 
265  $value3 = $this->cache->getScopedLock( $key, 0 );
266  $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
267  unset( $value3 );
268 
269  $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
270  $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
271 
272  $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
273  $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
274  }
275 
280  public function testReportDupes() {
281  $logger = $this->createMock( Psr\Log\NullLogger::class );
282  $logger->expects( $this->once() )
283  ->method( 'warning' )
284  ->with( 'Duplicate get(): "{key}" fetched {count} times', [
285  'key' => 'foo',
286  'count' => 2,
287  ] );
288 
289  $cache = new HashBagOStuff( [
290  'reportDupes' => true,
291  'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
292  'logger' => $logger,
293  ] );
294  $cache->get( 'foo' );
295  $cache->get( 'bar' );
296  $cache->get( 'foo' );
297 
299  }
300 }
BagOStuffTest\setUp
setUp()
Definition: BagOStuffTest.php:13
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:221
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
BagOStuffTest\testGetScopedLock
testGetScopedLock()
BagOStuff::getScopedLock.
Definition: BagOStuffTest.php:255
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
BagOStuffTest\testIncrWithInit
testIncrWithInit()
BagOStuff::incrWithInit.
Definition: BagOStuffTest.php:209
MediaWikiTestCase\getCliArg
getCliArg( $offset)
Definition: MediaWikiTestCase.php:1651
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
BagOStuff\makeKey
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Definition: BagOStuff.php:766
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
BagOStuffTest\testChangeTTL
testChangeTTL()
BagOStuff::changeTTL.
Definition: BagOStuffTest.php:147
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::mergeViaLock.
Definition: BagOStuffTest.php:67
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:280
BagOStuffTest\testMakeKey
testMakeKey()
BagOStuff::makeGlobalKey BagOStuff::makeKeyInternal.
Definition: BagOStuffTest.php:33
wfMemcKey
wfMemcKey()
Make a cache key for the local wiki.
Definition: GlobalFunctions.php:2700
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
BagOStuffTest\testIncr
testIncr()
BagOStuff::incr.
Definition: BagOStuffTest.php:197
BagOStuffTest\testGet
testGet()
BagOStuff::get.
Definition: BagOStuffTest.php:169
$value
$value
Definition: styleTest.css.php:45
BagOStuff\makeGlobalKey
makeGlobalKey( $class, $component=null)
Make a global cache key.
Definition: BagOStuff.php:754
MultiWriteBagOStuff
A cache class that replicates all writes to multiple child caches.
Definition: MultiWriteBagOStuff.php:32
BagOStuffTest\testGetWithSetCallback
testGetWithSetCallback()
BagOStuff::getWithSetCallback.
Definition: BagOStuffTest.php:180
BagOStuff\get
get( $key, $flags=0, $oldFlags=null)
Get an item with the given key.
Definition: BagOStuff.php:179
DeferredUpdates\doUpdates
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
Definition: DeferredUpdates.php:123
BagOStuffTest\$cache
BagOStuff $cache
Definition: BagOStuffTest.php:11
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:737
MediaWikiTestCase\assertType
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
Definition: MediaWikiTestCase.php:1878
BagOStuffTest\testAdd
testAdd()
BagOStuff::add.
Definition: BagOStuffTest.php:161