Fix typo in tests/phpunit/includes/Revision/MainSlotRoleHandlerTest.php
[lhc/web/wiklou.git] / tests / phpunit / includes / deferred / DeferredUpdatesTest.php
index 8332d2c..3662c26 100644 (file)
@@ -207,7 +207,8 @@ class DeferredUpdatesTest extends MediaWikiTestCase {
                ];
 
                // clear anything
-               wfGetLBFactory()->commitMasterChanges( __METHOD__ );
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $lbFactory->commitMasterChanges( __METHOD__ );
 
                DeferredUpdates::addCallableUpdate(
                        function () use ( $updates ) {
@@ -271,7 +272,8 @@ class DeferredUpdatesTest extends MediaWikiTestCase {
                $x = false;
                $y = false;
                // clear anything
-               wfGetLBFactory()->commitMasterChanges( __METHOD__ );
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $lbFactory->commitMasterChanges( __METHOD__ );
 
                DeferredUpdates::addCallableUpdate(
                        function () use ( &$x, &$y ) {
@@ -333,4 +335,42 @@ class DeferredUpdatesTest extends MediaWikiTestCase {
 
                $this->assertSame( 1, $ran, 'Update ran' );
        }
+
+       /**
+        * @covers DeferredUpdates::tryOpportunisticExecute
+        */
+       public function testTryOpportunisticExecute() {
+               $calls = [];
+               $callback1 = function () use ( &$calls ) {
+                       $calls[] = 1;
+               };
+               $callback2 = function () use ( &$calls ) {
+                       $calls[] = 2;
+               };
+
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $lbFactory->beginMasterChanges( __METHOD__ );
+
+               DeferredUpdates::addCallableUpdate( $callback1 );
+               $this->assertEquals( [], $calls );
+
+               DeferredUpdates::tryOpportunisticExecute( 'run' );
+               $this->assertEquals( [], $calls );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->onTransactionCommitOrIdle( function () use ( &$calls, $callback2 ) {
+                       DeferredUpdates::addCallableUpdate( $callback2 );
+                       $this->assertEquals( [], $calls );
+                       $calls[] = 'oti';
+               } );
+               $this->assertEquals( 1, $dbw->trxLevel() );
+               $this->assertEquals( [], $calls );
+
+               $lbFactory->commitMasterChanges( __METHOD__ );
+
+               $this->assertEquals( [ 'oti' ], $calls );
+
+               DeferredUpdates::tryOpportunisticExecute( 'run' );
+               $this->assertEquals( [ 'oti', 1, 2 ], $calls );
+       }
 }