objectcache: Refresh key in HashBagOStuff::set() for maxKeys eviction
[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 public function testEvictionOrder() {
9 $cache = new HashBagOStuff( array( 'maxKeys' => 10 ) );
10 for ( $i = 0; $i < 10; $i++ ) {
11 $cache->set( "key$i", 1 );
12 $this->assertEquals( 1, $cache->get( "key$i" ) );
13 }
14 for ( $i = 10; $i < 20; $i++ ) {
15 $cache->set( "key$i", 1 );
16 $this->assertEquals( 1, $cache->get( "key$i" ) );
17 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
18 }
19 }
20
21 public function testExpire() {
22 $cache = new HashBagOStuff();
23 $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
24 $cache->set( 'foo', 1 );
25 $cache->set( 'bar', 1, 10 );
26 $cache->set( 'baz', 1, -10 );
27
28 $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
29 // 2 seconds tolerance
30 $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
31 $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
32
33 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
34 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
35 }
36
37 public function testKeyOrder() {
38 $cache = new HashBagOStuff( array( 'maxKeys' => 3 ) );
39
40 foreach ( array( 'foo', 'bar', 'baz' ) as $key ) {
41 $cache->set( $key, 1 );
42 }
43
44 // Set existing key
45 $cache->set( 'foo', 1 );
46
47 // Add a 4th key (beyond the allowed maximum)
48 $cache->set( 'quux', 1 );
49
50 // Foo's life should have been extended over Bar
51 foreach ( array( 'foo', 'baz', 'quux' ) as $key ) {
52 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
53 }
54 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
55 }
56 }