MediaWiki  1.23.2
ProcessCacheLRUTest.php
Go to the documentation of this file.
1 <?php
2 
13 
18  function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
19  $this->assertAttributeEquals( array(), 'cache', $cache, $msg );
20  }
21 
25  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  function getExpectedCache( $cacheMaxEntries, $entryToFill ) {
37  $expected = array();
38 
39  if ( $entryToFill === 0 ) {
40  # The cache is empty!
41  return array();
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"] = array( "prop-$i" => "value-$i" );
54  }
55 
56  return $expected;
57  }
58 
62  public function testPhpUnitArrayEquality() {
63  $one = array( 'A' => 1, 'B' => 2 );
64  $two = array( 'B' => 2, 'A' => 1 );
65  $this->assertEquals( $one, $two ); // ==
66  $this->assertNotSame( $one, $two ); // ===
67  }
68 
73  public function testConstructorGivenInvalidValue( $maxSize ) {
74  new ProcessCacheLRUTestable( $maxSize );
75  }
76 
80  public static function provideInvalidConstructorArg() {
81  return array(
82  array( null ),
83  array( array() ),
84  array( new stdClass() ),
85  array( 0 ),
86  array( '5' ),
87  array( -1 ),
88  );
89  }
90 
91  public function testAddAndGetAKey() {
92  $oneCache = new ProcessCacheLRUTestable( 1 );
93  $this->assertCacheEmpty( $oneCache );
94 
95  // First set just one value
96  $oneCache->set( 'cache-key', 'prop1', 'value1' );
97  $this->assertEquals( 1, $oneCache->getEntriesCount() );
98  $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
99  $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
100  }
101 
102  public function testDeleteOldKey() {
103  $oneCache = new ProcessCacheLRUTestable( 1 );
104  $this->assertCacheEmpty( $oneCache );
105 
106  $oneCache->set( 'cache-key', 'prop1', 'value1' );
107  $oneCache->set( 'cache-key', 'prop1', 'value2' );
108  $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
109  }
110 
120  public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
121  $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
122  $this->fillCache( $cache, $entryToFill );
123 
124  $this->assertSame(
125  $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
126  $cache->getCache(),
127  "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
128  );
129  }
130 
134  public static function provideCacheFilling() {
135  // ($cacheMaxEntries, $entryToFill, $msg='')
136  return array(
137  array( 1, 0 ),
138  array( 1, 1 ),
139  array( 1, 2 ), # overflow
140  array( 5, 33 ), # overflow
141  );
142  }
143 
149  $maxEntries = 3;
150 
151  $cache = new ProcessCacheLRUTestable( $maxEntries );
152  // Fill cache leaving just one remaining slot
153  $this->fillCache( $cache, $maxEntries - 1 );
154 
155  // Set an existing cache key
156  $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
157 
158  $this->assertSame(
159  array(
160  'cache-key-2' => array( 'prop-2' => 'value-2' ),
161  'cache-key-1' => array( 'prop-1' => 'new-value-for-1' ),
162  ),
163  $cache->getCache()
164  );
165  }
166 
167  public function testRecentlyAccessedKeyStickIn() {
168  $cache = new ProcessCacheLRUTestable( 2 );
169  $cache->set( 'first', 'prop1', 'value1' );
170  $cache->set( 'second', 'prop2', 'value2' );
171 
172  // Get first
173  $cache->get( 'first', 'prop1' );
174  // Cache a third value, should invalidate the least used one
175  $cache->set( 'third', 'prop3', 'value3' );
176 
177  $this->assertFalse( $cache->has( 'second', 'prop2' ) );
178  }
179 
187  $maxEntries = 3;
188 
189  $cache = new ProcessCacheLRUTestable( $maxEntries );
190  $this->fillCache( $cache, $maxEntries );
191 
192  // Set an existing cache key
193  $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
194  $this->assertSame(
195  array(
196  'cache-key-1' => array( 'prop-1' => 'value-1' ),
197  'cache-key-3' => array( 'prop-3' => 'value-3' ),
198  'cache-key-2' => array( 'prop-2' => 'new-value-for-2' ),
199  ),
200  $cache->getCache()
201  );
202  $this->assertEquals( 'new-value-for-2',
203  $cache->get( 'cache-key-2', 'prop-2' )
204  );
205  }
206 
207  public function testBumpExistingKeyToTop() {
208  $cache = new ProcessCacheLRUTestable( 3 );
209  $this->fillCache( $cache, 3 );
210 
211  // Set the very first cache key to a new value
212  $cache->set( "cache-key-1", "prop-1", "new value for 1" );
213  $this->assertEquals(
214  array(
215  'cache-key-2' => array( 'prop-2' => 'value-2' ),
216  'cache-key-3' => array( 'prop-3' => 'value-3' ),
217  'cache-key-1' => array( 'prop-1' => 'new value for 1' ),
218  ),
219  $cache->getCache()
220  );
221  }
222 }
223 
228  public $cache = array();
229 
230  public function getCache() {
231  return $this->cache;
232  }
233 
234  public function getEntriesCount() {
235  return count( $this->cache );
236  }
237 }
ProcessCacheLRUTestable\$cache
$cache
Definition: ProcessCacheLRUTest.php:228
ProcessCacheLRUTest\testConstructorGivenInvalidValue
testConstructorGivenInvalidValue( $maxSize)
@dataProvider provideInvalidConstructorArg @expectedException UnexpectedValueException
Definition: ProcessCacheLRUTest.php:73
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ProcessCacheLRUTest\provideCacheFilling
static provideCacheFilling()
Provider for testFillingCache.
Definition: ProcessCacheLRUTest.php:134
ProcessCacheLRUTestable\getCache
getCache()
Definition: ProcessCacheLRUTest.php:230
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
ProcessCacheLRUTestable\getEntriesCount
getEntriesCount()
Definition: ProcessCacheLRUTest.php:234
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:120
ProcessCacheLRUTest
Test for ProcessCacheLRU class.
Definition: ProcessCacheLRUTest.php:12
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
ProcessCacheLRUTest\testPhpUnitArrayEquality
testPhpUnitArrayEquality()
Highlight diff between assertEquals and assertNotSame.
Definition: ProcessCacheLRUTest.php:62
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ProcessCacheLRUTest\assertCacheEmpty
assertCacheEmpty( $cache, $msg='Cache should be empty')
Helper to verify emptiness of a cache object.
Definition: ProcessCacheLRUTest.php:18
ProcessCacheLRUTest\testAddAndGetAKey
testAddAndGetAKey()
Definition: ProcessCacheLRUTest.php:91
ProcessCacheLRUTestable
Overrides some ProcessCacheLRU methods and properties accessibility.
Definition: ProcessCacheLRUTest.php:227
ProcessCacheLRUTest\testReplaceExistingKeyShouldBumpEntryToTop
testReplaceExistingKeyShouldBumpEntryToTop()
Create a cache with only one remaining entry then update the first inserted entry.
Definition: ProcessCacheLRUTest.php:148
ProcessCacheLRUTest\provideInvalidConstructorArg
static provideInvalidConstructorArg()
Value which are forbidden by the constructor.
Definition: ProcessCacheLRUTest.php:80
ProcessCacheLRUTest\testDeleteOldKey
testDeleteOldKey()
Definition: ProcessCacheLRUTest.php:102
ProcessCacheLRUTest\testReplaceExistingKeyInAFullCacheShouldBumpToTop
testReplaceExistingKeyInAFullCacheShouldBumpToTop()
This first create a full cache then update the value for the 2nd filled entry.
Definition: ProcessCacheLRUTest.php:186
ProcessCacheLRUTest\testRecentlyAccessedKeyStickIn
testRecentlyAccessedKeyStickIn()
Definition: ProcessCacheLRUTest.php:167
$cache
$cache
Definition: mcc.php:32
ProcessCacheLRU
Handles per process caching of items.
Definition: ProcessCacheLRU.php:28
ProcessCacheLRUTest\testBumpExistingKeyToTop
testBumpExistingKeyToTop()
Definition: ProcessCacheLRUTest.php:207
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