Debian Codegraph @

tests/phpunit/includes/deferred/RefreshSecondaryDataUpdateTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Deferred\DataUpdate;
4 use MediaWiki\Deferred\DeferredUpdates;
5 use MediaWiki\Deferred\RefreshSecondaryDataUpdate;
6 use MediaWiki\Revision\MutableRevisionRecord;
7 use MediaWiki\Storage\DerivedPageDataUpdater;
8 use MediaWiki\Title\Title;
9 use Psr\Log\NullLogger;
10 use Wikimedia\ScopedCallback;
11
12 /**
13 * @covers \MediaWiki\Deferred\RefreshSecondaryDataUpdate
14 * @group Database
15 */
16 class RefreshSecondaryDataUpdateTest extends MediaWikiIntegrationTestCase {
17 public function testSuccess() {
18 $services = $this->getServiceContainer();
19 $lbFactory = $services->getDBLoadBalancerFactory();
20 $queue = $services->getJobQueueGroup()->get( 'refreshLinksPrioritized' );
21 $user = $this->getTestUser()->getUser();
22
23 $goodCalls = 0;
24 $goodUpdate = $this->getMockBuilder( DataUpdate::class )
25 ->onlyMethods( [ 'doUpdate', 'setTransactionTicket' ] )
26 ->getMock();
27 $goodTrxFname = get_class( $goodUpdate ) . '::doUpdate';
28 $goodUpdate->method( 'doUpdate' )
29 ->willReturnCallback( static function () use ( &$goodCalls, $lbFactory, $goodTrxFname ) {
30 // Update can commit since it owns the transaction
31 $lbFactory->commitPrimaryChanges( $goodTrxFname );
32 ++$goodCalls;
33 } );
34 $goodUpdate->expects( $this->once() )
35 ->method( 'setTransactionTicket' );
36
37 $updater = $this->getMockBuilder( DerivedPageDataUpdater::class )
38 ->disableOriginalConstructor()
39 ->onlyMethods( [ 'getSecondaryDataUpdates' ] )
40 ->getMock();
41 $updater->method( 'getSecondaryDataUpdates' )
42 ->willReturn( [ $goodUpdate ] );
43
44 $revision = $this->getMockBuilder( MutableRevisionRecord::class )
45 ->disableOriginalConstructor()
46 ->onlyMethods( [ 'getId' ] )
47 ->getMock();
48 $revision->method( 'getId' )
49 ->willReturn( 42 );
50
51 $dbw = $this->getDb();
52
53 $dbw->startAtomic( __METHOD__ );
54
55 $this->assertSame( 0, $queue->getSize() );
56 $this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
57 $wikiPage = $services->getWikiPageFactory()->newFromTitle( Title::makeTitle( NS_MAIN, 'TestPage' ) );
58 DeferredUpdates::addUpdate( new RefreshSecondaryDataUpdate(
59 $lbFactory,
60 $user,
61 $wikiPage,
62 $revision,
63 $updater,
64 []
65 ) );
66 $this->assertSame( 1, DeferredUpdates::pendingUpdatesCount() );
67 $this->assertSame( 0, $queue->getSize() );
68
69 $dbw->endAtomic( __METHOD__ ); // run updates
70
71 $this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
72 $queue->flushCaches();
73 $this->assertSame( 0, $queue->getSize(), "Nothing failed; no enqueue" );
74 $this->assertSame( 1, $goodCalls );
75 }
76
77 public function testEnqueueOnFailure() {
78 $services = $this->getServiceContainer();
79 $lbFactory = $services->getDBLoadBalancerFactory();
80 $queue = $services->getJobQueueGroup()->get( 'refreshLinksPrioritized' );
81 $user = $this->getTestUser()->getUser();
82
83 // T248189: DeferredUpdate will log the exception, don't fail because of that.
84 $this->setLogger( 'exception', new NullLogger() );
85
86 $goodCalls = 0;
87 $goodUpdate = $this->getMockBuilder( DataUpdate::class )
88 ->onlyMethods( [ 'doUpdate', 'setTransactionTicket' ] )
89 ->getMock();
90 $goodTrxFname = get_class( $goodUpdate ) . '::doUpdate';
91 $goodUpdate->method( 'doUpdate' )
92 ->willReturnCallback( static function () use ( &$goodCalls, $lbFactory, $goodTrxFname ) {
93 // Update can commit since it owns the transaction
94 $lbFactory->commitPrimaryChanges( $goodTrxFname );
95 ++$goodCalls;
96 } );
97 $goodUpdate->expects( $this->once() )
98 ->method( 'setTransactionTicket' );
99
100 $badCalls = 0;
101 $badUpdate = $this->getMockBuilder( DataUpdate::class )
102 ->onlyMethods( [ 'doUpdate', 'setTransactionTicket' ] )
103 ->getMock();
104 $badTrxFname = get_class( $goodUpdate ) . '::doUpdate';
105 $badUpdate->expects( $this->once() )
106 ->method( 'setTransactionTicket' );
107 $badUpdate->method( 'doUpdate' )
108 ->willReturnCallback( static function () use ( &$badCalls, $lbFactory, $badTrxFname ) {
109 // Update can commit since it owns the transaction
110 $lbFactory->commitPrimaryChanges( $badTrxFname );
111 ++$badCalls;
112 throw new LogicException( 'We have a problem' );
113 } );
114
115 $updater = $this->getMockBuilder( DerivedPageDataUpdater::class )
116 ->disableOriginalConstructor()
117 ->onlyMethods( [ 'getSecondaryDataUpdates' ] )
118 ->getMock();
119 $updater->method( 'getSecondaryDataUpdates' )
120 ->willReturn( [ $goodUpdate, $badUpdate ] );
121
122 $revision = $this->getMockBuilder( MutableRevisionRecord::class )
123 ->disableOriginalConstructor()
124 ->onlyMethods( [ 'getId' ] )
125 ->getMock();
126 $revision->method( 'getId' )
127 ->willReturn( 42 );
128
129 $dbw = $this->getDb();
130 $dbw->startAtomic( __METHOD__ );
131 $goodCalls = 0;
132
133 $this->assertSame( 0, $queue->getSize() );
134 $this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
135 $wikiPage = $services->getWikiPageFactory()->newFromTitle( Title::makeTitle( NS_MAIN, 'TestPage' ) );
136 DeferredUpdates::addUpdate( new RefreshSecondaryDataUpdate(
137 $lbFactory,
138 $user,
139 $wikiPage,
140 $revision,
141 $updater,
142 []
143 ) );
144 $this->assertSame( 1, DeferredUpdates::pendingUpdatesCount() );
145 $this->assertSame( 0, $queue->getSize() );
146
147 try {
148 // Trigger deferred updates run to execute the update and secondary updates
149 $dbw->endAtomic( __METHOD__ );
150 // Callback rigged to fail
151 $this->fail( "Expected LogicException" );
152 } catch ( LogicException $e ) {
153 $this->assertSame( "We have a problem", $e->getMessage() );
154 }
155
156 $this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
157 $queue->flushCaches();
158 $this->assertSame( 1, $queue->getSize(), "Update failed; job enqueued" );
159 $this->assertSame( 1, $goodCalls );
160 $this->assertSame( 1, $badCalls );
161
162 // Run the RefreshLinksJob
163 $this->runJobs( [ 'ignoreErrorsMatchingFormat' => 'Revision %d is not current' ] );
164
165 $queue->flushCaches();
166 $this->assertSame( 0, $queue->getSize() );
167 }
168
169 /**
170 * Attempted use of onTransactionResolution() to avoid an update running on
171 * rollback shouldn't cause DeferredUpdates to fail to get a ticket.
172 */
173 public function testT248003() {
174 $services = $this->getServiceContainer();
175 $lbFactory = $services->getDBLoadBalancerFactory();
176 $user = $this->getTestUser()->getUser();
177
178 $fname = __METHOD__;
179 $dbw = $lbFactory->getMainLB()->getConnection( DB_PRIMARY );
180 $dbw->setFlag( DBO_TRX, $dbw::REMEMBER_PRIOR ); // make queries trigger TRX
181 $reset = new ScopedCallback( [ $dbw, 'restoreFlags' ] );
182
183 $this->assertSame( 0, $dbw->trxLevel() );
184 $dbw->newSelectQueryBuilder()
185 ->select( '*' )
186 ->from( 'page' )
187 ->caller( __METHOD__ )->fetchRow();
188 if ( !$dbw->trxLevel() ) {
189 $this->markTestSkipped( 'No implicit transaction, cannot test for T248003' );
190 }
191 $dbw->commit( __METHOD__, $dbw::FLUSHING_INTERNAL );
192 $this->assertSame( 0, $dbw->trxLevel() );
193
194 $goodCalls = 0;
195 $goodUpdate = $this->getMockBuilder( DataUpdate::class )
196 ->onlyMethods( [ 'doUpdate' ] )
197 ->getMock();
198 $goodUpdate->method( 'doUpdate' )
199 ->willReturnCallback( static function () use ( &$goodCalls ) {
200 ++$goodCalls;
201 } );
202
203 $updater = $this->getMockBuilder( DerivedPageDataUpdater::class )
204 ->disableOriginalConstructor()
205 ->onlyMethods( [ 'getSecondaryDataUpdates' ] )
206 ->getMock();
207 $updater->method( 'getSecondaryDataUpdates' )
208 ->willReturnCallback( static function () use ( $dbw, $fname, $goodUpdate ) {
209 $dbw->newSelectQueryBuilder()
210 ->select( '*' )
211 ->from( 'page' )
212 ->caller( $fname )->fetchRow();
213 $dbw->onTransactionResolution( static function () {
214 }, $fname );
215
216 return [ $goodUpdate ];
217 } );
218
219 $wikiPage = $this->getExistingTestPage();
220 $update = new RefreshSecondaryDataUpdate(
221 $lbFactory,
222 $user,
223 $wikiPage,
224 $wikiPage->getRevisionRecord(),
225 $updater,
226 []
227 );
228 $update->doUpdate();
229
230 $this->assertSame( 1, $goodCalls );
231 }
232 }