MediaWiki REL1_33
HashBagOStuffTest.php
Go to the documentation of this file.
1<?php
2
4
8class HashBagOStuffTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
15 public function testConstruct() {
16 $this->assertInstanceOf(
17 HashBagOStuff::class,
18 new HashBagOStuff()
19 );
20 }
21
26 public function testConstructBadZero() {
27 $cache = new HashBagOStuff( [ 'maxKeys' => 0 ] );
28 }
29
34 public function testConstructBadNeg() {
35 $cache = new HashBagOStuff( [ 'maxKeys' => -1 ] );
36 }
37
42 public function testConstructBadType() {
43 $cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] );
44 }
45
49 public function testDelete() {
50 $cache = new HashBagOStuff();
51 for ( $i = 0; $i < 10; $i++ ) {
52 $cache->set( "key$i", 1 );
53 $this->assertEquals( 1, $cache->get( "key$i" ) );
54 $cache->delete( "key$i" );
55 $this->assertEquals( false, $cache->get( "key$i" ) );
56 }
57 }
58
62 public function testClear() {
63 $cache = new HashBagOStuff();
64 for ( $i = 0; $i < 10; $i++ ) {
65 $cache->set( "key$i", 1 );
66 $this->assertEquals( 1, $cache->get( "key$i" ) );
67 }
68 $cache->clear();
69 for ( $i = 0; $i < 10; $i++ ) {
70 $this->assertEquals( false, $cache->get( "key$i" ) );
71 }
72 }
73
78 public function testExpire() {
79 $cache = new HashBagOStuff();
80 $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
81 $cache->set( 'foo', 1 );
82 $cache->set( 'bar', 1, 10 );
83 $cache->set( 'baz', 1, -10 );
84
85 $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
86 // 2 seconds tolerance
87 $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
88 $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
89
90 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
91 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
92 }
93
99 public function testEvictionAdd() {
100 $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
101 for ( $i = 0; $i < 10; $i++ ) {
102 $cache->set( "key$i", 1 );
103 $this->assertEquals( 1, $cache->get( "key$i" ) );
104 }
105 for ( $i = 10; $i < 20; $i++ ) {
106 $cache->set( "key$i", 1 );
107 $this->assertEquals( 1, $cache->get( "key$i" ) );
108 $this->assertEquals( false, $cache->get( "key" . ( $i - 10 ) ) );
109 }
110 }
111
118 public function testEvictionSet() {
119 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
120
121 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
122 $cache->set( $key, 1 );
123 }
124
125 // Set existing key
126 $cache->set( 'foo', 1 );
127
128 // Add a 4th key (beyond the allowed maximum)
129 $cache->set( 'quux', 1 );
130
131 // Foo's life should have been extended over Bar
132 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
133 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
134 }
135 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
136 }
137
144 public function testEvictionGet() {
145 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
146
147 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
148 $cache->set( $key, 1 );
149 }
150
151 // Get existing key
152 $cache->get( 'foo', 1 );
153
154 // Add a 4th key (beyond the allowed maximum)
155 $cache->set( 'quux', 1 );
156
157 // Foo's life should have been extended over Bar
158 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
159 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
160 }
161 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
162 }
163}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
testConstructBadType()
HashBagOStuff::__construct InvalidArgumentException.
testConstructBadZero()
HashBagOStuff::__construct InvalidArgumentException.
testEvictionGet()
Ensure maxKeys eviction prefers recently retrieved keys (LRU).
testEvictionAdd()
Ensure maxKeys eviction prefers keeping new keys.
testConstruct()
HashBagOStuff::__construct.
testDelete()
HashBagOStuff::delete.
testExpire()
HashBagOStuff::doGet HashBagOStuff::expire.
testEvictionSet()
Ensure maxKeys eviction prefers recently set keys even if the keys pre-exist.
testClear()
HashBagOStuff::clear.
testConstructBadNeg()
HashBagOStuff::__construct InvalidArgumentException.
Simple store for keeping values in an associative array for the current process.
$cache
Definition mcc.php:33