diff options
author | Tim Starling <tstarling@wikimedia.org> | 2022-02-07 14:25:04 +1100 |
---|---|---|
committer | Tim Starling <tstarling@wikimedia.org> | 2022-02-11 10:20:33 +1100 |
commit | e582572622c55439b6e10e64dc8693460faf6215 (patch) | |
tree | 18b20076e63700eeac22911891032cfefc58c137 | |
parent | 0607fc561aebf894ef17d5acdfa0a456bc3e70e5 (diff) | |
download | mediawikicore-e582572622c55439b6e10e64dc8693460faf6215.tar.gz mediawikicore-e582572622c55439b6e10e64dc8693460faf6215.zip |
Improve ObjectCache integration tests
* Add BagOStuffTest subclasses for all core BagOStuff subclasses,
replacing PHPUNIT_USE_BAGOSTUFF, as suggested in a todo comment.
* Add config $wgEnableRemoteBagOStuffTests which causes all tests
enabled by $wgObjectCache to execute, which means that the memcached
tests are executed by default.
I have verified all except RESTBagOStuff and WinCacheBagOStuff. The
memcached tests fail against memcached 1.5.x but pass against memcached
1.6.x.
Bug: T90875
Change-Id: Id74b5226669f8cb857f859fbc35bc58ab001e873
16 files changed, 146 insertions, 25 deletions
diff --git a/RELEASE-NOTES-1.38 b/RELEASE-NOTES-1.38 index b8496ef0139e..6e8994858427 100644 --- a/RELEASE-NOTES-1.38 +++ b/RELEASE-NOTES-1.38 @@ -38,6 +38,8 @@ For notes on 1.37.x and older releases, see HISTORY. from 1 hour to 4 hours. * $wgSkinsPreferred – List of preferred skins to be listed higher in Special:Preferences +* $wgEnableRemoteBagOStuffTests replaces the environment variable + PHPUNIT_USE_BAGOSTUFF. * … ==== Changed configuration ==== diff --git a/docs/Configuration.md b/docs/Configuration.md index f67a077e200b..ff6c4cbcf208 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -2303,6 +2303,12 @@ and all the cache entries already written in JSON are discarded. @deprecated since 1.36 @see https://phabricator.wikimedia.org/T263579 +# EnableRemoteBagOStuffTests {#EnableRemoteBagOStuffTests} +If this is set to true, phpunit will run integration tests against remote +caches defined in $wgObjectCaches. + +@since 1.38 + # UseCdn {#UseCdn} Enable/disable CDN. diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 7d1844713332..1b2dffdc0edd 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -3226,6 +3226,14 @@ $wgExtensionInfoMTime = false; */ $wgParserCacheUseJson = true; +/** + * If this is set to true, phpunit will run integration tests against remote + * caches defined in $wgObjectCaches. + * + * @since 1.38 + */ +$wgEnableRemoteBagOStuffTests = false; + // endregion -- end of cache settings /***************************************************************************/ diff --git a/includes/config-schema.php b/includes/config-schema.php index b9e8aa6a25e5..c69953a03b37 100644 --- a/includes/config-schema.php +++ b/includes/config-schema.php @@ -1173,6 +1173,9 @@ return [ 'ParserCacheUseJson' => [ 'default' => true, ], + 'EnableRemoteBagOStuffTests' => [ + 'default' => false, + ], 'UseCdn' => [ 'default' => false, ], diff --git a/includes/config-schema.yaml b/includes/config-schema.yaml index 93f80c5d26b9..f65e60711310 100644 --- a/includes/config-schema.yaml +++ b/includes/config-schema.yaml @@ -2563,6 +2563,13 @@ config-schema: @since 1.36 @deprecated since 1.36 @see https://phabricator.wikimedia.org/T263579 + EnableRemoteBagOStuffTests: + default: false + description: |- + If this is set to true, phpunit will run integration tests against remote + caches defined in $wgObjectCaches. + + @since 1.38 UseCdn: default: false description: |- diff --git a/tests/phpunit/MediaWikiCliOptions.php b/tests/phpunit/MediaWikiCliOptions.php index c08eebf31d80..fae94a4e3491 100644 --- a/tests/phpunit/MediaWikiCliOptions.php +++ b/tests/phpunit/MediaWikiCliOptions.php @@ -1,7 +1,7 @@ <?php /** - * @todo Get rid of this class, the options we don't need (e.g. for filebackend, bagostuff and jobqueue + * @todo Get rid of this class, the options we don't need (e.g. for filebackend and jobqueue * we should have dedicated test subclasses), and use getenv directly in calling code. */ final class MediaWikiCliOptions { @@ -10,7 +10,6 @@ final class MediaWikiCliOptions { */ public static $additionalOptions = [ 'use-filebackend' => null, - 'use-bagostuff' => null, 'use-jobqueue' => null, 'use-normal-tables' => false ]; @@ -18,7 +17,6 @@ final class MediaWikiCliOptions { public static function initialize(): void { self::$additionalOptions['use-normal-tables'] = (bool)getenv( 'PHPUNIT_USE_NORMAL_TABLES' ); self::$additionalOptions['use-filebackend'] = getenv( 'PHPUNIT_USE_FILEBACKEND' ) ?: null; - self::$additionalOptions['use-bagostuff'] = getenv( 'PHPUNIT_USE_BAGOSTUFF' ) ?: null; self::$additionalOptions['use-jobqueue'] = getenv( 'PHPUNIT_USE_JOBQUEUE' ) ?: null; } } diff --git a/tests/phpunit/MediaWikiIntegrationTestCase.php b/tests/phpunit/MediaWikiIntegrationTestCase.php index f19d35eab38b..946aead60a69 100644 --- a/tests/phpunit/MediaWikiIntegrationTestCase.php +++ b/tests/phpunit/MediaWikiIntegrationTestCase.php @@ -683,6 +683,17 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { } /** + * Get a configuration variable + * + * @param string $name + * @return mixed + * @since 1.38 + */ + protected function getConfVar( $name ) { + return $this->getServiceContainer()->getMainConfig()->get( $name ); + } + + /** * Sets a service, maintaining a stashed version of the previous service to be * restored in tearDown. * diff --git a/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php b/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php deleted file mode 100644 index 124c0b4bbc4f..000000000000 --- a/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -/** - * @group BagOStuff - * @covers BagOStuff - */ -class BagOStuffTest extends BagOStuffTestBase { - protected function newCacheInstance() { - if ( $this->getCliArg( 'use-bagostuff' ) !== null ) { - global $wgObjectCaches; - - $id = $this->getCliArg( 'use-bagostuff' ); - $cache = ObjectCache::newFromParams( $wgObjectCaches[$id] ); - } else { - // no type defined - use simple hash - $cache = new HashBagOStuff; - } - - return $cache; - } -} diff --git a/tests/phpunit/includes/libs/objectcache/BagOStuffTestBase.php b/tests/phpunit/includes/libs/objectcache/BagOStuffTestBase.php index d17952e639c8..1d8acb1453c2 100644 --- a/tests/phpunit/includes/libs/objectcache/BagOStuffTestBase.php +++ b/tests/phpunit/includes/libs/objectcache/BagOStuffTestBase.php @@ -19,7 +19,12 @@ abstract class BagOStuffTestBase extends MediaWikiIntegrationTestCase { protected function setUp(): void { parent::setUp(); - $this->cache = $this->newCacheInstance(); + try { + $this->cache = $this->newCacheInstance(); + } catch ( InvalidArgumentException $e ) { + $this->markTestSkipped( "Cannot create cache instance for " . static::class . + ': the configuration is presumably missing from $wgObjectCaches' ); + } $this->cache->deleteMulti( [ $this->cache->makeKey( self::TEST_KEY ), $this->cache->makeKey( self::TEST_KEY ) . ':lock' @@ -31,6 +36,16 @@ abstract class BagOStuffTestBase extends MediaWikiIntegrationTestCase { */ abstract protected function newCacheInstance(); + protected function getCacheByClass( $className ) { + $caches = $this->getConfVar( 'ObjectCaches' ); + foreach ( $caches as $id => $cache ) { + if ( ( $cache['class'] ?? '' ) === $className ) { + return ObjectCache::getInstance( $id ); + } + } + $this->markTestSkipped( "No $className is configured" ); + } + /** * @covers MediumSpecificBagOStuff::makeGlobalKey * @covers MediumSpecificBagOStuff::makeKeyInternal diff --git a/tests/phpunit/includes/libs/objectcache/HashBagOStuffIntegrationTest.php b/tests/phpunit/includes/libs/objectcache/HashBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..9c546d26544f --- /dev/null +++ b/tests/phpunit/includes/libs/objectcache/HashBagOStuffIntegrationTest.php @@ -0,0 +1,11 @@ +<?php + +/** + * @group BagOStuff + * @covers HashBagOStuff + */ +class HashBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + return new HashBagOStuff(); + } +} diff --git a/tests/phpunit/includes/libs/objectcache/MemcachedPeclBagOStuffIntegrationTest.php b/tests/phpunit/includes/libs/objectcache/MemcachedPeclBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..ee6ea649bcb2 --- /dev/null +++ b/tests/phpunit/includes/libs/objectcache/MemcachedPeclBagOStuffIntegrationTest.php @@ -0,0 +1,15 @@ +<?php + +/** + * @group BagOStuff + * @covers MemcachedPeclBagOStuff + * @requires extension memcached + */ +class MemcachedPeclBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + if ( !$this->getConfVar( 'EnableRemoteBagOStuffTests' ) ) { + $this->markTestSkipped( '$wgEnableRemoteBagOStuffTests is false' ); + } + return ObjectCache::getInstance( 'memcached-pecl' ); + } +} diff --git a/tests/phpunit/includes/libs/objectcache/MemcachedPhpBagOStuffIntegrationTest.php b/tests/phpunit/includes/libs/objectcache/MemcachedPhpBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..60cbef5d480c --- /dev/null +++ b/tests/phpunit/includes/libs/objectcache/MemcachedPhpBagOStuffIntegrationTest.php @@ -0,0 +1,14 @@ +<?php + +/** + * @group BagOStuff + * @covers MemcachedPhpBagOStuff + */ +class MemcachedPhpBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + if ( !$this->getConfVar( 'EnableRemoteBagOStuffTests' ) ) { + $this->markTestSkipped( '$wgEnableRemoteBagOStuffTests is false' ); + } + return ObjectCache::getInstance( 'memcached-php' ); + } +} diff --git a/tests/phpunit/includes/libs/objectcache/RESTBagOStuffIntegrationTest.php b/tests/phpunit/includes/libs/objectcache/RESTBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..d5f654738bed --- /dev/null +++ b/tests/phpunit/includes/libs/objectcache/RESTBagOStuffIntegrationTest.php @@ -0,0 +1,14 @@ +<?php + +/** + * @group BagOStuff + * @covers RESTBagOStuff + */ +class RESTBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + if ( !$this->getConfVar( 'EnableRemoteBagOStuffTests' ) ) { + $this->markTestSkipped( '$wgEnableRemoteBagOStuffTests is false' ); + } + return $this->getCacheByClass( RESTBagOStuff::class ); + } +} diff --git a/tests/phpunit/includes/libs/objectcache/RedisBagOStuffIntegrationTest.php b/tests/phpunit/includes/libs/objectcache/RedisBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..07447cc31811 --- /dev/null +++ b/tests/phpunit/includes/libs/objectcache/RedisBagOStuffIntegrationTest.php @@ -0,0 +1,15 @@ +<?php + +/** + * @group BagOStuff + * @covers RedisBagOStuff + * @requires extension redis + */ +class RedisBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + if ( !$this->getConfVar( 'EnableRemoteBagOStuffTests' ) ) { + $this->markTestSkipped( '$wgEnableRemoteBagOStuffTests is false' ); + } + return $this->getCacheByClass( RedisBagOStuff::class ); + } +} diff --git a/tests/phpunit/includes/libs/objectcache/WinCacheBagOStuffIntegrationTest.php b/tests/phpunit/includes/libs/objectcache/WinCacheBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..477ef47ec1a3 --- /dev/null +++ b/tests/phpunit/includes/libs/objectcache/WinCacheBagOStuffIntegrationTest.php @@ -0,0 +1,12 @@ +<?php + +/** + * @group BagOStuff + * @covers WinCacheBagOStuff + * @requires extension wincache + */ +class WinCacheBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + return ObjectCache::getInstance( 'wincache' ); + } +} diff --git a/tests/phpunit/includes/objectcache/SqlBagOStuffIntegrationTest.php b/tests/phpunit/includes/objectcache/SqlBagOStuffIntegrationTest.php new file mode 100644 index 000000000000..5f71c1135531 --- /dev/null +++ b/tests/phpunit/includes/objectcache/SqlBagOStuffIntegrationTest.php @@ -0,0 +1,11 @@ +<?php + +/** + * @group BagOStuff + * @covers SqlBagOStuff + */ +class SqlBagOStuffIntegrationTest extends BagOStuffTestBase { + protected function newCacheInstance() { + return ObjectCache::getInstance( CACHE_DB ); + } +} |