wiki.techinc.nl/tests/phpunit/includes/SiteStatsTest.php
Timo Tijhof 45660456cf phpunit: Clear job queues in MediaWikiTestCase::setUp()
This should fix the flaky unit test 'SiteStatsTest::testJobsCountGetCached',
which fails locally as follows, when run in isolation.

> 1) SiteStatsTest::testJobsCountGetCached
> A single job enqueued bumps jobscount stat to 1
> Failed asserting that 2 matches expected 1.
>
> /var/www/mediawiki/tests/phpunit/includes/SiteStatsTest.php:22
> /var/www/mediawiki/tests/phpunit/MediaWikiTestCase.php:421
> /var/www/mediawiki/maintenance/doMaintenance.php:94

Instrumenting JobQueueMemory::doBatchPush reveals the following
jobs to have been queued.

- MediaWikiTestCase->run/->addCoreDBData/::getTestSysop/..
  ../User->addGroup/UserGroupMembership->insert/..
  > UserGroupExpiryJob (2)
- MediaWikiTestCase->run/->addCoreDBData/WikiPage->doEditContent/..
  ../WikiPage->{closure}/WikiPage->doEditUpdates/JobQueueGroup->lazyPush/..
  > CategoryMembershipChangeJob
  > HTMLCacheUpdateJob (2)

Fix this by adding clearing of job queues to doLightweightServiceReset()
in MediaWikiTestCase.

Also:
- Move the call to doLightweightServiceReset() from run() to setUp(),
  where it is easier to understand in context. It still runs at the same
  logical point because PHPUnit calls setUp() right before run().

- Remove redundant reset for WANObjectCache->clearProcessCache that
  was both in setUp() and in doLightweightServiceReset().

- Simplify SiteStatsTest by removing the hardcoded delete() calls.
  An alternative fix for the flaky unit test would've been to add
  a delete() call to categoryMembershipChange, but rather than hardcoding
  all possible jobs that TestCase or another test could make, it's easier
  to just reset/delete them all between tests.

- Simplify SiteStatsTest by using the $cache reference directly instead
  of roundtripping through MediaWikiServices. If for some reason
  setService() didn't work, the test will fail either way because it must
  match the one used by JobQueueGroup (TODO: Use injection!), and besides
  the setService() method already has its own unit test.

Change-Id: Ia4b7871221c76c65eacf31915b515705a36940d5
2018-04-24 00:46:19 +01:00

35 lines
1 KiB
PHP

<?php
class SiteStatsTest extends MediaWikiTestCase {
/**
* @covers SiteStats::jobs
*/
function testJobsCountGetCached() {
$cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
$this->setService( 'MainWANObjectCache', $cache );
$jobq = JobQueueGroup::singleton();
$jobq->push( new NullJob( Title::newMainPage(), [] ) );
$this->assertEquals( 1, SiteStats::jobs(),
'A single job enqueued bumps jobscount stat to 1' );
$jobq->push( new NullJob( Title::newMainPage(), [] ) );
$this->assertEquals( 1, SiteStats::jobs(),
'SiteStats::jobs() count does not reflect addition ' .
'of a second job (cached)'
);
$jobq->get( 'null' )->delete(); // clear jobqueue
$this->assertEquals( 0, $jobq->get( 'null' )->getSize(),
'Job queue for NullJob has been cleaned' );
$cache->delete( $cache->makeKey( 'SiteStats', 'jobscount' ) );
$this->assertEquals( 1, SiteStats::jobs(),
'jobs count is kept in process cache' );
$cache->clearProcessCache();
$this->assertEquals( 0, SiteStats::jobs() );
}
}