rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / libs / objectcache / HashBagOStuffTest.php
blobb2278c3437450ae6550d4c147e62910fe8ef4ef8
1 <?php
3 use Wikimedia\TestingAccessWrapper;
5 /**
6 * @group BagOStuff
7 */
8 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
10 /**
11 * @covers HashBagOStuff::__construct
13 public function testConstruct() {
14 $this->assertInstanceOf(
15 HashBagOStuff::class,
16 new HashBagOStuff()
20 /**
21 * @covers HashBagOStuff::__construct
22 * @expectedException InvalidArgumentException
24 public function testConstructBadZero() {
25 $cache = new HashBagOStuff( [ 'maxKeys' => 0 ] );
28 /**
29 * @covers HashBagOStuff::__construct
30 * @expectedException InvalidArgumentException
32 public function testConstructBadNeg() {
33 $cache = new HashBagOStuff( [ 'maxKeys' => -1 ] );
36 /**
37 * @covers HashBagOStuff::__construct
38 * @expectedException InvalidArgumentException
40 public function testConstructBadType() {
41 $cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] );
44 /**
45 * @covers HashBagOStuff::delete
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" ) );
57 /**
58 * @covers HashBagOStuff::clear
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" ) );
66 $cache->clear();
67 for ( $i = 0; $i < 10; $i++ ) {
68 $this->assertEquals( false, $cache->get( "key$i" ) );
72 /**
73 * @covers HashBagOStuff::doGet
74 * @covers HashBagOStuff::expire
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 );
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 );
88 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
89 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
92 /**
93 * Ensure maxKeys eviction prefers keeping new keys.
95 * @covers HashBagOStuff::set
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" ) );
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 ) );
111 * Ensure maxKeys eviction prefers recently set keys
112 * even if the keys pre-exist.
114 * @covers HashBagOStuff::set
116 public function testEvictionSet() {
117 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
119 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
120 $cache->set( $key, 1 );
123 // Set existing key
124 $cache->set( 'foo', 1 );
126 // Add a 4th key (beyond the allowed maximum)
127 $cache->set( 'quux', 1 );
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" );
133 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
137 * Ensure maxKeys eviction prefers recently retrieved keys (LRU).
139 * @covers HashBagOStuff::doGet
140 * @covers HashBagOStuff::hasKey
142 public function testEvictionGet() {
143 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
145 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
146 $cache->set( $key, 1 );
149 // Get existing key
150 $cache->get( 'foo', 1 );
152 // Add a 4th key (beyond the allowed maximum)
153 $cache->set( 'quux', 1 );
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" );
159 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );