MediaWiki  1.29.2
ProcessCacheLRUTest.php
Go to the documentation of this file.
1 <?php
2 
12 class ProcessCacheLRUTest extends PHPUnit_Framework_TestCase {
13 
18  protected function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
19  $this->assertAttributeEquals( [], 'cache', $cache, $msg );
20  }
21 
25  protected function fillCache( &$cache, $numEntries ) {
26  // Fill cache with three values
27  for ( $i = 1; $i <= $numEntries; $i++ ) {
28  $cache->set( "cache-key-$i", "prop-$i", "value-$i" );
29  }
30  }
31 
36  protected function getExpectedCache( $cacheMaxEntries, $entryToFill ) {
37  $expected = [];
38 
39  if ( $entryToFill === 0 ) {
40  // The cache is empty!
41  return [];
42  } elseif ( $entryToFill <= $cacheMaxEntries ) {
43  // Cache is not fully filled
44  $firstKey = 1;
45  } else {
46  // Cache overflowed
47  $firstKey = 1 + $entryToFill - $cacheMaxEntries;
48  }
49 
50  $lastKey = $entryToFill;
51 
52  for ( $i = $firstKey; $i <= $lastKey; $i++ ) {
53  $expected["cache-key-$i"] = [ "prop-$i" => "value-$i" ];
54  }
55 
56  return $expected;
57  }
58 
62  public function testPhpUnitArrayEquality() {
63  $one = [ 'A' => 1, 'B' => 2 ];
64  $two = [ 'B' => 2, 'A' => 1 ];
65  // ==
66  $this->assertEquals( $one, $two );
67  // ===
68  $this->assertNotSame( $one, $two );
69  }
70 
76  public function testConstructorGivenInvalidValue( $maxSize ) {
77  new ProcessCacheLRUTestable( $maxSize );
78  }
79 
83  public static function provideInvalidConstructorArg() {
84  return [
85  [ null ],
86  [ [] ],
87  [ new stdClass() ],
88  [ 0 ],
89  [ '5' ],
90  [ -1 ],
91  ];
92  }
93 
99  public function testAddAndGetAKey() {
100  $oneCache = new ProcessCacheLRUTestable( 1 );
101  $this->assertCacheEmpty( $oneCache );
102 
103  // First set just one value
104  $oneCache->set( 'cache-key', 'prop1', 'value1' );
105  $this->assertEquals( 1, $oneCache->getEntriesCount() );
106  $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
107  $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
108  }
109 
114  public function testDeleteOldKey() {
115  $oneCache = new ProcessCacheLRUTestable( 1 );
116  $this->assertCacheEmpty( $oneCache );
117 
118  $oneCache->set( 'cache-key', 'prop1', 'value1' );
119  $oneCache->set( 'cache-key', 'prop1', 'value2' );
120  $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
121  }
122 
133  public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
134  $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
135  $this->fillCache( $cache, $entryToFill );
136 
137  $this->assertSame(
138  $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
139  $cache->getCache(),
140  "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
141  );
142  }
143 
147  public static function provideCacheFilling() {
148  // ($cacheMaxEntries, $entryToFill, $msg='')
149  return [
150  [ 1, 0 ],
151  [ 1, 1 ],
152  // overflow
153  [ 1, 2 ],
154  // overflow
155  [ 5, 33 ],
156  ];
157  }
158 
166  $maxEntries = 3;
167 
168  $cache = new ProcessCacheLRUTestable( $maxEntries );
169  // Fill cache leaving just one remaining slot
170  $this->fillCache( $cache, $maxEntries - 1 );
171 
172  // Set an existing cache key
173  $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
174 
175  $this->assertSame(
176  [
177  'cache-key-2' => [ 'prop-2' => 'value-2' ],
178  'cache-key-1' => [ 'prop-1' => 'new-value-for-1' ],
179  ],
180  $cache->getCache()
181  );
182  }
183 
189  public function testRecentlyAccessedKeyStickIn() {
190  $cache = new ProcessCacheLRUTestable( 2 );
191  $cache->set( 'first', 'prop1', 'value1' );
192  $cache->set( 'second', 'prop2', 'value2' );
193 
194  // Get first
195  $cache->get( 'first', 'prop1' );
196  // Cache a third value, should invalidate the least used one
197  $cache->set( 'third', 'prop3', 'value3' );
198 
199  $this->assertFalse( $cache->has( 'second', 'prop2' ) );
200  }
201 
212  $maxEntries = 3;
213 
214  $cache = new ProcessCacheLRUTestable( $maxEntries );
215  $this->fillCache( $cache, $maxEntries );
216 
217  // Set an existing cache key
218  $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
219  $this->assertSame(
220  [
221  'cache-key-1' => [ 'prop-1' => 'value-1' ],
222  'cache-key-3' => [ 'prop-3' => 'value-3' ],
223  'cache-key-2' => [ 'prop-2' => 'new-value-for-2' ],
224  ],
225  $cache->getCache()
226  );
227  $this->assertEquals( 'new-value-for-2',
228  $cache->get( 'cache-key-2', 'prop-2' )
229  );
230  }
231 
235  public function testBumpExistingKeyToTop() {
236  $cache = new ProcessCacheLRUTestable( 3 );
237  $this->fillCache( $cache, 3 );
238 
239  // Set the very first cache key to a new value
240  $cache->set( "cache-key-1", "prop-1", "new value for 1" );
241  $this->assertEquals(
242  [
243  'cache-key-2' => [ 'prop-2' => 'value-2' ],
244  'cache-key-3' => [ 'prop-3' => 'value-3' ],
245  'cache-key-1' => [ 'prop-1' => 'new value for 1' ],
246  ],
247  $cache->getCache()
248  );
249  }
250 }
251 
256  public $cache = [];
257 
258  public function getCache() {
259  return $this->cache;
260  }
261 
262  public function getEntriesCount() {
263  return count( $this->cache );
264  }
265 }
ProcessCacheLRUTestable\$cache
$cache
Definition: ProcessCacheLRUTest.php:256
ProcessCacheLRUTest\testConstructorGivenInvalidValue
testConstructorGivenInvalidValue( $maxSize)
provideInvalidConstructorArg Wikimedia\Assert\ParameterAssertionException ProcessCacheLRU::__construc...
Definition: ProcessCacheLRUTest.php:76
captcha-old.count
count
Definition: captcha-old.py:225
ProcessCacheLRUTest\provideCacheFilling
static provideCacheFilling()
Provider for testFillingCache.
Definition: ProcessCacheLRUTest.php:147
ProcessCacheLRUTestable\getCache
getCache()
Definition: ProcessCacheLRUTest.php:258
ProcessCacheLRUTest\fillCache
fillCache(&$cache, $numEntries)
Helper to fill a cache object passed by reference.
Definition: ProcessCacheLRUTest.php:25
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
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
ProcessCacheLRUTestable\getEntriesCount
getEntriesCount()
Definition: ProcessCacheLRUTest.php:262
ProcessCacheLRUTest\testFillingCache
testFillingCache( $cacheMaxEntries, $entryToFill, $msg='')
This test that we properly overflow when filling a cache with a sequence of always different cache-ke...
Definition: ProcessCacheLRUTest.php:133
ProcessCacheLRUTest
Test for ProcessCacheLRU class.
Definition: ProcessCacheLRUTest.php:12
ProcessCacheLRUTest\testPhpUnitArrayEquality
testPhpUnitArrayEquality()
Highlight diff between assertEquals and assertNotSame.
Definition: ProcessCacheLRUTest.php:62
ProcessCacheLRUTest\assertCacheEmpty
assertCacheEmpty( $cache, $msg='Cache should be empty')
Helper to verify emptiness of a cache object.
Definition: ProcessCacheLRUTest.php:18
ProcessCacheLRUTest\testAddAndGetAKey
testAddAndGetAKey()
ProcessCacheLRU::get ProcessCacheLRU::set ProcessCacheLRU::has.
Definition: ProcessCacheLRUTest.php:99
ProcessCacheLRUTestable
Overrides some ProcessCacheLRU methods and properties accessibility.
Definition: ProcessCacheLRUTest.php:255
ProcessCacheLRUTest\testReplaceExistingKeyShouldBumpEntryToTop
testReplaceExistingKeyShouldBumpEntryToTop()
Create a cache with only one remaining entry then update the first inserted entry.
Definition: ProcessCacheLRUTest.php:165
ProcessCacheLRUTest\provideInvalidConstructorArg
static provideInvalidConstructorArg()
Value which are forbidden by the constructor.
Definition: ProcessCacheLRUTest.php:83
ProcessCacheLRUTest\testDeleteOldKey
testDeleteOldKey()
ProcessCacheLRU::set ProcessCacheLRU::get.
Definition: ProcessCacheLRUTest.php:114
ProcessCacheLRUTest\testReplaceExistingKeyInAFullCacheShouldBumpToTop
testReplaceExistingKeyInAFullCacheShouldBumpToTop()
This first create a full cache then update the value for the 2nd filled entry.
Definition: ProcessCacheLRUTest.php:211
ProcessCacheLRUTest\testRecentlyAccessedKeyStickIn
testRecentlyAccessedKeyStickIn()
ProcessCacheLRU::get ProcessCacheLRU::set ProcessCacheLRU::has.
Definition: ProcessCacheLRUTest.php:189
$cache
$cache
Definition: mcc.php:33
ProcessCacheLRU
Handles per process caching of items.
Definition: ProcessCacheLRU.php:29
ProcessCacheLRUTest\testBumpExistingKeyToTop
testBumpExistingKeyToTop()
ProcessCacheLRU::set.
Definition: ProcessCacheLRUTest.php:235
ProcessCacheLRUTest\getExpectedCache
getExpectedCache( $cacheMaxEntries, $entryToFill)
Generates an array of what would be expected in cache for a given cache size and a number of entries ...
Definition: ProcessCacheLRUTest.php:36