1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<?php
use MediaWiki\Json\JsonCodec;
use MediaWiki\Parser\ParserCache;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\PoolCounter\PoolWorkArticleViewCurrent;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Status\Status;
use Psr\Log\NullLogger;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Rdbms\ChronologyProtector;
use Wikimedia\Stats\StatsFactory;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\PoolCounter\PoolWorkArticleViewCurrent
* @group Database
*/
class PoolWorkArticleViewCurrentTest extends PoolWorkArticleViewTest {
/** @var ParserCache */
private $parserCache = null;
/**
* @param WikiPage $page
* @param RevisionRecord|null $rev
* @param ParserOptions|null $options
*
* @return PoolWorkArticleViewCurrent
*/
protected function newPoolWorkArticleView(
WikiPage $page,
?RevisionRecord $rev = null,
$options = null
) {
if ( !$options ) {
$options = ParserOptions::newFromAnon();
}
if ( !$rev ) {
$rev = $page->getRevisionRecord();
}
$parserCache = $this->parserCache ?: $this->installParserCache();
return new PoolWorkArticleViewCurrent(
'test:' . $rev->getId(),
$page,
$rev,
$options,
$this->getServiceContainer()->getRevisionRenderer(),
$parserCache,
$this->getServiceContainer()->getConnectionProvider(),
$this->getServiceContainer()->getChronologyProtector(),
$this->getLoggerSpi(),
$this->getServiceContainer()->getWikiPageFactory()
);
}
private function installParserCache( $bag = null ) {
$this->parserCache = new ParserCache(
'test',
$bag ?: new HashBagOStuff(),
'',
$this->getServiceContainer()->getHookContainer(),
new JsonCodec(),
StatsFactory::newNull(),
new NullLogger(),
$this->getServiceContainer()->getTitleFactory(),
$this->getServiceContainer()->getWikiPageFactory(),
$this->getServiceContainer()->getGlobalIdGenerator()
);
return $this->parserCache;
}
public function testUpdateCachedOutput() {
$options = ParserOptions::newFromAnon();
$page = $this->getExistingTestPage( __METHOD__ );
$parserCache = $this->installParserCache();
// rendering of a deleted revision should work, audience checks are bypassed
$work = $this->newPoolWorkArticleView( $page, null, $options );
/** @var Status $status */
$status = $work->execute();
$this->assertStatusGood( $status );
$cachedOutput = $parserCache->get( $page, $options );
$this->assertNotEmpty( $cachedOutput );
$this->assertSame( $status->getValue()->getRawText(), $cachedOutput->getRawText() );
}
/**
* Test that cache miss is not cached in-process, so pool work can fetch
* a parse cached by other pool work after waiting for a lock. See T277829
*/
public function testFetchAfterMissWithLock() {
$bag = new HashBagOStuff();
$options = ParserOptions::newFromAnon();
$page = $this->getExistingTestPage( __METHOD__ );
$this->installParserCache( $bag );
$work1 = $this->newPoolWorkArticleView( $page, null, $options );
$this->assertFalse( $work1->getCachedWork() );
// Pretend we're in another process with another ParserCache,
// but share the backend store
$this->installParserCache( $bag );
$work2 = $this->newPoolWorkArticleView( $page, null, $options );
/** @var Status $status2 */
$status2 = $work2->execute();
$this->assertStatusGood( $status2 );
// The parser output cached but $work2 should now be also visible to $work1
$status1 = $work1->getCachedWork();
$this->assertInstanceOf( ParserOutput::class, $status1->getValue() );
$this->assertSame( $status2->getValue()->getText(), $status1->getValue()->getText() );
}
public function testFallbackFromOutdatedParserCache() {
// Fake Unix timestamps
$lastWrite = 10;
$outdated = $lastWrite;
$chronologyProtector = $this->createNoOpMock( ChronologyProtector::class, [ 'getTouched' ] );
$chronologyProtector->method( 'getTouched' )->willReturn( $lastWrite );
$output = $this->createNoOpMock( ParserOutput::class, [ 'getCacheTime' ] );
$output->method( 'getCacheTime' )->willReturn( $outdated );
$this->parserCache = $this->createNoOpMock( ParserCache::class, [ 'getDirty' ] );
$this->parserCache->method( 'getDirty' )->willReturn( $output );
$work = $this->newPoolWorkArticleView(
$this->createMock( WikiPage::class ),
$this->createMock( RevisionRecord::class )
);
TestingAccessWrapper::newFromObject( $work )->chronologyProtector = $chronologyProtector;
$this->assertFalse( $work->fallback( true ) );
$status = $work->fallback( false );
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
$this->assertStatusWarning( 'view-pool-overload', $status );
}
public function testFallbackFromMoreRecentParserCache() {
// Fake Unix timestamps
$lastWrite = 10;
$moreRecent = $lastWrite + 1;
$chronologyProtector = $this->createNoOpMock( ChronologyProtector::class, [ 'getTouched' ] );
$chronologyProtector->method( 'getTouched' )->willReturn( $lastWrite );
$output = $this->createNoOpMock( ParserOutput::class, [ 'getCacheTime' ] );
$output->method( 'getCacheTime' )->willReturn( $moreRecent );
$this->parserCache = $this->createNoOpMock( ParserCache::class, [ 'getDirty' ] );
$this->parserCache->method( 'getDirty' )->willReturn( $output );
$work = $this->newPoolWorkArticleView(
$this->createMock( WikiPage::class ),
$this->createMock( RevisionRecord::class )
);
TestingAccessWrapper::newFromObject( $work )->chronologyProtector = $chronologyProtector;
$status = $work->fallback( true );
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
$this->assertStatusWarning( 'view-pool-contention', $status );
$status = $work->fallback( false );
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
$this->assertStatusWarning( 'view-pool-overload', $status );
}
}
|