Page MenuHomePhabricator

Create a tests to prevent unexpected usage of ILoadBalancer and ILBFactory instances in Wikibase
Closed, ResolvedPublic

Description

This means we want something similar to the forbidden Client/Repo usage tests.

MediaWikiServices::getDBLoadBalancerFactory and MediaWikiServices::getDBLoadBalancer shouldn't be called in Wikibase production code outside of the three places where it is currently being called.

Event Timeline

So what exactly do we want to prevent…

  • The Wikimedia\Rdmbs\ namespace: this would be closest to the existing repo/client/lib usage test, but I’m pretty sure this isn’t feasible – there are several uses of other types in that namespace, notably IResultWrapper, that should be allowed.
  • The I?(LoadBalancer|LBFactory) types (with an exception to allow ILoadBalancer::CONN_TRX_AUTOCOMMIT, unless we copy that flag somewhere else).
  • The getDBLoadBalancer(Factory)? service container functions, and/or the old-style wfGet[DL]B global functions.

Maybe both of the last ones? We want to disallow classes with ILoadBalancer parameters, and also inline usage like MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection().

Where the test should reside? In Lib? I want to avoid that as lib should be dismantled. In Client and repo separately? then how are going to test lib? Maybe we shouldn't test lib in this regard at all.

@Addshore had previously found phparch and phpat, two projects that are supposed to help you keep your architecture clean. Though both seem to be fairly focused on namespaces.

I think ideally I would want this to be a part of PHPCS, so that we can allow the permitted places (RepoDomainDbFactory etc.) using // phpcs:disable comments in those files, rather than hard-coding paths to those files in some other test file. But that might not be the easiest solution.

Doesn’t look like there’s any existing PHPCS ruleset we could easily use. (mediawiki-codesniffer has several sniffs for forbidden or deprecated things, but they’re all hard-coded, not configurable. Slevomat coding standard has a forbidden classes sniff, but it’s for detecting when the class is extended (interface is implemented, trait is used), not when it’s referenced in any way. PHPCS itself has a forbidden functions sniff, but it’s for global functions (printecho), not methods.)

I guess it’ll have to be a PHPUnit test with a bunch of regexes.

Change 703460 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/extensions/Wikibase@master] Test number of MW RDBMS references in lib/

https://gerrit.wikimedia.org/r/703460

Change 703462 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/extensions/Wikibase@master] Also strip line comments in NoBadDependencyUsageTest

https://gerrit.wikimedia.org/r/703462

Change 703587 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/extensions/Wikibase@master] Rewrite NoBadDependencyUsageTest as NoBadUsageTest

https://gerrit.wikimedia.org/r/703587

Change 703588 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/extensions/Wikibase@master] WIP: Add RepoNoBadUsageTest

https://gerrit.wikimedia.org/r/703588

Change 703460 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Test number of MW RDBMS references in lib/

https://gerrit.wikimedia.org/r/703460

Change 703462 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Also strip line comments in NoBadDependencyUsageTest

https://gerrit.wikimedia.org/r/703462

Change 703587 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Rewrite NoBadDependencyUsageTest as NoBadUsageTest

https://gerrit.wikimedia.org/r/703587

Change 703588 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Add RepoNoBadUsageTest

https://gerrit.wikimedia.org/r/703588

Change 703720 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/extensions/Wikibase@master] Add ClientNoBadUsageTest

https://gerrit.wikimedia.org/r/703720

Change 703720 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] Add ClientNoBadUsageTest

https://gerrit.wikimedia.org/r/703720

Awesome refactoring of the test happened here too ! :)