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
;
34 * Fake class around abstract class so we can call concrete methods.
36 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
38 function __construct() {
39 $this->profiler
= new ProfilerStub( [] );
40 $this->trxProfiler
= new TransactionProfiler();
41 $this->cliMode
= true;
42 $this->connLogger
= new \Psr\Log\
NullLogger();
43 $this->queryLogger
= new \Psr\Log\
NullLogger();
44 $this->errorLogger
= function ( Exception
$e ) {
45 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
47 $this->currentDomain
= DatabaseDomain
::newUnspecified();
50 protected function closeConnection() {
53 protected function doQuery( $sql ) {
56 protected function fetchAffectedRowCount() {
59 // From DatabaseMysqli
60 protected function mysqlConnect( $realServer ) {
63 protected function mysqlSetCharset( $charset ) {
66 protected function mysqlFreeResult( $res ) {
69 protected function mysqlFetchObject( $res ) {
72 protected function mysqlFetchArray( $res ) {
75 protected function mysqlNumRows( $res ) {
78 protected function mysqlNumFields( $res ) {
81 protected function mysqlFieldName( $res, $n ) {
84 protected function mysqlFieldType( $res, $n ) {
87 protected function mysqlDataSeek( $res, $row ) {
90 protected function mysqlError( $conn = null ) {
93 protected function mysqlFetchField( $res, $n ) {
96 protected function mysqlRealEscapeString( $s ) {
102 function lastErrno() {
105 function affectedRows() {
108 function getServerVersion() {
112 class DatabaseMysqlBaseTest
extends PHPUnit_Framework_TestCase
{
114 use MediaWikiCoversValidator
;
117 * @dataProvider provideDiapers
118 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
120 public function testAddIdentifierQuotes( $expected, $in ) {
121 $db = new FakeDatabaseMysqlBase();
122 $quoted = $db->addIdentifierQuotes( $in );
123 $this->assertEquals( $expected, $quoted );
127 * Feeds testAddIdentifierQuotes
129 * Named per T22281 convention.
131 public static function provideDiapers() {
133 // Format: expected, input
136 // Yeah I really hate loosely typed PHP idiocies nowadays
139 // Dear codereviewer, guess what addIdentifierQuotes()
140 // will return with thoses:
144 // We never know what could happen
148 // Whatchout! Should probably use something more meaningful
149 [ "`'`", "'" ], # single quote
150 [ '`"`', '"' ], # double quote
151 [ '````', '`' ], # backtick
152 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
154 // sneaky NUL bytes are lurking everywhere
156 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
160 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
161 self
::createUnicodeString( '\u0001a\uFFFFb' )
164 self
::createUnicodeString( '`\u0001\uFFFF`' ),
165 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
168 [ '`メインページ`', 'メインページ' ],
169 [ '`Басты_бет`', 'Басты_бет' ],
172 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
173 [ '`Backtick: ```', 'Backtick: `' ],
174 [ '`This is a test`', 'This is a test' ],
178 private static function createUnicodeString( $str ) {
179 return json_decode( '"' . $str . '"' );
182 private function getMockForViews() {
183 $db = $this->getMockBuilder( DatabaseMysqli
::class )
184 ->disableOriginalConstructor()
185 ->setMethods( [ 'fetchRow', 'query' ] )
188 $db->method( 'query' )
189 ->with( $this->anything() )
190 ->willReturn( new FakeResultWrapper( [
191 (object)[ 'Tables_in_' => 'view1' ],
192 (object)[ 'Tables_in_' => 'view2' ],
193 (object)[ 'Tables_in_' => 'myview' ]
200 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
202 public function testListviews() {
203 $db = $this->getMockForViews();
205 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
209 $this->assertEquals( [ 'view1', 'view2' ],
210 $db->listViews( 'view' ) );
211 $this->assertEquals( [ 'myview' ],
212 $db->listViews( 'my' ) );
213 $this->assertEquals( [],
214 $db->listViews( 'UNUSED_PREFIX' ) );
215 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
216 $db->listViews( '' ) );
219 public function testBinLogName() {
220 $pos = new MySQLMasterPos( "db1052.2424/4643", 1 );
222 $this->assertEquals( "db1052", $pos->binlog
);
223 $this->assertEquals( "db1052.2424", $pos->getLogFile() );
224 $this->assertEquals( [ 2424, 4643 ], $pos->pos
);
228 * @dataProvider provideComparePositions
229 * @covers Wikimedia\Rdbms\MySQLMasterPos
231 public function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
233 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
235 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
236 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
237 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
238 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
239 } else { // channels don't match
240 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
242 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
243 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
247 public static function provideComparePositions() {
248 $now = microtime( true );
253 new MySQLMasterPos( 'db1034-bin.000976/843431247', $now ),
254 new MySQLMasterPos( 'db1034-bin.000976/843431248', $now ),
258 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
259 new MySQLMasterPos( 'db1034-bin.000976/1000', $now ),
263 new MySQLMasterPos( 'db1034-bin.000976/999', $now ),
264 new MySQLMasterPos( 'db1035-bin.000976/1000', $now ),
269 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:23', $now ),
270 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:24', $now ),
274 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
275 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
279 new MySQLMasterPos( '3E11FA47-71CA-11E1-9E33-C80AA9429562:99', $now ),
280 new MySQLMasterPos( '1E11FA47-71CA-11E1-9E33-C80AA9429562:100', $now ),
283 // MariaDB GTID style
285 new MySQLMasterPos( '255-11-23', $now ),
286 new MySQLMasterPos( '255-11-24', $now ),
290 new MySQLMasterPos( '255-11-99', $now ),
291 new MySQLMasterPos( '255-11-100', $now ),
295 new MySQLMasterPos( '255-11-999', $now ),
296 new MySQLMasterPos( '254-11-1000', $now ),
303 * @dataProvider provideChannelPositions
304 * @covers Wikimedia\Rdbms\MySQLMasterPos
306 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
307 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
308 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
310 $roundtripPos = new MySQLMasterPos( (string)$pos1, 1 );
311 $this->assertEquals( (string)$pos1, (string)$roundtripPos );
314 public static function provideChannelPositions() {
315 $now = microtime( true );
319 new MySQLMasterPos( 'db1034-bin.000876/44', $now ),
320 new MySQLMasterPos( 'db1034-bin.000976/74', $now ),
324 new MySQLMasterPos( 'db1052-bin.000976/999', $now ),
325 new MySQLMasterPos( 'db1052-bin.000976/1000', $now ),
329 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
330 new MySQLMasterPos( 'db1035-bin.000976/10000', $now ),
334 new MySQLMasterPos( 'db1066-bin.000976/9999', $now ),
335 new MySQLMasterPos( 'trump2016.000976/10000', $now ),
342 * @dataProvider provideLagAmounts
343 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
344 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
346 public function testPtHeartbeat( $lag ) {
347 $db = $this->getMockBuilder( DatabaseMysqli
::class )
348 ->disableOriginalConstructor()
350 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
353 $db->method( 'getLagDetectionMethod' )
354 ->willReturn( 'pt-heartbeat' );
356 $db->method( 'getMasterServerInfo' )
357 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
359 // Fake the current time.
360 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
361 $now = (float)$nowSec +
(float)$nowSecFrac;
362 // Fake the heartbeat time.
363 // Work arounds for weak DataTime microseconds support.
364 $ptTime = $now - $lag;
365 $ptSec = (int)$ptTime;
366 $ptSecFrac = ( $ptTime - $ptSec );
367 $ptDateTime = new DateTime( "@$ptSec" );
368 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
369 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
371 $db->method( 'getHeartbeatData' )
372 ->with( [ 'server_id' => 172 ] )
373 ->willReturn( [ $ptTimeISO, $now ] );
375 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
376 $lagEst = $db->getLag();
378 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
379 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
382 public static function provideLagAmounts() {
396 * @expectedException UnexpectedValueException
397 * @covers Wikimedia\Rdbms\Database::setFlag
399 public function testDBOIgnoreSet() {
400 $db = new FakeDatabaseMysqlBase();
402 $db->setFlag( Database
::DBO_IGNORE
);
406 * @expectedException UnexpectedValueException
407 * @covers Wikimedia\Rdbms\Database::clearFlag
409 public function testDBOIgnoreClear() {
410 $db = new FakeDatabaseMysqlBase();
412 $db->clearFlag( Database
::DBO_IGNORE
);