MediaWiki REL1_33
HashBagOStuffTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$cache
Definition mcc.php:33