File: MapCacheLRUTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (316 lines) | stat: -rw-r--r-- 8,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php

namespace Wikimedia\Tests;

use MapCacheLRU;
use MediaWikiCoversValidator;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

/**
 * @group Cache
 * @covers \MapCacheLRU
 */
class MapCacheLRUTest extends TestCase {

	use MediaWikiCoversValidator;

	public function testArrayConversion() {
		$raw = [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ];
		$cache = MapCacheLRU::newFromArray( $raw, 3 );

		$this->assertEquals( 3, $cache->getMaxSize() );
		$this->assertSame( true, $cache->has( 'a' ) );
		$this->assertSame( true, $cache->has( 'b' ) );
		$this->assertSame( true, $cache->has( 'c' ) );
		$this->assertSame( 1, $cache->get( 'a' ) );
		$this->assertSame( 2, $cache->get( 'b' ) );
		$this->assertSame( 3, $cache->get( 'c' ) );

		$this->assertSame(
			[ 'a' => 1, 'b' => 2, 'c' => 3 ],
			$cache->toArray()
		);
		$this->assertSame(
			[ 'a', 'b', 'c' ],
			$cache->getAllKeys()
		);

		$cache->clear( 'a' );
		$this->assertSame(
			[ 'b' => 2, 'c' => 3 ],
			$cache->toArray()
		);

		$cache->clear();
		$this->assertSame(
			[],
			$cache->toArray()
		);

		$cache = MapCacheLRU::newFromArray( [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ], 4 );
		$cache->setMaxSize( 3 );
		$this->assertSame(
			[ 'c' => 3, 'b' => 2, 'a' => 1 ],
			$cache->toArray()
		);
	}

	public function testSerialize() {
		$cache = new MapCacheLRU( 3 );
		$cache->set( 'a', 1 );
		$cache->set( 'b', 2 );
		$cache->set( 'c', 3 );

		$string = serialize( $cache );
		$cache = unserialize( $string );
		$this->assertSame(
			[ 'a' => 1, 'b' => 2, 'c' => 3 ],
			$cache->toArray(),
			'entries are preserved'
		);

		$cache->set( 'd', 4 );
		$this->assertSame(
			[ 'b' => 2, 'c' => 3, 'd' => 4 ],
			$cache->toArray(),
			'maxKeys is preserved'
		);
	}

	public function testGetWithSetCallback() {
		$cache = new MapCacheLRU( 3 );
		$i = 0;
		$fn = static function () use ( &$i ) {
			$i++;
			return $i;
		};

		$this->assertSame( null, $cache->get( 'a' ), 'initial' );
		$this->assertSame( 1, $cache->getWithSetCallback( 'a', $fn ), 'first gen' );
		$this->assertSame( 1, $cache->getWithSetCallback( 'a', $fn ), 'getWithSet hit' );
		$this->assertSame( 1, $cache->get( 'a' ), 'plain get hit' );
		$cache->clear( 'a' );
		$this->assertSame( 2, $cache->getWithSetCallback( 'a', $fn ), 'second gen' );
	}

	public function testMissing() {
		$raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
		$cache = MapCacheLRU::newFromArray( $raw, 3 );

		$this->assertFalse( $cache->has( 'd' ) );
		$this->assertNull( $cache->get( 'd' ) );
		$this->assertNull( $cache->get( 'd', 0.0, null ) );
		$this->assertFalse( $cache->get( 'd', 0.0, false ) );
	}

	public function testLRU() {
		$raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
		$cache = MapCacheLRU::newFromArray( $raw, 3 );

		$this->assertSame( true, $cache->has( 'c' ) );
		$this->assertSame(
			[ 'a' => 1, 'b' => 2, 'c' => 3 ],
			$cache->toArray()
		);

		$this->assertSame( 3, $cache->get( 'c' ) );
		$this->assertSame(
			[ 'a' => 1, 'b' => 2, 'c' => 3 ],
			$cache->toArray()
		);

		$this->assertSame( 1, $cache->get( 'a' ) );
		$this->assertSame(
			[ 'b' => 2, 'c' => 3, 'a' => 1 ],
			$cache->toArray()
		);

		$cache->set( 'a', 1 );
		$this->assertSame(
			[ 'b' => 2, 'c' => 3, 'a' => 1 ],
			$cache->toArray()
		);

		$cache->set( 'b', 22 );
		$this->assertSame(
			[ 'c' => 3, 'a' => 1, 'b' => 22 ],
			$cache->toArray()
		);

		$cache->set( 'd', 4 );
		$this->assertSame(
			[ 'a' => 1, 'b' => 22, 'd' => 4 ],
			$cache->toArray()
		);

		$cache->set( 'e', 5, 0.33 );
		$this->assertSame(
			[ 'e' => 5, 'b' => 22, 'd' => 4 ],
			$cache->toArray()
		);

		$cache->set( 'f', 6, 0.66 );
		$this->assertSame(
			[ 'b' => 22, 'f' => 6, 'd' => 4 ],
			$cache->toArray()
		);

		$cache->set( 'g', 7, 0.90 );
		$this->assertSame(
			[ 'f' => 6, 'g' => 7, 'd' => 4 ],
			$cache->toArray()
		);

		$cache->set( 'g', 7, 1.0 );
		$this->assertSame(
			[ 'f' => 6, 'd' => 4, 'g' => 7 ],
			$cache->toArray()
		);
	}

