3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\WikiMap\WikiMap
;
5 use Wikimedia\ObjectCache\HashBagOStuff
;
6 use Wikimedia\ObjectCache\WANObjectCache
;
14 class JobQueueTest
extends MediaWikiIntegrationTestCase
{
15 protected ?JobQueue
$queueRand;
16 protected ?JobQueue
$queueRandTTL;
17 protected ?JobQueue
$queueTimestamp;
18 protected ?JobQueue
$queueTimestampTTL;
19 protected ?JobQueue
$queueFifo;
20 protected ?JobQueue
$queueFifoTTL;
22 protected function setUp(): void
{
23 global $wgJobTypeConf;
26 $services = $this->getServiceContainer();
27 if ( $this->getCliArg( 'use-jobqueue' ) ) {
28 $name = $this->getCliArg( 'use-jobqueue' );
29 if ( !isset( $wgJobTypeConf[$name] ) ) {
30 throw new RuntimeException( "No \$wgJobTypeConf entry for '$name'." );
32 $baseConfig = $wgJobTypeConf[$name];
34 $baseConfig = [ 'class' => JobQueueDBSingle
::class ];
36 $baseConfig['type'] = 'null';
37 $baseConfig['domain'] = WikiMap
::getCurrentWikiDbDomain()->getId();
38 $baseConfig['stash'] = new HashBagOStuff();
39 $baseConfig['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
40 $baseConfig['idGenerator'] = $services->getGlobalIdGenerator();
42 'queueRand' => [ 'order' => 'random', 'claimTTL' => 0 ],
43 'queueRandTTL' => [ 'order' => 'random', 'claimTTL' => 10 ],
44 'queueTimestamp' => [ 'order' => 'timestamp', 'claimTTL' => 0 ],
45 'queueTimestampTTL' => [ 'order' => 'timestamp', 'claimTTL' => 10 ],
46 'queueFifo' => [ 'order' => 'fifo', 'claimTTL' => 0 ],
47 'queueFifoTTL' => [ 'order' => 'fifo', 'claimTTL' => 10 ],
49 foreach ( $variants as $q => $settings ) {
50 $this->$q = JobQueue
::factory( $settings +
$baseConfig );
54 protected function tearDown(): void
{
57 'queueRand', 'queueRandTTL', 'queueTimestamp', 'queueTimestampTTL',
58 'queueFifo', 'queueFifoTTL'
70 * @dataProvider provider_queueLists
72 public function testGetType( $queue, $recycles, $desc ) {
73 $queue = $this->$queue;
75 $this->markTestSkipped( $desc );
77 $this->assertEquals( 'null', $queue->getType(), "Proper job type ($desc)" );
81 * @dataProvider provider_queueLists
83 public function testBasicOperations( $queue, $recycles, $desc ) {
84 $queue = $this->$queue;
86 $this->markTestSkipped( $desc );
89 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
91 $queue->flushCaches();
92 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
93 $this->assertSame( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
95 $this->assertNull( $queue->push( $this->newJob() ), "Push worked ($desc)" );
96 $this->assertNull( $queue->batchPush( [ $this->newJob() ] ), "Push worked ($desc)" );
98 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
100 $queue->flushCaches();
101 $this->assertEquals( 2, $queue->getSize(), "Queue size is correct ($desc)" );
102 $this->assertSame( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
103 $jobs = iterator_to_array( $queue->getAllQueuedJobs() );
104 $this->assertCount( 2, $jobs, "Queue iterator size is correct ($desc)" );
106 $job1 = $queue->pop();
107 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
109 $queue->flushCaches();
110 $this->assertSame( 1, $queue->getSize(), "Queue size is correct ($desc)" );
112 $queue->flushCaches();
114 $this->assertSame( 1, $queue->getAcquiredCount(), "Active job count ($desc)" );
117 $job2 = $queue->pop();
118 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
119 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
121 $queue->flushCaches();
123 $this->assertEquals( 2, $queue->getAcquiredCount(), "Active job count ($desc)" );
126 $queue->ack( $job1 );
128 $queue->flushCaches();
130 $this->assertSame( 1, $queue->getAcquiredCount(), "Active job count ($desc)" );
133 $queue->ack( $job2 );
135 $queue->flushCaches();
136 $this->assertSame( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
138 $this->assertNull( $queue->batchPush( [ $this->newJob(), $this->newJob() ] ),
139 "Push worked ($desc)" );
140 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
143 $queue->flushCaches();
144 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
145 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
149 * @dataProvider provider_queueLists
151 public function testBasicDeduplication( $queue, $recycles, $desc ) {
152 $queue = $this->$queue;
154 $this->markTestSkipped( $desc );
157 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
159 $queue->flushCaches();
160 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
161 $this->assertSame( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
165 [ $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() ]
167 "Push worked ($desc)" );
169 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
171 $queue->flushCaches();
172 $this->assertSame( 1, $queue->getSize(), "Queue size is correct ($desc)" );
173 $this->assertSame( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
177 [ $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() ]
179 "Push worked ($desc)"
182 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
184 $queue->flushCaches();
185 $this->assertSame( 1, $queue->getSize(), "Queue size is correct ($desc)" );
186 $this->assertSame( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
188 $job1 = $queue->pop();
189 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
191 $queue->flushCaches();
192 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
194 $this->assertSame( 1, $queue->getAcquiredCount(), "Active job count ($desc)" );
197 $queue->ack( $job1 );
199 $queue->flushCaches();
200 $this->assertSame( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
204 * @dataProvider provider_queueLists
206 public function testDeduplicationWhileClaimed( $queue, $recycles, $desc ) {
207 $queue = $this->$queue;
209 $this->markTestSkipped( $desc );
212 $job = $this->newDedupedJob();
213 $queue->push( $job );
215 // De-duplication does not apply to already-claimed jobs
217 $queue->push( $job );
221 // Make sure ack() of the twin did not delete the sibling data
222 $this->assertInstanceOf( NullJob
::class, $j );
226 * @dataProvider provider_queueLists
228 public function testRootDeduplication( $queue, $recycles, $desc ) {
229 $queue = $this->$queue;
231 $this->markTestSkipped( $desc );
234 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
236 $queue->flushCaches();
237 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
238 $this->assertSame( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
240 $root1 = Job
::newRootJobParams( "nulljobspam:testId" ); // task ID/timestamp
241 for ( $i = 0; $i < 5; ++
$i ) {
242 $this->assertNull( $queue->push( $this->newJob( 0, $root1 ) ), "Push worked ($desc)" );
244 $queue->deduplicateRootJob( $this->newJob( 0, $root1 ) );
247 # Add a second to UNIX epoch and format back to TS_MW
248 $root2_ts = strtotime( $root2['rootJobTimestamp'] );
250 $root2['rootJobTimestamp'] = wfTimestamp( TS_MW
, $root2_ts );
252 $this->assertNotEquals( $root1['rootJobTimestamp'], $root2['rootJobTimestamp'],
253 "Root job signatures have different timestamps." );
254 for ( $i = 0; $i < 5; ++
$i ) {
255 $this->assertNull( $queue->push( $this->newJob( 0, $root2 ) ), "Push worked ($desc)" );
257 $queue->deduplicateRootJob( $this->newJob( 0, $root2 ) );
259 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
261 $queue->flushCaches();
262 $this->assertEquals( 10, $queue->getSize(), "Queue size is correct ($desc)" );
263 $this->assertSame( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
268 $job = $queue->pop();
273 if ( $job instanceof DuplicateJob
) {
278 $this->assertCount( 10, $jobs, "Correct number of jobs popped ($desc)" );
279 $this->assertEquals( 5, $dupcount, "Correct number of duplicate jobs popped ($desc)" );
283 * @dataProvider provider_fifoQueueLists
285 public function testJobOrder( $queue, $recycles, $desc ) {
286 $queue = $this->$queue;
288 $this->markTestSkipped( $desc );
291 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
293 $queue->flushCaches();
294 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
295 $this->assertSame( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
297 for ( $i = 0; $i < 10; ++
$i ) {
298 $this->assertNull( $queue->push( $this->newJob( $i ) ), "Push worked ($desc)" );
301 for ( $i = 0; $i < 10; ++
$i ) {
302 $job = $queue->pop();
303 $this->assertTrue( $job instanceof Job
, "Jobs popped from queue ($desc)" );
304 $params = $job->getParams();
305 $this->assertEquals( $i, $params['i'], "Job popped from queue is FIFO ($desc)" );
309 $this->assertFalse( $queue->pop(), "Queue is not empty ($desc)" );
311 $queue->flushCaches();
312 $this->assertSame( 0, $queue->getSize(), "Queue is empty ($desc)" );
313 $this->assertSame( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
316 public function testQueueAggregateTable() {
317 $this->hideDeprecated( 'JobQueue::getWiki' );
319 $queue = $this->queueFifo
;
320 if ( !$queue ||
!method_exists( $queue, 'getServerQueuesWithJobs' ) ) {
321 $this->markTestSkipped();
324 $this->assertNotContains(
325 [ $queue->getType(), $queue->getWiki() ],
326 $queue->getServerQueuesWithJobs(),
327 "Null queue not in listing"
330 $queue->push( $this->newJob( 0 ) );
332 $this->assertContains(
333 [ $queue->getType(), $queue->getWiki() ],
334 $queue->getServerQueuesWithJobs(),
335 "Null queue in listing"
339 public static function provider_queueLists() {
341 [ 'queueRand', false, 'Random queue without ack()' ],
342 [ 'queueRandTTL', true, 'Random queue with ack()' ],
343 [ 'queueTimestamp', false, 'Time ordered queue without ack()' ],
344 [ 'queueTimestampTTL', true, 'Time ordered queue with ack()' ],
345 [ 'queueFifo', false, 'FIFO ordered queue without ack()' ],
346 [ 'queueFifoTTL', true, 'FIFO ordered queue with ack()' ]
350 public static function provider_fifoQueueLists() {
352 [ 'queueFifo', false, 'Ordered queue without ack()' ],
353 [ 'queueFifoTTL', true, 'Ordered queue with ack()' ]
357 protected function newJob( $i = 0, $rootJob = [] ) {
359 'namespace' => NS_MAIN
,
360 'title' => 'Main_Page',
363 'removeDuplicates' => 0,
367 return $this->getServiceContainer()->getJobFactory()->newJob( 'null', $params );
370 protected function newDedupedJob( $i = 0, $rootJob = [] ) {
372 'namespace' => NS_MAIN
,
373 'title' => 'Main_Page',
376 'removeDuplicates' => 1,
380 return $this->getServiceContainer()->getJobFactory()->newJob( 'null', $params );
384 class JobQueueDBSingle
extends JobQueueDB
{
385 protected function getDB( $index ) {
386 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
387 // Override to not use CONN_TRX_AUTOCOMMIT so that we see the same temporary `job` table
388 return $lb->getConnection( $index, [], $this->domain
);