rdbms: specify DB name and table prefix even for the local domain
[lhc/web/wiklou.git] / tests / phpunit / includes / jobqueue / JobQueueTest.php
index 9808a55..bd21dc8 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * @group JobQueue
  * @group medium
@@ -9,7 +11,7 @@ class JobQueueTest extends MediaWikiTestCase {
        protected $key;
        protected $queueRand, $queueRandTTL, $queueFifo, $queueFifoTTL;
 
-       function __construct( $name = null, array $data = array(), $dataName = '' ) {
+       function __construct( $name = null, array $data = [], $dataName = '' ) {
                parent::__construct( $name, $data, $dataName );
 
                $this->tablesUsed[] = 'job';
@@ -19,8 +21,6 @@ class JobQueueTest extends MediaWikiTestCase {
                global $wgJobTypeConf;
                parent::setUp();
 
-               $this->setMwGlobals( 'wgMemc', new HashBagOStuff() );
-
                if ( $this->getCliArg( 'use-jobqueue' ) ) {
                        $name = $this->getCliArg( 'use-jobqueue' );
                        if ( !isset( $wgJobTypeConf[$name] ) ) {
@@ -28,24 +28,21 @@ class JobQueueTest extends MediaWikiTestCase {
                        }
                        $baseConfig = $wgJobTypeConf[$name];
                } else {
-                       $baseConfig = array( 'class' => 'JobQueueDB' );
+                       $baseConfig = [ 'class' => 'JobQueueDBSingle' ];
                }
                $baseConfig['type'] = 'null';
                $baseConfig['wiki'] = wfWikiID();
-               $variants = array(
-                       'queueRand' => array( 'order' => 'random', 'claimTTL' => 0 ),
-                       'queueRandTTL' => array( 'order' => 'random', 'claimTTL' => 10 ),
-                       'queueTimestamp' => array( 'order' => 'timestamp', 'claimTTL' => 0 ),
-                       'queueTimestampTTL' => array( 'order' => 'timestamp', 'claimTTL' => 10 ),
-                       'queueFifo' => array( 'order' => 'fifo', 'claimTTL' => 0 ),
-                       'queueFifoTTL' => array( 'order' => 'fifo', 'claimTTL' => 10 ),
-               );
+               $variants = [
+                       'queueRand' => [ 'order' => 'random', 'claimTTL' => 0 ],
+                       'queueRandTTL' => [ 'order' => 'random', 'claimTTL' => 10 ],
+                       'queueTimestamp' => [ 'order' => 'timestamp', 'claimTTL' => 0 ],
+                       'queueTimestampTTL' => [ 'order' => 'timestamp', 'claimTTL' => 10 ],
+                       'queueFifo' => [ 'order' => 'fifo', 'claimTTL' => 0 ],
+                       'queueFifoTTL' => [ 'order' => 'fifo', 'claimTTL' => 10 ],
+               ];
                foreach ( $variants as $q => $settings ) {
                        try {
                                $this->$q = JobQueue::factory( $settings + $baseConfig );
-                               if ( !( $this->$q instanceof JobQueueDB ) ) {
-                                       $this->$q->setTestingPrefix( 'unittests-' . wfRandomString( 32 ) );
-                               }
                        } catch ( MWException $e ) {
                                // unsupported?
                                // @todo What if it was another error?
@@ -56,10 +53,10 @@ class JobQueueTest extends MediaWikiTestCase {
        protected function tearDown() {
                parent::tearDown();
                foreach (
-                       array(
+                       [
                                'queueRand', 'queueRandTTL', 'queueTimestamp', 'queueTimestampTTL',
                                'queueFifo', 'queueFifoTTL'
-                       ) as $q
+                       ] as $q
                ) {
                        if ( $this->$q ) {
                                $this->$q->delete();
@@ -109,7 +106,7 @@ class JobQueueTest extends MediaWikiTestCase {
                $this->assertEquals( 0, $queue->getAcquiredCount(), "Queue is empty ($desc)" );
 
                $this->assertNull( $queue->push( $this->newJob() ), "Push worked ($desc)" );
-               $this->assertNull( $queue->batchPush( array( $this->newJob() ) ), "Push worked ($desc)" );
+               $this->assertNull( $queue->batchPush( [ $this->newJob() ] ), "Push worked ($desc)" );
 
                $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
 
@@ -151,7 +148,7 @@ class JobQueueTest extends MediaWikiTestCase {
                $queue->flushCaches();
                $this->assertEquals( 0, $queue->getAcquiredCount(), "Active job count ($desc)" );
 
-               $this->assertNull( $queue->batchPush( array( $this->newJob(), $this->newJob() ) ),
+               $this->assertNull( $queue->batchPush( [ $this->newJob(), $this->newJob() ] ),
                        "Push worked ($desc)" );
                $this->assertFalse( $queue->isEmpty(), "Queue is not empty ($desc)" );
 
@@ -179,7 +176,7 @@ class JobQueueTest extends MediaWikiTestCase {
 
                $this->assertNull(
                        $queue->batchPush(
-                               array( $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() )
+                               [ $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() ]
                        ),
                        "Push worked ($desc)" );
 
@@ -191,7 +188,7 @@ class JobQueueTest extends MediaWikiTestCase {
 
                $this->assertNull(
                        $queue->batchPush(
-                               array( $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() )
+                               [ $this->newDedupedJob(), $this->newDedupedJob(), $this->newDedupedJob() ]
                        ),
                        "Push worked ($desc)"
                );
@@ -283,7 +280,7 @@ class JobQueueTest extends MediaWikiTestCase {
                $this->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
 
                $dupcount = 0;
-               $jobs = array();
+               $jobs = [];
                do {
                        $job = $queue->pop();
                        if ( $job ) {
@@ -334,31 +331,63 @@ class JobQueueTest extends MediaWikiTestCase {
                $this->assertEquals( 0, $queue->getAcquiredCount(), "No jobs active ($desc)" );
        }
 
-       public static function provider_queueLists() {
-               return array(
-                       array( 'queueRand', false, 'Random queue without ack()' ),
-                       array( 'queueRandTTL', true, 'Random queue with ack()' ),
-                       array( 'queueTimestamp', false, 'Time ordered queue without ack()' ),
-                       array( 'queueTimestampTTL', true, 'Time ordered queue with ack()' ),
-                       array( 'queueFifo', false, 'FIFO ordered queue without ack()' ),
-                       array( 'queueFifoTTL', true, 'FIFO ordered queue with ack()' )
+       /**
+        * @covers JobQueue
+        */
+       public function testQueueAggregateTable() {
+               $queue = $this->queueFifo;
+               if ( !$queue || !method_exists( $queue, 'getServerQueuesWithJobs' ) ) {
+                       $this->markTestSkipped();
+               }
+
+               $this->assertNotContains(
+                       [ $queue->getType(), $queue->getWiki() ],
+                       $queue->getServerQueuesWithJobs(),
+                       "Null queue not in listing"
                );
+
+               $queue->push( $this->newJob( 0 ) );
+
+               $this->assertContains(
+                       [ $queue->getType(), $queue->getWiki() ],
+                       $queue->getServerQueuesWithJobs(),
+                       "Null queue in listing"
+               );
+       }
+
+       public static function provider_queueLists() {
+               return [
+                       [ 'queueRand', false, 'Random queue without ack()' ],
+                       [ 'queueRandTTL', true, 'Random queue with ack()' ],
+                       [ 'queueTimestamp', false, 'Time ordered queue without ack()' ],
+                       [ 'queueTimestampTTL', true, 'Time ordered queue with ack()' ],
+                       [ 'queueFifo', false, 'FIFO ordered queue without ack()' ],
+                       [ 'queueFifoTTL', true, 'FIFO ordered queue with ack()' ]
+               ];
        }
 
        public static function provider_fifoQueueLists() {
-               return array(
-                       array( 'queueFifo', false, 'Ordered queue without ack()' ),
-                       array( 'queueFifoTTL', true, 'Ordered queue with ack()' )
-               );
+               return [
+                       [ 'queueFifo', false, 'Ordered queue without ack()' ],
+                       [ 'queueFifoTTL', true, 'Ordered queue with ack()' ]
+               ];
        }
 
-       function newJob( $i = 0, $rootJob = array() ) {
+       function newJob( $i = 0, $rootJob = [] ) {
                return new NullJob( Title::newMainPage(),
-                       array( 'lives' => 0, 'usleep' => 0, 'removeDuplicates' => 0, 'i' => $i ) + $rootJob );
+                       [ 'lives' => 0, 'usleep' => 0, 'removeDuplicates' => 0, 'i' => $i ] + $rootJob );
        }
 
-       function newDedupedJob( $i = 0, $rootJob = array() ) {
+       function newDedupedJob( $i = 0, $rootJob = [] ) {
                return new NullJob( Title::newMainPage(),
-                       array( 'lives' => 0, 'usleep' => 0, 'removeDuplicates' => 1, 'i' => $i ) + $rootJob );
+                       [ 'lives' => 0, 'usleep' => 0, 'removeDuplicates' => 1, 'i' => $i ] + $rootJob );
+       }
+}
+
+class JobQueueDBSingle extends JobQueueDB {
+       protected function getDB( $index ) {
+               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+               // Override to not use CONN_TRX_AUTO so that we see the same temporary `job` table
+               return $lb->getConnection( $index, [], $this->wiki );
        }
 }