2017-03-15 22:51:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-07-28 20:36:55 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2024-03-21 19:06:32 +00:00
|
|
|
use Wikimedia\Rdbms\DatabaseDomain;
|
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2022-07-28 20:36:55 +00:00
|
|
|
|
2023-02-21 16:44:38 +00:00
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \ObjectCache
|
2023-02-21 16:44:38 +00:00
|
|
|
* @group BagOStuff
|
2023-08-05 23:03:25 +00:00
|
|
|
* @group Database
|
2023-02-21 16:44:38 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ObjectCacheTest extends MediaWikiIntegrationTestCase {
|
2017-03-15 22:51:13 +00:00
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2017-03-15 22:51:13 +00:00
|
|
|
// Parent calls ObjectCache::clear() among other things
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->setCacheConfig();
|
2022-07-28 20:36:55 +00:00
|
|
|
$this->setMainCache( CACHE_NONE );
|
|
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::MessageCacheType => CACHE_NONE,
|
|
|
|
|
MainConfigNames::ParserCacheType => CACHE_NONE,
|
2017-03-15 22:51:13 +00:00
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 02:14:13 +00:00
|
|
|
protected function tearDown(): void {
|
|
|
|
|
ObjectCache::$localServerCacheClass = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-15 22:51:13 +00:00
|
|
|
private function setCacheConfig( $arr = [] ) {
|
|
|
|
|
$defaults = [
|
2018-01-13 00:02:09 +00:00
|
|
|
CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
|
|
|
|
|
CACHE_DB => [ 'class' => SqlBagOStuff::class ],
|
|
|
|
|
'hash' => [ 'class' => HashBagOStuff::class ],
|
2023-09-07 15:12:17 +00:00
|
|
|
CACHE_ANYTHING => [ 'class' => HashBagOStuff::class ],
|
2017-03-15 22:51:13 +00:00
|
|
|
];
|
2022-07-28 20:36:55 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::ObjectCaches, $arr + $defaults );
|
2023-05-03 02:14:13 +00:00
|
|
|
// Mock ACCEL with 'hash' as being installed.
|
|
|
|
|
// This makes tests deterministic regardless of APC.
|
|
|
|
|
ObjectCache::$localServerCacheClass = 'HashBagOStuff';
|
2017-03-15 22:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewAnythingNothing() {
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
SqlBagOStuff::class,
|
2024-05-05 08:33:19 +00:00
|
|
|
$this->getServiceContainer()->getObjectCacheFactory()->getInstance( ObjectCache::getAnythingId() ),
|
2017-03-15 22:51:13 +00:00
|
|
|
'No available types. Fallback to DB'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewAnythingHash() {
|
2022-07-28 20:36:55 +00:00
|
|
|
$this->setMainCache( CACHE_HASH );
|
2017-03-15 22:51:13 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
HashBagOStuff::class,
|
2024-05-05 08:33:19 +00:00
|
|
|
$this->getServiceContainer()->getObjectCacheFactory()->getInstance( ObjectCache::getAnythingId() ),
|
2017-03-15 22:51:13 +00:00
|
|
|
'Use an available type (hash)'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewAnythingAccel() {
|
2022-07-28 20:36:55 +00:00
|
|
|
$this->setMainCache( CACHE_ACCEL );
|
2017-03-15 22:51:13 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
HashBagOStuff::class,
|
2024-05-05 08:33:19 +00:00
|
|
|
$this->getServiceContainer()->getObjectCacheFactory()->getInstance( ObjectCache::getAnythingId() ),
|
2017-03-15 22:51:13 +00:00
|
|
|
'Use an available type (CACHE_ACCEL)'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-10 21:41:12 +00:00
|
|
|
public function testNewAnythingNoAccel() {
|
2023-02-14 21:18:33 +00:00
|
|
|
// Mock APC not being installed (T160519, T147161)
|
2023-05-03 02:14:13 +00:00
|
|
|
ObjectCache::$localServerCacheClass = EmptyBagOStuff::class;
|
2023-02-14 21:18:33 +00:00
|
|
|
$this->setMainCache( CACHE_ACCEL );
|
2017-03-15 22:51:13 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
SqlBagOStuff::class,
|
2024-05-05 08:33:19 +00:00
|
|
|
$this->getServiceContainer()->getObjectCacheFactory()->getInstance( ObjectCache::getAnythingId() ),
|
2017-03-15 22:51:13 +00:00
|
|
|
'Fallback to DB if available types fall back to Empty'
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-04-10 21:41:12 +00:00
|
|
|
|
|
|
|
|
public function testNewAnythingNoAccelNoDb() {
|
|
|
|
|
$this->setCacheConfig( [
|
|
|
|
|
// Mock APC not being installed (T160519, T147161)
|
2018-01-13 00:02:09 +00:00
|
|
|
CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
|
2017-04-10 21:41:12 +00:00
|
|
|
] );
|
2023-02-14 21:18:33 +00:00
|
|
|
$this->setMainCache( CACHE_ACCEL );
|
2017-04-10 21:41:12 +00:00
|
|
|
|
2023-01-25 15:11:10 +00:00
|
|
|
$this->getServiceContainer()->disableStorage();
|
2019-08-26 08:17:59 +00:00
|
|
|
|
2017-04-10 21:41:12 +00:00
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
EmptyBagOStuff::class,
|
2024-05-05 08:33:19 +00:00
|
|
|
$this->getServiceContainer()->getObjectCacheFactory()->getInstance( ObjectCache::getAnythingId() ),
|
2017-04-10 21:41:12 +00:00
|
|
|
'Fallback to none if available types and DB are unavailable'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewAnythingNothingNoDb() {
|
2023-01-25 15:11:10 +00:00
|
|
|
$this->getServiceContainer()->disableStorage();
|
2017-04-10 21:41:12 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
EmptyBagOStuff::class,
|
2024-05-05 08:33:19 +00:00
|
|
|
$this->getServiceContainer()->getObjectCacheFactory()->getInstance( ObjectCache::getAnythingId() ),
|
2017-04-10 21:41:12 +00:00
|
|
|
'No available types or DB. Fallback to none.'
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-05-03 02:14:13 +00:00
|
|
|
|
2024-03-21 19:06:32 +00:00
|
|
|
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
|
|
|
|
|
* @covers \ObjectCache
|
|
|
|
|
* @covers \ObjectCacheFactory
|
|
|
|
|
* @covers \MediaWiki\WikiMap\WikiMap
|
|
|
|
|
*/
|
|
|
|
|
public function testLocalServerKeyspace( $cachePrefix, $dbName, $dbPrefix, $expect ) {
|
|
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::CachePrefix => $cachePrefix,
|
|
|
|
|
MainConfigNames::DBname => $dbName,
|
|
|
|
|
MainConfigNames::DBprefix => $dbPrefix,
|
|
|
|
|
] );
|
|
|
|
|
// Regression against T247562 (2020), T361177 (2024).
|
|
|
|
|
$cache = $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_ACCEL );
|
|
|
|
|
$cache = TestingAccessWrapper::newFromObject( $cache );
|
|
|
|
|
$this->assertSame( $expect, $cache->keyspace );
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 02:14:13 +00:00
|
|
|
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 ) {
|
2023-07-17 21:02:32 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
|
MainConfigNames::MainCacheType => $mainCacheType
|
2023-05-03 02:14:13 +00:00
|
|
|
] );
|
|
|
|
|
$this->assertSame( $expected, ObjectCache::isDatabaseId( $id ) );
|
|
|
|
|
}
|
2017-03-15 22:51:13 +00:00
|
|
|
}
|