From: jenkins-bot Date: Fri, 2 Feb 2018 03:51:32 +0000 (+0000) Subject: Merge "deferred: Make MWCallableUpdate support a list of DB handles" X-Git-Tag: 1.31.0-rc.0~732 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=dd2e27c4b1c83403d510fbf7eb240a3b62aa9563;hp=4af9e538a452c4d9c2d8a3ce84e429068272ae44 Merge "deferred: Make MWCallableUpdate support a list of DB handles" --- diff --git a/includes/deferred/MWCallableUpdate.php b/includes/deferred/MWCallableUpdate.php index 5b822af492..9803b7a491 100644 --- a/includes/deferred/MWCallableUpdate.php +++ b/includes/deferred/MWCallableUpdate.php @@ -14,14 +14,18 @@ class MWCallableUpdate implements DeferrableUpdate, DeferrableCallback { /** * @param callable $callback * @param string $fname Calling method - * @param IDatabase|null $dbw Abort if this DB is rolled back [optional] (since 1.28) + * @param IDatabase|IDatabase[]|null $dbws Abort if any of the specified DB handles have + * a currently pending transaction which later gets rolled back [optional] (since 1.28) */ - public function __construct( callable $callback, $fname = 'unknown', IDatabase $dbw = null ) { + public function __construct( callable $callback, $fname = 'unknown', $dbws = [] ) { $this->callback = $callback; $this->fname = $fname; - if ( $dbw && $dbw->trxLevel() ) { - $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname ); + $dbws = is_array( $dbws ) ? $dbws : [ $dbws ]; + foreach ( $dbws as $dbw ) { + if ( $dbw && $dbw->trxLevel() ) { + $dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname ); + } } } diff --git a/tests/phpunit/includes/deferred/MWCallableUpdateTest.php b/tests/phpunit/includes/deferred/MWCallableUpdateTest.php index 088ab4f544..6977aef1a2 100644 --- a/tests/phpunit/includes/deferred/MWCallableUpdateTest.php +++ b/tests/phpunit/includes/deferred/MWCallableUpdateTest.php @@ -29,8 +29,54 @@ class MWCallableUpdateTest extends PHPUnit_Framework_TestCase { // Emulate rollback $db->rollback( __METHOD__ ); + $update->doUpdate(); + + // Ensure it was cancelled + $this->assertSame( 0, $ran ); + } + + public function testCancelSome() { + // Prepare update and DB + $db1 = new DatabaseTestHelper( __METHOD__ ); + $db1->begin( __METHOD__ ); + $db2 = new DatabaseTestHelper( __METHOD__ ); + $db2->begin( __METHOD__ ); + $ran = 0; + $update = new MWCallableUpdate( function () use ( &$ran ) { + $ran++; + }, __METHOD__, [ $db1, $db2 ] ); + + // Emulate rollback + $db1->rollback( __METHOD__ ); + + $update->doUpdate(); + + // Prevents: "Notice: DB transaction writes or callbacks still pending" + $db2->rollback( __METHOD__ ); + // Ensure it was cancelled + $this->assertSame( 0, $ran ); + } + + public function testCancelAll() { + // Prepare update and DB + $db1 = new DatabaseTestHelper( __METHOD__ ); + $db1->begin( __METHOD__ ); + $db2 = new DatabaseTestHelper( __METHOD__ ); + $db2->begin( __METHOD__ ); + $ran = 0; + $update = new MWCallableUpdate( function () use ( &$ran ) { + $ran++; + }, __METHOD__, [ $db1, $db2 ] ); + + // Emulate rollbacks + $db1->rollback( __METHOD__ ); + $db2->rollback( __METHOD__ ); + $update->doUpdate(); + + // Ensure it was cancelled $this->assertSame( 0, $ran ); } + }