3 * Holds tests for DatabaseMysqlBase class.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation and contributors
26 use Wikimedia\Rdbms\TransactionProfiler
;
27 use Wikimedia\Rdbms\DatabaseDomain
;
28 use Wikimedia\Rdbms\MySQLMasterPos
;
29 use Wikimedia\Rdbms\DatabaseMysqlBase
;
30 use Wikimedia\Rdbms\DatabaseMysqli
;
31 use Wikimedia\Rdbms\Database
;
32 use Wikimedia\TestingAccessWrapper
;
35 * Fake class around abstract class so we can call concrete methods.
37 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
39 function __construct() {
40 $this->profiler
= new ProfilerStub( [] );
41 $this->trxProfiler
= new TransactionProfiler();
42 $this->cliMode
= true;
43 $this->connLogger
= new \Psr\Log\
NullLogger();
44 $this->queryLogger
= new \Psr\Log\
NullLogger();
45 $this->errorLogger
= function ( Exception
$e ) {
46 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
48 $this->currentDomain
= DatabaseDomain
::newUnspecified();
51 protected function closeConnection() {
54 protected function doQuery( $sql ) {
57 protected function fetchAffectedRowCount() {
60 // From DatabaseMysqli
61 protected function mysqlConnect( $realServer ) {
64 protected function mysqlSetCharset( $charset ) {
67 protected function mysqlFreeResult( $res ) {
70 protected function mysqlFetchObject( $res ) {
73 protected function mysqlFetchArray( $res ) {
76 protected function mysqlNumRows( $res ) {
79 protected function mysqlNumFields( $res ) {
82 protected function mysqlFieldName( $res, $n ) {
85 protected function mysqlFieldType( $res, $n ) {
88 protected function mysqlDataSeek( $res, $row ) {
91 protected function mysqlError( $conn = null ) {
94 protected function mysqlFetchField( $res, $n ) {
97 protected function mysqlRealEscapeString( $s ) {
100 function insertId() {
103 function lastErrno() {
106 function affectedRows() {
109 function getServerVersion() {
113 class DatabaseMysqlBaseTest
extends PHPUnit\Framework\TestCase
{
115 use MediaWikiCoversValidator
;
118 * @dataProvider provideDiapers
119 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
121 public function testAddIdentifierQuotes( $expected, $in ) {
122 $db = new FakeDatabaseMysqlBase();
123 $quoted = $db->addIdentifierQuotes( $in );
124 $this->assertEquals( $expected, $quoted );
128 * Feeds testAddIdentifierQuotes
130 * Named per T22281 convention.
132 public static function provideDiapers() {
134 // Format: expected, input
137 // Yeah I really hate loosely typed PHP idiocies nowadays
140 // Dear codereviewer, guess what addIdentifierQuotes()
141 // will return with thoses:
145 // We never know what could happen
149 // Whatchout! Should probably use something more meaningful
150 [ "`'`", "'" ], # single quote
151 [ '`"`', '"' ], # double quote
152 [ '````', '`' ], # backtick
153 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
155 // sneaky NUL bytes are lurking everywhere
157 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
161 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
162 self
::createUnicodeString( '\u0001a\uFFFFb' )
165 self
::createUnicodeString( '`\u0001\uFFFF`' ),
166 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
169 [ '`メインページ`', 'メインページ' ],
170 [ '`Басты_бет`', 'Басты_бет' ],
173 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
174 [ '`Backtick: ```', 'Backtick: `' ],
175 [ '`This is a test`', 'This is a test' ],
179 private static function createUnicodeString( $str ) {
180 return json_decode( '"' . $str . '"' );
183 private function getMockForViews() {
184 $db = $this->getMockBuilder( DatabaseMysqli
::class )
185 ->disableOriginalConstructor()
186 ->setMethods( [ 'fetchRow', 'query' ] )
189 $db->method( 'query' )
190 ->with( $this->anything() )
191 ->willReturn( new FakeResultWrapper( [
192 (object)[ 'Tables_in_' => 'view1' ],
193 (object)[ 'Tables_in_' => 'view2' ],
194 (object)[ 'Tables_in_' => 'myview' ]
201 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
203 public function testListviews() {
204 $db = $this->getMockForViews();
206 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
210 $this->assertEquals( [ 'view1', 'view2' ],
211 $db->listViews( 'view' ) );
212 $this->assertEquals( [ 'myview' ],
213 $db->listViews( 'my' ) );
214 $this->assertEquals( [],
215 $db->listViews( 'UNUSED_PREFIX' ) );
216 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
217 $db->listViews( '' ) );
220 public function testBinLogName() {
221 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
223 $this->assertEquals( "db1052", $pos->binlog
);
224 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
225 $this->assertEquals( [ 2424, 4643 ], $pos->pos
);
229 * @dataProvider provideComparePositions
230 * @covers Wikimedia\Rdbms\MySQLMasterPos
232 public function testHasReached(
233 MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match, $hetero
236 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
239 // Each position is has one channel higher than the other
240 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
242 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
244 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
245 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
246 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
247 } else { // channels don't match
248 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
250 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
251 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
255 public static function provideComparePositions() {
256 $now = microtime( true );
261 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
262 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
267 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
268 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
273 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
274 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
280 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
281 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
286 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
287 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
292 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
293 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
297 // MariaDB GTID style
299 new MySQLMasterPos( '255-11-23', $now ),
300 new MySQLMasterPos( '255-11-24', $now ),
305 new MySQLMasterPos( '255-11-99', $now ),
306 new MySQLMasterPos( '255-11-100', $now ),
311 new MySQLMasterPos( '255-11-999', $now ),
312 new MySQLMasterPos( '254-11-1000', $now ),
317 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
318 new MySQLMasterPos( '255-11-24', $now ),
323 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
324 new MySQLMasterPos( '255-11-1000', $now ),
329 new MySQLMasterPos( '255-11-23,256-12-50', $now ),
330 new MySQLMasterPos( '255-11-24,155-52-63', $now ),
335 new MySQLMasterPos( '255-11-99,256-12-50,257-12-50', $now ),
336 new MySQLMasterPos( '255-11-1000,256-12-51', $now ),
341 new MySQLMasterPos( '255-11-99,256-12-50', $now ),
342 new MySQLMasterPos( '255-13-1000,256-14-49', $now ),
347 new MySQLMasterPos( '253-11-999,255-11-999', $now ),
348 new MySQLMasterPos( '254-11-1000', $now ),
356 * @dataProvider provideChannelPositions
357 * @covers Wikimedia\Rdbms\MySQLMasterPos
359 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
360 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
361 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
363 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
364 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
367 public static function provideChannelPositions() {
368 $now = microtime( true );
372 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
373 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
377 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
378 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
382 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
383 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
387 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
388 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
395 * @dataProvider provideCommonDomainGTIDs
396 * @covers Wikimedia\Rdbms\MySQLMasterPos
398 public function testCommonGtidDomains( MySQLMasterPos
$pos, MySQLMasterPos
$ref, $gtids ) {
399 $this->assertEquals( $gtids, MySQLMasterPos
::getCommonDomainGTIDs( $pos, $ref ) );
402 public static function provideCommonDomainGTIDs() {
405 new MySQLMasterPos( '255-13-99,256-12-50,257-14-50', 1 ),
406 new MySQLMasterPos( '255-11-1000', 1 ),
411 '2E11FA47-71CA-11E1-9E33-C80AA9429562:5,' .
412 '3E11FA47-71CA-11E1-9E33-C80AA9429562:99,' .
413 '7E11FA47-71CA-11E1-9E33-C80AA9429562:30',
417 '1E11FA47-71CA-11E1-9E33-C80AA9429562:100,' .
418 '3E11FA47-71CA-11E1-9E33-C80AA9429562:66',
421 [ '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ]
427 * @dataProvider provideLagAmounts
428 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
429 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
431 public function testPtHeartbeat( $lag ) {
432 $db = $this->getMockBuilder( DatabaseMysqli
::class )
433 ->disableOriginalConstructor()
435 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
438 $db->method( 'getLagDetectionMethod' )
439 ->willReturn( 'pt-heartbeat' );
441 $db->method( 'getMasterServerInfo' )
442 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
444 // Fake the current time.
445 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
446 $now = (float)$nowSec +
(float)$nowSecFrac;
447 // Fake the heartbeat time.
448 // Work arounds for weak DataTime microseconds support.
449 $ptTime = $now - $lag;
450 $ptSec = (int)$ptTime;
451 $ptSecFrac = ( $ptTime - $ptSec );
452 $ptDateTime = new DateTime( "@$ptSec" );
453 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
454 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
456 $db->method( 'getHeartbeatData' )
457 ->with( [ 'server_id' => 172 ] )
458 ->willReturn( [ $ptTimeISO, $now ] );
460 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
461 $lagEst = $db->getLag();
463 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
464 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
467 public static function provideLagAmounts() {
481 * @expectedException UnexpectedValueException
482 * @covers Wikimedia\Rdbms\Database::setFlag
484 public function testDBOIgnoreSet() {
485 $db = new FakeDatabaseMysqlBase();
487 $db->setFlag( Database
::DBO_IGNORE
);
491 * @expectedException UnexpectedValueException
492 * @covers Wikimedia\Rdbms\Database::clearFlag
494 public function testDBOIgnoreClear() {
495 $db = new FakeDatabaseMysqlBase();
497 $db->clearFlag( Database
::DBO_IGNORE
);
501 * @covers Wikimedia\Rdbms\MySQLMasterPos
503 public function testSerialize() {
504 $pos = new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', 53636363 );
505 $roundtripPos = unserialize( serialize( $pos ) );
507 $this->assertEquals( $pos, $roundtripPos );
509 $pos = new MySQLMasterPos( '255-11-23', 53636363 );
510 $roundtripPos = unserialize( serialize( $pos ) );
512 $this->assertEquals( $pos, $roundtripPos );
516 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::isInsertSelectSafe
517 * @dataProvider provideInsertSelectCases
519 public function testInsertSelectIsSafe( $insertOpts, $selectOpts, $row, $safe ) {
520 $db = $this->getMockBuilder( DatabaseMysqli
::class )
521 ->disableOriginalConstructor()
522 ->setMethods( [ 'getReplicationSafetyInfo' ] )
524 $db->method( 'getReplicationSafetyInfo' )->willReturn( (object)$row );
525 $dbw = TestingAccessWrapper
::newFromObject( $db );
527 $this->assertEquals( $safe, $dbw->isInsertSelectSafe( $insertOpts, $selectOpts ) );
530 public function provideInsertSelectCases() {
536 'innodb_autoinc_lock_mode' => '2',
537 'binlog_format' => 'ROW',
545 'innodb_autoinc_lock_mode' => '2',
546 'binlog_format' => 'ROW',
554 'innodb_autoinc_lock_mode' => '0',
555 'binlog_format' => 'STATEMENT',
563 'innodb_autoinc_lock_mode' => '2',
564 'binlog_format' => 'STATEMENT',
569 [ 'NO_AUTO_COLUMNS' ],
572 'innodb_autoinc_lock_mode' => '0',
573 'binlog_format' => 'STATEMENT',
581 'innodb_autoinc_lock_mode' => 0,
582 'binlog_format' => 'STATEMENT',
587 [ 'NO_AUTO_COLUMNS' ],
590 'innodb_autoinc_lock_mode' => 2,
591 'binlog_format' => 'STATEMENT',
596 [ 'NO_AUTO_COLUMNS' ],
599 'innodb_autoinc_lock_mode' => 0,
600 'binlog_format' => 'STATEMENT',