summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/libs/objectcache/HashBagOStuffTest.php
blob: 332e23b25be99124a8ac25a85691b65398b25c34 (plain)
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
<?php

use Wikimedia\TestingAccessWrapper;

/**
 * @group BagOStuff
 */
class HashBagOStuffTest extends PHPUnit\Framework\TestCase {

	use MediaWikiCoversValidator;

	/**
	 * @covers HashBagOStuff::__construct
	 */
	public function testConstruct() {
		$this->assertInstanceOf(
			HashBagOStuff::class,
			new HashBagOStuff()
		);
	}

	/**
	 * @covers HashBagOStuff::__construct
	 * @expectedException InvalidArgumentException
	 */
	public function testConstructBadZero() {
		$cache = new HashBagOStuff( [ 'maxKeys' => 0 ] );
	}

	/**
	 * @covers HashBagOStuff::__construct
	 * @expectedException InvalidArgumentException
	 */
	public function testConstructBadNeg() {
		$cache = new HashBagOStuff( [ 'maxKeys' => -1 ] );
	}

	/**
	 * @covers HashBagOStuff::__construct
	 * @expectedException InvalidArgumentException
	 */
	public function testConstructBadType() {
		$cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] );
	}

	/**
	 * @covers HashBagOStuff::delete
	 */
	public function testDelete() {
		$cache = new HashBagOStuff();
		for ( $i = 0; $i < 10; $i++ ) {
			$cache->set( "key$i", 1 );
			$this->assertEquals( 1, $cache->get( "key$i" ) );
			$cache->delete( "key$i" );
			$this->assertEquals( false, $cache->get( "key$i" ) );
		}
	}

	/**
	 * @covers HashBagOStuff::clear
	 */
	public function testClear() {
		$cache = new HashBagOStuff();
		for ( $i = 0; $i < 10; $i++ ) {
			$cache->set( "key$i", 1 );
			$this->assertEquals( 1, $cache->get( "key$i" ) );
		}
		$cache->clear();
		for ( $i = 0; $i < 10; $i++ ) {
			$this->assertEquals( false, $cache->get( "key$i" ) );
		}
	}

	/**
	 * @covers HashBagOStuff::doGet
	 * @covers HashBagOStuff::expire
	 */
	public function testExpire() {
		$cache = new HashBagOStuff();
		$cacheInternal = TestingAccessWrapper::newFromObject( $cache );
		$cache->set( 'foo', 1 );
		$cache->set( 'bar', 1, 10 );
		$cache->set( 'baz', 1, -10 );

		$this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
		// 2 seconds tolerance
		$this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
		$this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );

		$this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
		$this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
	}

	/**
	 * Ensure maxKeys eviction prefers keeping new keys.
	 *
	 * @covers HashBagOStuff::set
	 */
	public function testEvictionAdd() {
		$cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
		for ( $i = 0; $i < 10; $i++ ) {
			$cache->set( "key$i", 1 );
			$this->assertEquals( 1, $cache->get( "key$i" ) );
		}
		for ( $i = 10; $i < 20; $i++ ) {
			$cache->set( "key$i", 1 );
			$this->assertEquals( 1, $cache->get( "key$i" ) );
			$this->assertEquals( false, $cache->get( "key" . ( $i - 10 ) ) );
		}
	}

	/**
	 * Ensure maxKeys eviction prefers recently set keys
	 * even if the keys pre-exist.
	 *
	 * @covers HashBagOStuff::set
	 */
	public function testEvictionSet() {
		$cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );

		foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
			$cache->set( $key, 1 );
		}

		// Set existing key
		$cache->set( 'foo', 1 );

		// Add a 4th key (beyond the allowed maximum)
		$cache->set( 'quux', 1 );

		// Foo's life should have been extended over Bar
		foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
			$this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
		}
		$this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
	}

	/**
	 * Ensure maxKeys eviction prefers recently retrieved keys (LRU).
	 *
	 * @covers HashBagOStuff::doGet
	 * @covers HashBagOStuff::hasKey
	 */
	public function testEvictionGet() {
		$cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );

		foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
			$cache->set( $key, 1 );
		}

		// Get existing key
		$cache->get( 'foo', 1 );

		// Add a 4th key (beyond the allowed maximum)
		$cache->set( 'quux', 1 );

		// Foo's life should have been extended over Bar
		foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
			$this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
		}
		$this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
	}
}