Legoktm has uploaded a new change for review.
https://gerrit.wikimedia.org/r/200284
Change subject: Use TransactionProfiler to find unit tests that cause database
queries
......................................................................
Use TransactionProfiler to find unit tests that cause database queries
Change-Id: Iefef8c604d804696fdbeb60e9dfe5c95e1ef55c3
---
M includes/profiler/TransactionProfiler.php
M tests/phpunit/MediaWikiTestCase.php
2 files changed, 29 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/84/200284/1
diff --git a/includes/profiler/TransactionProfiler.php
b/includes/profiler/TransactionProfiler.php
index b313558..8870102 100644
--- a/includes/profiler/TransactionProfiler.php
+++ b/includes/profiler/TransactionProfiler.php
@@ -78,6 +78,21 @@
}
/**
+ * Get the current hit count for a event
+ *
+ * @since 1.25
+ * @param string $event
+ * @throws InvalidArgumentException
+ * @return array
+ */
+ public function getHits( $event ) {
+ if ( !isset( $this->hits[$event] ) ) {
+ throw new InvalidArgumentException( "'$event' is not a
valid event" );
+ }
+ return $this->hits[$event];
+ }
+
+ /**
* Reset performance expectations and hit counters
*
* @since 1.25
diff --git a/tests/phpunit/MediaWikiTestCase.php
b/tests/phpunit/MediaWikiTestCase.php
index 0253284..7fd9c66 100644
--- a/tests/phpunit/MediaWikiTestCase.php
+++ b/tests/phpunit/MediaWikiTestCase.php
@@ -208,6 +208,9 @@
// don't ignore DB errors
$this->db->ignoreErrors( false );
+ } else {
+ $trxProfiler =
Profiler::instance()->getTransactionProfiler();
+ $trxProfiler->setExpectation( 'queries', 0, __METHOD__
);
}
DeferredUpdates::clearPendingUpdates();
@@ -237,6 +240,17 @@
// don't ignore DB errors
$this->db->ignoreErrors( false );
+ } else {
+ $trxProfiler =
Profiler::instance()->getTransactionProfiler();
+ $hits = $trxProfiler->getHits( 'queries' );
+ if ( $hits > 0 ) {
+ $trxProfiler->resetExpectations();
+ $this->fail(
+ "Test caused $hits database queries,
add the " .
+ "'@group Database' tag if it requires
database access"
+ );
+ }
+ $trxProfiler->resetExpectations();
}
// Restore mw globals
--
To view, visit https://gerrit.wikimedia.org/r/200284
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iefef8c604d804696fdbeb60e9dfe5c95e1ef55c3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits