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