Merge "deferred: Introduce TransactionRoundDefiningUpdate"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 13 Oct 2017 21:54:02 +0000 (21:54 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 13 Oct 2017 21:54:03 +0000 (21:54 +0000)
autoload.php
includes/deferred/DeferredUpdates.php
includes/deferred/TransactionRoundDefiningUpdate.php [new file with mode: 0644]
tests/phpunit/includes/deferred/DeferredUpdatesTest.php
tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php [new file with mode: 0644]

index abedffd..a489b8b 100644 (file)
@@ -1497,6 +1497,7 @@ $wgAutoloadLocalClasses = [
        'TrackBlobs' => __DIR__ . '/maintenance/storage/trackBlobs.php',
        'TrackingCategories' => __DIR__ . '/includes/TrackingCategories.php',
        'TraditionalImageGallery' => __DIR__ . '/includes/gallery/TraditionalImageGallery.php',
+       'TransactionRoundDefiningUpdate' => __DIR__ . '/includes/deferred/TransactionRoundDefiningUpdate.php',
        'TransformParameterError' => __DIR__ . '/includes/media/MediaTransformOutput.php',
        'TransformTooBigImageAreaError' => __DIR__ . '/includes/media/MediaTransformOutput.php',
        'TransformationalImageHandler' => __DIR__ . '/includes/media/TransformationalImageHandler.php',
index e8e250b..3c4833c 100644 (file)
@@ -250,6 +250,8 @@ class DeferredUpdates {
                                // Run only the job enqueue logic to complete the update later
                                $spec = $update->getAsJobSpecification();
                                JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
+                       } elseif ( $update instanceof TransactionRoundDefiningUpdate ) {
+                               $update->doUpdate();
                        } else {
                                // Run the bulk of the update now
                                $fnameTrxOwner = get_class( $update ) . '::doUpdate';
diff --git a/includes/deferred/TransactionRoundDefiningUpdate.php b/includes/deferred/TransactionRoundDefiningUpdate.php
new file mode 100644 (file)
index 0000000..65baec5
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Deferrable update for closure/callback updates that need LBFactory and Database
+ * to be outside any active transaction round.
+ *
+ * @since 1.31
+ */
+class TransactionRoundDefiningUpdate implements DeferrableUpdate, DeferrableCallback {
+       /** @var callable|null */
+       private $callback;
+       /** @var string */
+       private $fname;
+
+       /**
+        * @param callable $callback
+        * @param string $fname Calling method
+        */
+       public function __construct( callable $callback, $fname = 'unknown' ) {
+               $this->callback = $callback;
+               $this->fname = $fname;
+       }
+
+       public function doUpdate() {
+               call_user_func( $this->callback );
+       }
+
+       public function getOrigin() {
+               return $this->fname;
+       }
+}
index 999ad03..8332d2c 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 class DeferredUpdatesTest extends MediaWikiTestCase {
 
        /**
@@ -289,4 +291,46 @@ class DeferredUpdatesTest extends MediaWikiTestCase {
                $this->assertTrue( $x, "Outer POSTSEND update ran" );
                $this->assertTrue( $y, "Nested PRESEND update ran" );
        }
+
+       /**
+        * @covers DeferredUpdates::runUpdate
+        */
+       public function testRunUpdateTransactionScope() {
+               $this->setMwGlobals( 'wgCommandLineMode', false );
+
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' );
+
+               $ran = 0;
+               DeferredUpdates::addCallableUpdate( function () use ( &$ran, $lbFactory ) {
+                       $ran++;
+                       $this->assertTrue( $lbFactory->hasTransactionRound(), 'Has transaction' );
+               } );
+               DeferredUpdates::doUpdates();
+
+               $this->assertSame( 1, $ran, 'Update ran' );
+               $this->assertFalse( $lbFactory->hasTransactionRound(), 'Final state' );
+       }
+
+       /**
+        * @covers DeferredUpdates::runUpdate
+        * @covers TransactionRoundDefiningUpdate::getOrigin
+        */
+       public function testRunOuterScopeUpdate() {
+               $this->setMwGlobals( 'wgCommandLineMode', false );
+
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' );
+
+               $ran = 0;
+               DeferredUpdates::addUpdate( new TransactionRoundDefiningUpdate(
+                       function () use ( &$ran, $lbFactory ) {
+                               $ran++;
+                               $this->assertFalse( $lbFactory->hasTransactionRound(), 'No transaction' );
+                       } )
+               );
+               DeferredUpdates::doUpdates();
+
+               $this->assertSame( 1, $ran, 'Update ran' );
+       }
 }
diff --git a/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php b/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php
new file mode 100644 (file)
index 0000000..e6ad072
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @covers TransactionRoundDefiningUpdate
+ */
+class TransactionRoundDefiningUpdateTest extends PHPUnit_Framework_TestCase {
+
+       public function testDoUpdate() {
+               $ran = 0;
+               $update = new TransactionRoundDefiningUpdate( function () use ( &$ran ) {
+                       $ran++;
+               } );
+               $this->assertSame( 0, $ran );
+               $update->doUpdate();
+               $this->assertSame( 1, $ran );
+       }
+}