Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / libs / objectcache / HashBagOStuffTest.php
blobc4db0cf8bf1b2504c8b51071a512f6c936d1e6f5
1 <?php
3 /**
4 * @group BagOStuff
5 */
6 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
8 /**
9 * @covers HashBagOStuff::delete
11 public function testDelete() {
12 $cache = new HashBagOStuff();
13 for ( $i = 0; $i < 10; $i++ ) {
14 $cache->set( "key$i", 1 );
15 $this->assertEquals( 1, $cache->get( "key$i" ) );
16 $cache->delete( "key$i" );
17 $this->assertEquals( false, $cache->get( "key$i" ) );
21 /**
22 * @covers HashBagOStuff::clear
24 public function testClear() {
25 $cache = new HashBagOStuff();
26 for ( $i = 0; $i < 10; $i++ ) {
27 $cache->set( "key$i", 1 );
28 $this->assertEquals( 1, $cache->get( "key$i" ) );
30 $cache->clear();
31 for ( $i = 0; $i < 10; $i++ ) {
32 $this->assertEquals( false, $cache->get( "key$i" ) );
36 /**
37 * @covers HashBagOStuff::doGet
38 * @covers HashBagOStuff::expire
40 public function testExpire() {
41 $cache = new HashBagOStuff();
42 $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
43 $cache->set( 'foo', 1 );
44 $cache->set( 'bar', 1, 10 );
45 $cache->set( 'baz', 1, -10 );
47 $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
48 // 2 seconds tolerance
49 $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
50 $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
52 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
53 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
56 /**
57 * Ensure maxKeys eviction prefers keeping new keys.
59 * @covers HashBagOStuff::__construct
60 * @covers HashBagOStuff::set
62 public function testEvictionAdd() {
63 $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
64 for ( $i = 0; $i < 10; $i++ ) {
65 $cache->set( "key$i", 1 );
66 $this->assertEquals( 1, $cache->get( "key$i" ) );
68 for ( $i = 10; $i < 20; $i++ ) {
69 $cache->set( "key$i", 1 );
70 $this->assertEquals( 1, $cache->get( "key$i" ) );
71 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
75 /**
76 * Ensure maxKeys eviction prefers recently set keys
77 * even if the keys pre-exist.
79 * @covers HashBagOStuff::__construct
80 * @covers HashBagOStuff::set
82 public function testEvictionSet() {
83 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
85 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
86 $cache->set( $key, 1 );
89 // Set existing key
90 $cache->set( 'foo', 1 );
92 // Add a 4th key (beyond the allowed maximum)
93 $cache->set( 'quux', 1 );
95 // Foo's life should have been extended over Bar
96 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
97 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
99 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
103 * Ensure maxKeys eviction prefers recently retrieved keys (LRU).
105 * @covers HashBagOStuff::__construct
106 * @covers HashBagOStuff::doGet
107 * @covers HashBagOStuff::hasKey
109 public function testEvictionGet() {
110 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
112 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
113 $cache->set( $key, 1 );
116 // Get existing key
117 $cache->get( 'foo', 1 );
119 // Add a 4th key (beyond the allowed maximum)
120 $cache->set( 'quux', 1 );
122 // Foo's life should have been extended over Bar
123 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
124 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
126 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );