MediaWiki REL1_32
ProcessCacheLRUTest.php
Go to the documentation of this file.
1<?php
2
12class ProcessCacheLRUTest extends PHPUnit\Framework\TestCase {
13
14 use MediaWikiCoversValidator;
15
20 protected function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
21 $this->assertEquals( 0, $cache->getEntriesCount(), $msg );
22 }
23
27 protected function fillCache( &$cache, $numEntries ) {
28 // Fill cache with three values
29 for ( $i = 1; $i <= $numEntries; $i++ ) {
30 $cache->set( "cache-key-$i", "prop-$i", "value-$i" );
31 }
32 }
33
38 protected function getExpectedCache( $cacheMaxEntries, $entryToFill ) {
39 $expected = [];
40
41 if ( $entryToFill === 0 ) {
42 // The cache is empty!
43 return [];
44 } elseif ( $entryToFill <= $cacheMaxEntries ) {
45 // Cache is not fully filled
46 $firstKey = 1;
47 } else {
48 // Cache overflowed
49 $firstKey = 1 + $entryToFill - $cacheMaxEntries;
50 }
51
52 $lastKey = $entryToFill;
53
54 for ( $i = $firstKey; $i <= $lastKey; $i++ ) {
55 $expected["cache-key-$i"] = [ "prop-$i" => "value-$i" ];
56 }
57
58 return $expected;
59 }
60
65 public function testPhpUnitArrayEquality() {
66 $one = [ 'A' => 1, 'B' => 2 ];
67 $two = [ 'B' => 2, 'A' => 1 ];
68 // ==
69 $this->assertEquals( $one, $two );
70 // ===
71 $this->assertNotSame( $one, $two );
72 }
73
79 public function testConstructorGivenInvalidValue( $maxSize ) {
80 new ProcessCacheLRUTestable( $maxSize );
81 }
82
86 public static function provideInvalidConstructorArg() {
87 return [
88 [ null ],
89 [ [] ],
90 [ new stdClass() ],
91 [ 0 ],
92 [ '5' ],
93 [ -1 ],
94 ];
95 }
96
102 public function testAddAndGetAKey() {
103 $oneCache = new ProcessCacheLRUTestable( 1 );
104 $this->assertCacheEmpty( $oneCache );
105
106 // First set just one value
107 $oneCache->set( 'cache-key', 'prop1', 'value1' );
108 $this->assertEquals( 1, $oneCache->getEntriesCount() );
109 $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
110 $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
111 }
112
117 public function testDeleteOldKey() {
118 $oneCache = new ProcessCacheLRUTestable( 1 );
119 $this->assertCacheEmpty( $oneCache );
120
121 $oneCache->set( 'cache-key', 'prop1', 'value1' );
122 $oneCache->set( 'cache-key', 'prop1', 'value2' );
123 $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
124 }
125
136 public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
137 $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
138 $this->fillCache( $cache, $entryToFill );
139
140 $this->assertSame(
141 $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
142 $cache->getCache(),
143 "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
144 );
145 }
146
150 public static function provideCacheFilling() {
151 // ($cacheMaxEntries, $entryToFill, $msg='')
152 return [
153 [ 1, 0 ],
154 [ 1, 1 ],
155 // overflow
156 [ 1, 2 ],
157 // overflow
158 [ 5, 33 ],
159 ];
160 }
161
169 $maxEntries = 3;
170
171 $cache = new ProcessCacheLRUTestable( $maxEntries );
172 // Fill cache leaving just one remaining slot
173 $this->fillCache( $cache, $maxEntries - 1 );
174
175 // Set an existing cache key
176 $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
177
178 $this->assertSame(
179 [
180 'cache-key-2' => [ 'prop-2' => 'value-2' ],
181 'cache-key-1' => [ 'prop-1' => 'new-value-for-1' ],
182 ],
183 $cache->getCache()
184 );
185 }
186
194 $cache->set( 'first', 'prop1', 'value1' );
195 $cache->set( 'second', 'prop2', 'value2' );
196
197 // Get first
198 $cache->get( 'first', 'prop1' );
199 // Cache a third value, should invalidate the least used one
200 $cache->set( 'third', 'prop3', 'value3' );
201
202 $this->assertFalse( $cache->has( 'second', 'prop2' ) );
203 }
204
215 $maxEntries = 3;
216
217 $cache = new ProcessCacheLRUTestable( $maxEntries );
218 $this->fillCache( $cache, $maxEntries );
219
220 // Set an existing cache key
221 $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
222 $this->assertSame(
223 [
224 'cache-key-1' => [ 'prop-1' => 'value-1' ],
225 'cache-key-3' => [ 'prop-3' => 'value-3' ],
226 'cache-key-2' => [ 'prop-2' => 'new-value-for-2' ],
227 ],
228 $cache->getCache()
229 );
230 $this->assertEquals( 'new-value-for-2',
231 $cache->get( 'cache-key-2', 'prop-2' )
232 );
233 }
234
238 public function testBumpExistingKeyToTop() {
240 $this->fillCache( $cache, 3 );
241
242 // Set the very first cache key to a new value
243 $cache->set( "cache-key-1", "prop-1", "new value for 1" );
244 $this->assertEquals(
245 [
246 'cache-key-2' => [ 'prop-2' => 'value-2' ],
247 'cache-key-3' => [ 'prop-3' => 'value-3' ],
248 'cache-key-1' => [ 'prop-1' => 'new value for 1' ],
249 ],
250 $cache->getCache()
251 );
252 }
253}
254
259 public function getCache() {
260 return $this->cache->toArray();
261 }
262
263 public function getEntriesCount() {
264 return count( $this->cache->toArray() );
265 }
266}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Test for ProcessCacheLRU class.
testPhpUnitArrayEquality()
Highlight diff between assertEquals and assertNotSame @coversNothing.
testReplaceExistingKeyInAFullCacheShouldBumpToTop()
This first create a full cache then update the value for the 2nd filled entry.
testFillingCache( $cacheMaxEntries, $entryToFill, $msg='')
This test that we properly overflow when filling a cache with a sequence of always different cache-ke...
getExpectedCache( $cacheMaxEntries, $entryToFill)
Generates an array of what would be expected in cache for a given cache size and a number of entries ...
testReplaceExistingKeyShouldBumpEntryToTop()
Create a cache with only one remaining entry then update the first inserted entry.
fillCache(&$cache, $numEntries)
Helper to fill a cache object passed by reference.
testAddAndGetAKey()
ProcessCacheLRU::get ProcessCacheLRU::set ProcessCacheLRU::has.
testConstructorGivenInvalidValue( $maxSize)
provideInvalidConstructorArg Wikimedia\Assert\ParameterAssertionException ProcessCacheLRU::__construc...
assertCacheEmpty( $cache, $msg='Cache should be empty')
Helper to verify emptiness of a cache object.
static provideInvalidConstructorArg()
Value which are forbidden by the constructor.
testDeleteOldKey()
ProcessCacheLRU::set ProcessCacheLRU::get.
testBumpExistingKeyToTop()
ProcessCacheLRU::set.
testRecentlyAccessedKeyStickIn()
ProcessCacheLRU::get ProcessCacheLRU::set ProcessCacheLRU::has.
static provideCacheFilling()
Provider for testFillingCache.
Overrides some ProcessCacheLRU methods and properties accessibility.
Class for process caching individual properties of expiring items.
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:37
you have access to all of the normal MediaWiki so you can get a DB use the cache
$cache
Definition mcc.php:33