	public function testExpiry() {
		$raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
		$cache = MapCacheLRU::newFromArray( $raw, 3 );

		$now = microtime( true );
		$cache->setMockTime( $now );

		$cache->set( 'd', 'xxx' );
		$this->assertTrue( $cache->has( 'd', 30 ) );
		$this->assertEquals( 'xxx', $cache->get( 'd' ) );

		$now += 29;
		$this->assertTrue( $cache->has( 'd', 30 ) );
		$this->assertEquals( 'xxx', $cache->get( 'd' ) );
		$this->assertEquals( 'xxx', $cache->get( 'd', 30 ) );

		$now += 1.5;
		$this->assertFalse( $cache->has( 'd', 30 ) );
		$this->assertEquals( 'xxx', $cache->get( 'd' ) );
		$this->assertNull( $cache->get( 'd', 30 ) );
	}

	public function testFields() {
		$raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
		$cache = MapCacheLRU::newFromArray( $raw, 3 );

		$now = microtime( true );
		$cache->setMockTime( $now );

		$cache->setField( 'PMs', 'Tony Blair', 'Labour' );
		$cache->setField( 'PMs', 'Margaret Thatcher', 'Tory' );
		$this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
		$this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
		$this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );

		$now += 29;
		$this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
		$this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
		$this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair', 30 ) );

		$now += 1.5;
		$this->assertFalse( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
		$this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
		$this->assertNull( $cache->getField( 'PMs', 'Tony Blair', 30 ) );

		$this->assertEquals(
			[ 'Tony Blair' => 'Labour', 'Margaret Thatcher' => 'Tory' ],
			$cache->get( 'PMs' )
		);

		$cache->set( 'MPs', [
			'Edwina Currie' => 1983,
			'Neil Kinnock' => 1970
		] );
		$this->assertEquals(
			[
				'Edwina Currie' => 1983,
				'Neil Kinnock' => 1970
			],
			$cache->get( 'MPs' )
		);

		$this->assertEquals( 1983, $cache->getField( 'MPs', 'Edwina Currie' ) );
		$this->assertEquals( 1970, $cache->getField( 'MPs', 'Neil Kinnock' ) );
	}

	public static function provideInvalidKeys() {
		yield [ 3.4 ];
		yield [ false ];
	}

	/**
	 * @dataProvider provideInvalidKeys
	 */
	public function testHasInvalidKey( $key ) {
		$cache = new MapCacheLRU( 3 );
		$this->expectException( UnexpectedValueException::class );
		$this->expectExceptionMessage( 'must be string or integer' );
		$cache->has( $key );
	}

	public static function provideMakeKey() {
		yield [ 'foo', 'foo' ];
		yield [ 'foo:bar:4:baz:qu%3Aux%2520', 'foo', 'bar', 4, 'baz', 'qu:ux%20' ];
		yield [ 'qu%3Aux%2520:4', 'qu:ux%20', 4 ];
	}

	/**
	 * @dataProvider provideMakeKey
	 */
	public function testMakeKey( $expected, ...$params ) {
		$cache = new MapCacheLRU( 3 );
		$this->assertSame( $expected, $cache->makeKey( ...$params ) );
	}

	/**
	 * @dataProvider provideInvalidKeys
	 */
	public function testGetInvalidKey( $key ) {
		$cache = new MapCacheLRU( 3 );
		$this->expectException( UnexpectedValueException::class );
		$this->expectExceptionMessage( 'must be string or integer' );
		$cache->get( $key );
	}

	/**
	 * @dataProvider provideInvalidKeys
	 */
	public function testSetInvalidKey( $key ) {
		$cache = new MapCacheLRU( 3 );
		$this->expectException( UnexpectedValueException::class );
		$this->expectExceptionMessage( 'must be string or integer' );
		$cache->set( $key, 'x' );
	}

	/**
	 * @dataProvider provideInvalidKeys
	 */
	public function testHasFieldInvalidKey( $field ) {
		$cache = MapCacheLRU::newFromArray( [ 'key' => [] ], 3 );
		$this->expectException( UnexpectedValueException::class );
		$this->expectExceptionMessage( 'must be string or integer' );
		$cache->hasField( 'key', $field );
	}

	/**
	 * @dataProvider provideInvalidKeys
	 */
	public function testGetFieldInvalidKey( $field ) {
		$cache = MapCacheLRU::newFromArray( [ 'key' => [] ], 3 );
		$this->expectException( UnexpectedValueException::class );
		$this->expectExceptionMessage( 'must be string or integer' );
		$cache->getField( 'key', $field );
	}

	/**
	 * @dataProvider provideInvalidKeys
	 */
	public function testSetFieldInvalidKey( $field ) {
		$cache = new MapCacheLRU( 3 );
		$this->expectException( UnexpectedValueException::class );
		$this->expectExceptionMessage( 'must be string or integer' );
		$cache->setField( 'key', $field, 'x' );
	}
}