Merge "Send integer ms to DB lag time guage instead of seconds"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / HashBagOStuffTest.php
1 <?php
2
3 /**
4 * @group BagOStuff
5 */
6 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
7
8 /**
9 * @covers HashBagOStuff::__construct
10 */
11 public function testConstruct() {
12 $this->assertInstanceOf(
13 HashBagOStuff::class,
14 new HashBagOStuff()
15 );
16 }
17
18 /**
19 * @covers HashBagOStuff::__construct
20 * @expectedException InvalidArgumentException
21 */
22 public function testConstructBadZero() {
23 $cache = new HashBagOStuff( [ 'maxKeys' => 0 ] );
24 }
25
26 /**
27 * @covers HashBagOStuff::__construct
28 * @expectedException InvalidArgumentException
29 */
30 public function testConstructBadNeg() {
31 $cache = new HashBagOStuff( [ 'maxKeys' => -1 ] );
32 }
33
34 /**
35 * @covers HashBagOStuff::__construct
36 * @expectedException InvalidArgumentException
37 */
38 public function testConstructBadType() {
39 $cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] );
40 }
41
42 /**
43 * @covers HashBagOStuff::delete
44 */
45 public function testDelete() {
46 $cache = new HashBagOStuff();
47 for ( $i = 0; $i < 10; $i++ ) {
48 $cache->set( "key$i", 1 );
49 $this->assertEquals( 1, $cache->get( "key$i" ) );
50 $cache->delete( "key$i" );
51 $this->assertEquals( false, $cache->get( "key$i" ) );
52 }
53 }
54
55 /**
56 * @covers HashBagOStuff::clear
57 */
58 public function testClear() {
59 $cache = new HashBagOStuff();
60 for ( $i = 0; $i < 10; $i++ ) {
61 $cache->set( "key$i", 1 );
62 $this->assertEquals( 1, $cache->get( "key$i" ) );
63 }
64 $cache->clear();
65 for ( $i = 0; $i < 10; $i++ ) {
66 $this->assertEquals( false, $cache->get( "key$i" ) );
67 }
68 }
69
70 /**
71 * @covers HashBagOStuff::doGet
72 * @covers HashBagOStuff::expire
73 */
74 public function testExpire() {
75 $cache = new HashBagOStuff();
76 $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
77 $cache->set( 'foo', 1 );
78 $cache->set( 'bar', 1, 10 );
79 $cache->set( 'baz', 1, -10 );
80
81 $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
82 // 2 seconds tolerance
83 $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
84 $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
85
86 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
87 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
88 }
89
90 /**
91 * Ensure maxKeys eviction prefers keeping new keys.
92 *
93 * @covers HashBagOStuff::set
94 */
95 public function testEvictionAdd() {
96 $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
97 for ( $i = 0; $i < 10; $i++ ) {
98 $cache->set( "key$i", 1 );
99 $this->assertEquals( 1, $cache->get( "key$i" ) );
100 }
101 for ( $i = 10; $i < 20; $i++ ) {
102 $cache->set( "key$i", 1 );
103 $this->assertEquals( 1, $cache->get( "key$i" ) );
104 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
105 }
106 }
107
108 /**
109 * Ensure maxKeys eviction prefers recently set keys
110 * even if the keys pre-exist.
111 *
112 * @covers HashBagOStuff::set
113 */
114 public function testEvictionSet() {
115 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
116
117 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
118 $cache->set( $key, 1 );
119 }
120
121 // Set existing key
122 $cache->set( 'foo', 1 );
123
124 // Add a 4th key (beyond the allowed maximum)
125 $cache->set( 'quux', 1 );
126
127 // Foo's life should have been extended over Bar
128 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
129 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
130 }
131 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
132 }
133
134 /**
135 * Ensure maxKeys eviction prefers recently retrieved keys (LRU).
136 *
137 * @covers HashBagOStuff::doGet
138 * @covers HashBagOStuff::hasKey
139 */
140 public function testEvictionGet() {
141 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
142
143 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
144 $cache->set( $key, 1 );
145 }
146
147 // Get existing key
148 $cache->get( 'foo', 1 );
149
150 // Add a 4th key (beyond the allowed maximum)
151 $cache->set( 'quux', 1 );
152
153 // Foo's life should have been extended over Bar
154 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
155 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
156 }
157 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
158 }
159 }