MediaWiki  1.30.0
HashBagOStuffTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
8 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
9 
13  public function testConstruct() {
14  $this->assertInstanceOf(
16  new HashBagOStuff()
17  );
18  }
19 
24  public function testConstructBadZero() {
25  $cache = new HashBagOStuff( [ 'maxKeys' => 0 ] );
26  }
27 
32  public function testConstructBadNeg() {
33  $cache = new HashBagOStuff( [ 'maxKeys' => -1 ] );
34  }
35 
40  public function testConstructBadType() {
41  $cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] );
42  }
43 
47  public function testDelete() {
48  $cache = new HashBagOStuff();
49  for ( $i = 0; $i < 10; $i++ ) {
50  $cache->set( "key$i", 1 );
51  $this->assertEquals( 1, $cache->get( "key$i" ) );
52  $cache->delete( "key$i" );
53  $this->assertEquals( false, $cache->get( "key$i" ) );
54  }
55  }
56 
60  public function testClear() {
61  $cache = new HashBagOStuff();
62  for ( $i = 0; $i < 10; $i++ ) {
63  $cache->set( "key$i", 1 );
64  $this->assertEquals( 1, $cache->get( "key$i" ) );
65  }
66  $cache->clear();
67  for ( $i = 0; $i < 10; $i++ ) {
68  $this->assertEquals( false, $cache->get( "key$i" ) );
69  }
70  }
71 
76  public function testExpire() {
77  $cache = new HashBagOStuff();
78  $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
79  $cache->set( 'foo', 1 );
80  $cache->set( 'bar', 1, 10 );
81  $cache->set( 'baz', 1, -10 );
82 
83  $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
84  // 2 seconds tolerance
85  $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
86  $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
87 
88  $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
89  $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
90  }
91 
97  public function testEvictionAdd() {
98  $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
99  for ( $i = 0; $i < 10; $i++ ) {
100  $cache->set( "key$i", 1 );
101  $this->assertEquals( 1, $cache->get( "key$i" ) );
102  }
103  for ( $i = 10; $i < 20; $i++ ) {
104  $cache->set( "key$i", 1 );
105  $this->assertEquals( 1, $cache->get( "key$i" ) );
106  $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
107  }
108  }
109 
116  public function testEvictionSet() {
117  $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
118 
119  foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
120  $cache->set( $key, 1 );
121  }
122 
123  // Set existing key
124  $cache->set( 'foo', 1 );
125 
126  // Add a 4th key (beyond the allowed maximum)
127  $cache->set( 'quux', 1 );
128 
129  // Foo's life should have been extended over Bar
130  foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
131  $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
132  }
133  $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
134  }
135 
142  public function testEvictionGet() {
143  $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
144 
145  foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
146  $cache->set( $key, 1 );
147  }
148 
149  // Get existing key
150  $cache->get( 'foo', 1 );
151 
152  // Add a 4th key (beyond the allowed maximum)
153  $cache->set( 'quux', 1 );
154 
155  // Foo's life should have been extended over Bar
156  foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
157  $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
158  }
159  $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
160  }
161 }
HashBagOStuffTest\testConstructBadType
testConstructBadType()
HashBagOStuff::__construct InvalidArgumentException.
Definition: HashBagOStuffTest.php:40
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
HashBagOStuffTest\testExpire
testExpire()
HashBagOStuff::doGet HashBagOStuff::expire.
Definition: HashBagOStuffTest.php:76
HashBagOStuffTest\testConstruct
testConstruct()
HashBagOStuff::__construct.
Definition: HashBagOStuffTest.php:13
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
HashBagOStuffTest\testEvictionSet
testEvictionSet()
Ensure maxKeys eviction prefers recently set keys even if the keys pre-exist.
Definition: HashBagOStuffTest.php:116
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
HashBagOStuffTest\testConstructBadZero
testConstructBadZero()
HashBagOStuff::__construct InvalidArgumentException.
Definition: HashBagOStuffTest.php:24
HashBagOStuffTest\testClear
testClear()
HashBagOStuff::clear.
Definition: HashBagOStuffTest.php:60
HashBagOStuffTest\testEvictionAdd
testEvictionAdd()
Ensure maxKeys eviction prefers keeping new keys.
Definition: HashBagOStuffTest.php:97
$cache
$cache
Definition: mcc.php:33
HashBagOStuffTest\testDelete
testDelete()
HashBagOStuff::delete.
Definition: HashBagOStuffTest.php:47
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
HashBagOStuffTest
BagOStuff.
Definition: HashBagOStuffTest.php:8
HashBagOStuffTest\testEvictionGet
testEvictionGet()
Ensure maxKeys eviction prefers recently retrieved keys (LRU).
Definition: HashBagOStuffTest.php:142
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
HashBagOStuffTest\testConstructBadNeg
testConstructBadNeg()
HashBagOStuff::__construct InvalidArgumentException.
Definition: HashBagOStuffTest.php:32