File: ObjectCacheFactoryIntegrationTest.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 (224 lines) | stat: -rw-r--r-- 6,939 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
<?php

use MediaWiki\MainConfigNames;
use Wikimedia\ObjectCache\EmptyBagOStuff;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\TestingAccessWrapper;

/**
 * @covers \ObjectCacheFactory
 * @group BagOStuff
 * @group Database
 */
class ObjectCacheFactoryIntegrationTest extends MediaWikiIntegrationTestCase {

	protected function setUp(): void {
		$this->setCacheConfig();
		$this->setMainCache( CACHE_NONE );
		$this->overrideConfigValues( [
			MainConfigNames::MessageCacheType => CACHE_NONE,
			MainConfigNames::ParserCacheType => CACHE_NONE,
		] );

		// Mock ACCEL with 'hash' as being installed.
		// This makes tests deterministic regardless of whether APCu is installed.
		ObjectCacheFactory::$localServerCacheClass = 'HashBagOStuff';
	}

	protected function tearDown(): void {
		ObjectCacheFactory::$localServerCacheClass = null;
	}

	private function setCacheConfig( $arr = [] ) {
		$defaults = [
			CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
			CACHE_DB => [ 'class' => SqlBagOStuff::class ],
			'hash' => [ 'class' => HashBagOStuff::class ],
			CACHE_ANYTHING => [ 'class' => HashBagOStuff::class ],
		];
		$this->overrideConfigValue( MainConfigNames::ObjectCaches, $arr + $defaults );
	}

	public function testNewAnythingNothing() {
		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertInstanceOf(
			SqlBagOStuff::class,
			$ocf->getInstance( $ocf->getAnythingId() ),
			'No available types. Fallback to DB'
		);
	}

	public function testNewAnythingHash() {
		$this->setMainCache( CACHE_HASH );

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertInstanceOf(
			HashBagOStuff::class,
			$ocf->getInstance( $ocf->getAnythingId() ),
			'Use an available type (hash)'
		);
	}

	public function testNewAnythingAccel() {
		$this->setMainCache( CACHE_ACCEL );

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertInstanceOf(
			HashBagOStuff::class,
			$ocf->getInstance( $ocf->getAnythingId() ),
			'Use an available type (CACHE_ACCEL)'
		);
	}

	public function testNewAnythingNoAccel() {
		// Mock APC not being installed (T160519, T147161)
		ObjectCacheFactory::$localServerCacheClass = EmptyBagOStuff::class;
		$this->setMainCache( CACHE_ACCEL );

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertInstanceOf(
			SqlBagOStuff::class,
			$ocf->getInstance( $ocf->getAnythingId() ),
			'Fallback to DB if available types fall back to Empty'
		);
	}

	public function testNewAnythingNoAccelNoDb() {
		$this->setCacheConfig( [
			// Mock APCu not being installed (T160519, T147161)
			CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
		] );
		$this->setMainCache( CACHE_ACCEL );

		$this->getServiceContainer()->disableStorage();

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertInstanceOf(
			EmptyBagOStuff::class,
			$ocf->getInstance( $ocf->getAnythingId() ),
			'Fallback to none if available types and DB are unavailable'
		);
	}

	public function testNewAnythingNothingNoDb() {
		$this->getServiceContainer()->disableStorage();

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertInstanceOf(
			EmptyBagOStuff::class,
			$ocf->getInstance( $ocf->getAnythingId() ),
			'No available types or DB. Fallback to none.'
		);
	}

	public function testNewFromIdWincacheAccel() {
		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$className = get_class( $ocf );

		$this->expectDeprecationAndContinue( "/^Use of $className::newFromId with cache ID \"wincache\"\s/" );

		$this->assertInstanceOf(
			HashBagOStuff::class,
			$ocf->getInstance( 'wincache' ),
			'Fallback to APCu for deprecated wincache'
		);
	}

	public function testNewFromIdWincacheNoAccel() {
		// Mock APC not being installed (T160519, T147161)
		ObjectCacheFactory::$localServerCacheClass = EmptyBagOStuff::class;

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$className = get_class( $ocf );

		$this->expectDeprecationAndContinue( "/^Use of $className::newFromId with cache ID \"wincache\"\s/" );

		$this->assertInstanceOf(
			EmptyBagOStuff::class,
			$ocf->getInstance( 'wincache' ),
			'No caching if APCu is not available'
		);
	}

	public function provideLocalServerKeyspace() {
		$dbDomain = static function ( $dbName, $dbPrefix ) {
			global $wgDBmwschema;
			return ( new DatabaseDomain( $dbName, $wgDBmwschema, $dbPrefix ) )->getId();
		};
		return [
			'default' => [ false, 'my_wiki', '', $dbDomain( 'my_wiki', '' ) ],
			'custom' => [ 'custom', 'my_wiki', '', 'custom' ],
			'prefix' => [ false, 'my_wiki', 'nl_', $dbDomain( 'my_wiki', 'nl_' ) ],
			'empty string' => [ '', 'my_wiki', 'nl_', $dbDomain( 'my_wiki', 'nl_' ) ],
		];
	}

	/**
	 * @dataProvider provideLocalServerKeyspace
	 */
	public function testLocalServerKeyspace( $cachePrefix, $dbName, $dbPrefix, $expect ) {
		$this->overrideConfigValues( [
			MainConfigNames::CachePrefix => $cachePrefix,
			MainConfigNames::DBname => $dbName,
			MainConfigNames::DBprefix => $dbPrefix,
		] );
		// Regression against T247562, T361177.
		$cache = $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_ACCEL );
		$cache = TestingAccessWrapper::newFromObject( $cache );
		$this->assertSame( $expect, $cache->keyspace );
	}

	public function testNewMultiWrite() {
		$this->overrideConfigValues( [
			MainConfigNames::CachePrefix => 'moon-river',
		] );
		$this->setCacheConfig( [
			'multi-example' => [
				'class' => 'MultiWriteBagOStuff',
				'caches' => [
					0 => [
						'class' => 'HashBagOStuff',
					],
					1 => [
						'class' => 'HashBagOStuff',
					],
				],
			],
		] );

		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$multi = $ocf->getInstance( 'multi-example' );
		$caches = TestingAccessWrapper::newFromObject( $multi )->caches;

		$this->assertSame( 'moon-river:x', $multi->makeKey( 'x' ), 'MultiWrite key' );

		// Confirm that dependency injection is also applied to the objects constructed
		// for the child caches (T318272).
		$this->assertSame( 'moon-river:x', $caches[0]->makeKey( 'x' ), 'inject cache 0 keyspace' );
		$this->assertSame( 'moon-river:x', $caches[1]->makeKey( 'x' ), 'inject cache 1 keyspace' );
	}

	public static function provideIsDatabaseId() {
		return [
			[ CACHE_DB, CACHE_NONE, true ],
			[ CACHE_ANYTHING, CACHE_DB, true ],
			[ CACHE_ANYTHING, 'hash', false ],
			[ CACHE_ANYTHING, CACHE_ANYTHING, true ]
		];
	}

	/**
	 * @dataProvider provideIsDatabaseId
	 * @param string|int $id
	 * @param string|int $mainCacheType
	 * @param bool $expected
	 */
	public function testIsDatabaseId( $id, $mainCacheType, $expected ) {
		$this->overrideConfigValues( [
			MainConfigNames::MainCacheType => $mainCacheType
		] );
		$ocf = $this->getServiceContainer()->getObjectCacheFactory();
		$this->assertSame( $expected, $ocf->isDatabaseId( $id ) );
	}
}