Add simple JobQueueMemory class for testing
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 13 Jan 2016 05:51:09 +0000 (21:51 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 14 Jan 2016 00:23:05 +0000 (00:23 +0000)
Change-Id: I1acce43dbab3b12393d6dc4675850695c619d88a

autoload.php
includes/jobqueue/JobQueueMemory.php [new file with mode: 0644]
tests/phpunit/phpunit.php

index 7fde81c..7dec6b7 100644 (file)
@@ -611,6 +611,7 @@ $wgAutoloadLocalClasses = array(
        'JobQueueError' => __DIR__ . '/includes/jobqueue/JobQueue.php',
        'JobQueueFederated' => __DIR__ . '/includes/jobqueue/JobQueueFederated.php',
        'JobQueueGroup' => __DIR__ . '/includes/jobqueue/JobQueueGroup.php',
+       'JobQueueMemory' => __DIR__ . '/includes/jobqueue/JobQueueMemory.php',
        'JobQueueRedis' => __DIR__ . '/includes/jobqueue/JobQueueRedis.php',
        'JobRunner' => __DIR__ . '/includes/jobqueue/JobRunner.php',
        'JobSpecification' => __DIR__ . '/includes/jobqueue/JobSpecification.php',
diff --git a/includes/jobqueue/JobQueueMemory.php b/includes/jobqueue/JobQueueMemory.php
new file mode 100644 (file)
index 0000000..c5d7257
--- /dev/null
@@ -0,0 +1,169 @@
+<?php
+/**
+ * PHP memory-backed job queue code.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Aaron Schulz
+ */
+
+/**
+ * Class to handle job queues stored in PHP memory for testing
+ *
+ * JobQueueGroup does not remember every queue instance, so statically track it here
+ *
+ * @ingroup JobQueue
+ * @since 1.27
+ */
+class JobQueueMemory extends JobQueue {
+       /** @var array[] */
+       protected static $data = array();
+
+       protected function doBatchPush( array $jobs, $flags ) {
+               $unclaimed =& $this->getQueueData( 'unclaimed', array() );
+
+               /** @var IJobSpecification[] $jobs */
+               foreach ( $jobs as $job ) {
+                       if ( $job->ignoreDuplicates() ) {
+                               $sha1 = Wikimedia\base_convert(
+                                       sha1( serialize( $job->getDeduplicationInfo() ) ),
+                                       16, 36, 31
+                               );
+                               if ( !isset( $unclaimed[$sha1] ) ) {
+                                       $unclaimed[$sha1] = $job;
+                               }
+                       } else {
+                               $unclaimed[] = $job;
+                       }
+               }
+       }
+
+       protected function supportedOrders() {
+               return array( 'random', 'timestamp', 'fifo' );
+       }
+
+       protected function optimalOrder() {
+               return array( 'fifo' );
+       }
+
+       protected function doIsEmpty() {
+               return ( $this->doGetSize() == 0 );
+       }
+
+       protected function doGetSize() {
+               $unclaimed = $this->getQueueData( 'unclaimed' );
+
+               return $unclaimed ? count( $unclaimed ) : 0;
+       }
+
+       protected function doGetAcquiredCount() {
+               $claimed = $this->getQueueData( 'claimed' );
+
+               return $claimed ? count( $claimed ) : 0;
+       }
+
+       protected function doPop() {
+               if ( $this->doGetSize() == 0 ) {
+                       return false;
+               }
+
+               $unclaimed =& $this->getQueueData( 'unclaimed' );
+               $claimed =& $this->getQueueData( 'claimed', array() );
+
+               if ( $this->order === 'random' ) {
+                       $key = array_rand( $unclaimed );
+               } else {
+                       reset( $unclaimed );
+                       $key = key( $unclaimed );
+               }
+
+               $spec = $unclaimed[$key];
+               unset( $unclaimed[$key] );
+               $claimed[] = $spec;
+
+               $job = $this->jobFromSpecInternal( $spec );
+
+               end( $claimed );
+               $job->metadata['claimId'] = key( $claimed );
+
+               return $job;
+       }
+
+       protected function doAck( Job $job ) {
+               if ( $this->getAcquiredCount() == 0 ) {
+                       return;
+               }
+
+               $claimed =& $this->getQueueData( 'claimed' );
+               unset( $claimed[$job->metadata['claimId']] );
+       }
+
+       protected function doDelete() {
+               if ( isset( self::$data[$this->type][$this->wiki] ) ) {
+                       unset( self::$data[$this->type][$this->wiki] );
+                       if ( !self::$data[$this->type] ) {
+                               unset( self::$data[$this->type] );
+                       }
+               }
+       }
+
+       public function getAllQueuedJobs() {
+               $unclaimed = $this->getQueueData( 'unclaimed' );
+               if ( !$unclaimed ) {
+                       return new ArrayIterator( array() );
+               }
+
+               $that = $this;
+               return new MappedIterator(
+                       $unclaimed,
+                       function ( $value ) use ( $that ) {
+                               $that->jobFromSpecInternal( $value );
+                       }
+               );
+       }
+
+       public function getAllAcquiredJobs() {
+               $claimed = $this->getQueueData( 'claimed' );
+               if ( !$claimed ) {
+                       return new ArrayIterator( array() );
+               }
+
+               $that = $this;
+               return new MappedIterator(
+                       $claimed,
+                       function ( $value ) use ( $that ) {
+                               $that->jobFromSpecInternal( $value );
+                       }
+               );
+       }
+
+       public function jobFromSpecInternal( IJobSpecification $spec ) {
+               return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
+       }
+
+       private function &getQueueData( $field, $init = null ) {
+               if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
+                       if ( $init !== null ) {
+                               self::$data[$this->type][$this->wiki][$field] = $init;
+                       } else {
+                               return null;
+                       }
+               }
+
+               return self::$data[$this->type][$this->wiki][$field];
+       }
+}
index ee0358c..0ae0b21 100755 (executable)
@@ -75,6 +75,7 @@ class PHPUnitMaintClass extends Maintenance {
                global $wgLocaltimezone, $wgLocalisationCacheConf;
                global $wgDevelopmentWarnings;
                global $wgSessionProviders;
+               global $wgJobTypeConf;
 
                // Inject test autoloader
                require_once __DIR__ . '/../TestsAutoLoader.php';
@@ -97,6 +98,10 @@ class PHPUnitMaintClass extends Maintenance {
                $wgLanguageConverterCacheType = 'hash';
                // Uses db-replicated in DefaultSettings
                $wgMainStash = 'hash';
+               // Use memory job queue
+               $wgJobTypeConf = array(
+                       'default' => array( 'class' => 'JobQueueMemory', 'order' => 'fifo' ),
+               );
 
                $wgUseDatabaseMessages = false; # Set for future resets