8 class JobQueueTest
extends MediaWikiTestCase
{
10 protected $queueRand, $queueRandTTL, $queueFifo, $queueFifoTTL;
12 function __construct( $name = null, array $data = array(), $dataName = '' ) {
13 parent
::__construct( $name, $data, $dataName );
15 $this->tablesUsed
[] = 'job';
18 protected function setUp() {
19 global $wgJobTypeConf;
22 $this->setMwGlobals( 'wgMemc', new HashBagOStuff() );
24 if ( $this->getCliArg( 'use-jobqueue=' ) ) {
25 $name = $this->getCliArg( 'use-jobqueue=' );
26 if ( !isset( $wgJobTypeConf[$name] ) ) {
27 throw new MWException( "No \$wgJobTypeConf entry for '$name'." );
29 $baseConfig = $wgJobTypeConf[$name];
31 $baseConfig = array( 'class' => 'JobQueueDB' );
33 $baseConfig['type'] = 'null';
34 $baseConfig['wiki'] = wfWikiID();
36 'queueRand' => array( 'order' => 'random', 'claimTTL' => 0 ),
37 'queueRandTTL' => array( 'order' => 'random', 'claimTTL' => 10 ),
38 'queueTimestamp' => array( 'order' => 'timestamp', 'claimTTL' => 0 ),
39 'queueTimestampTTL' => array( 'order' => 'timestamp', 'claimTTL' => 10 ),
40 'queueFifo' => array( 'order' => 'fifo', 'claimTTL' => 0 ),
41 'queueFifoTTL' => array( 'order' => 'fifo', 'claimTTL' => 10 ),
43 foreach ( $variants as $q => $settings ) {
45 $this->$q = JobQueue
::factory( $settings +
$baseConfig );
46 if ( !( $this->$q instanceof JobQueueDB
) ) {
47 $this->$q->setTestingPrefix( 'unittests-' . wfRandomString( 32 ) );
49 } catch ( MWException
$e ) {
51 // @todo What if it was another error?
56 protected function tearDown() {
60 'queueRand', 'queueRandTTL', 'queueTimestamp', 'queueTimestampTTL',
61 'queueFifo', 'queueFifoTTL'
72 * @dataProvider provider_queueLists
73 * @covers JobQueue::getWiki
75 public function testGetWiki( $queue, $recycles, $desc ) {
76 $queue = $this->$queue;
78 $this->markTestSkipped( $desc );
80 $this->assertEquals( wfWikiID(), $queue->getWiki(), "Proper wiki ID ($desc)" );
84 * @dataProvider provider_queueLists
85 * @covers JobQueue::getType
87 public function testGetType( $queue, $recycles, $desc ) {
88 $queue = $this->$queue;
90 $this->markTestSkipped( $desc );
92 $this->assertEquals( 'null', $queue->getType(), "Proper job type ($desc)" );
96 * @dataProvider provider_queueLists
99 public function testBasicOperations( $queue, $recycles, $desc ) {
100 $queue = $this->$queue;
102 $this->markTestSkipped( $desc );
105 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
107 $queue->flushCaches();
108 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
109 $this->assertEquals( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
111 $this->assertNull( $queue->push( $this->newJob() ), "Push worked ($desc)" );
112 $this->assertNull( $queue->batchPush( array( $this->newJob() ) ), "Push worked ($desc)" );
114 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
116 $queue->flushCaches();
117 $this->assertEquals( 2, $queue->getSize(), "Queue size is correct ($desc)" );
118 $this->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
119 $jobs = iterator_to_array( $queue->getAllQueuedJobs() );
120 $this->assertEquals( 2, count( $jobs ), "Queue iterator size is correct ($desc)" );
122 $job1 = $queue->pop();
123 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
125 $queue->flushCaches();
126 $this->assertEquals( 1, $queue->getSize(), "Queue size is correct ($desc)" );
128 $queue->flushCaches();
130 $this->assertEquals( 1, $queue->getAcquiredCount(), "Active job count ($desc)" );
132 $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
135 $job2 = $queue->pop();
136 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
137 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
139 $queue->flushCaches();
141 $this->assertEquals( 2, $queue->getAcquiredCount(), "Active job count ($desc)" );
143 $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
146 $queue->ack( $job1 );
148 $queue->flushCaches();
150 $this->assertEquals( 1, $queue->getAcquiredCount(), "Active job count ($desc)" );
152 $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
155 $queue->ack( $job2 );
157 $queue->flushCaches();
158 $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
160 $this->assertNull( $queue->batchPush( array( $this->newJob(), $this->newJob() ) ),
161 "Push worked ($desc)" );
162 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
165 $queue->flushCaches();
166 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
167 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
171 * @dataProvider provider_queueLists
174 public function testBasicDeduplication( $queue, $recycles, $desc ) {
175 $queue = $this->$queue;
177 $this->markTestSkipped( $desc );
180 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
182 $queue->flushCaches();
183 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
184 $this->assertEquals( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
188 array( $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() )
190 "Push worked ($desc)" );
192 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
194 $queue->flushCaches();
195 $this->assertEquals( 1, $queue->getSize(), "Queue size is correct ($desc)" );
196 $this->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
200 array( $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() )
202 "Push worked ($desc)"
205 $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
207 $queue->flushCaches();
208 $this->assertEquals( 1, $queue->getSize(), "Queue size is correct ($desc)" );
209 $this->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
211 $job1 = $queue->pop();
212 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
214 $queue->flushCaches();
215 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
217 $this->assertEquals( 1, $queue->getAcquiredCount(), "Active job count ($desc)" );
219 $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
222 $queue->ack( $job1 );
224 $queue->flushCaches();
225 $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
229 * @dataProvider provider_queueLists
232 public function testRootDeduplication( $queue, $recycles, $desc ) {
233 $queue = $this->$queue;
235 $this->markTestSkipped( $desc );
238 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
240 $queue->flushCaches();
241 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
242 $this->assertEquals( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
244 $id = wfRandomString( 32 );
245 $root1 = Job
::newRootJobParams( "nulljobspam:$id" ); // task ID/timestamp
246 for ( $i = 0; $i < 5; ++
$i ) {
247 $this->assertNull( $queue->push( $this->newJob( 0, $root1 ) ), "Push worked ($desc)" );
249 $queue->deduplicateRootJob( $this->newJob( 0, $root1 ) );
250 sleep( 1 ); // roo job timestamp will increase
251 $root2 = Job
::newRootJobParams( "nulljobspam:$id" ); // task ID/timestamp
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->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
268 $job = $queue->pop();
273 if ( $job instanceof DuplicateJob
) {
278 $this->assertEquals( 10, count( $jobs ), "Correct number of jobs popped ($desc)" );
279 $this->assertEquals( 5, $dupcount, "Correct number of duplicate jobs popped ($desc)" );
283 * @dataProvider provider_fifoQueueLists
286 public function testJobOrder( $queue, $recycles, $desc ) {
287 $queue = $this->$queue;
289 $this->markTestSkipped( $desc );
292 $this->assertTrue( $queue->isEmpty(), "Queue is empty ($desc)" );
294 $queue->flushCaches();
295 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
296 $this->assertEquals( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
298 for ( $i = 0; $i < 10; ++
$i ) {
299 $this->assertNull( $queue->push( $this->newJob( $i ) ), "Push worked ($desc)" );
302 for ( $i = 0; $i < 10; ++
$i ) {
303 $job = $queue->pop();
304 $this->assertTrue( $job instanceof Job
, "Jobs popped from queue ($desc)" );
305 $params = $job->getParams();
306 $this->assertEquals( $i, $params['i'], "Job popped from queue is FIFO ($desc)" );
310 $this->assertFalse( $queue->pop(), "Queue is not empty ($desc)" );
312 $queue->flushCaches();
313 $this->assertEquals( 0, $queue->getSize(), "Queue is empty ($desc)" );
314 $this->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
317 public static function provider_queueLists() {
319 array( 'queueRand', false, 'Random queue without ack()' ),
320 array( 'queueRandTTL', true, 'Random queue with ack()' ),
321 array( 'queueTimestamp', false, 'Time ordered queue without ack()' ),
322 array( 'queueTimestampTTL', true, 'Time ordered queue with ack()' ),
323 array( 'queueFifo', false, 'FIFO ordered queue without ack()' ),
324 array( 'queueFifoTTL', true, 'FIFO ordered queue with ack()' )
328 public static function provider_fifoQueueLists() {
330 array( 'queueFifo', false, 'Ordered queue without ack()' ),
331 array( 'queueFifoTTL', true, 'Ordered queue with ack()' )
335 function newJob( $i = 0, $rootJob = array() ) {
336 return new NullJob( Title
::newMainPage(),
337 array( 'lives' => 0, 'usleep' => 0, 'removeDuplicates' => 0, 'i' => $i ) +
$rootJob );
340 function newDedupedJob( $i = 0, $rootJob = array() ) {
341 return new NullJob( Title
::newMainPage(),
342 array( 'lives' => 0, 'usleep' => 0, 'removeDuplicates' => 1, 'i' => $i ) +
$rootJob );