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\Database
;
33 * Fake class around abstract class so we can call concrete methods.
35 class FakeDatabaseMysqlBase
extends DatabaseMysqlBase
{
37 function __construct() {
38 $this->profiler
= new ProfilerStub( [] );
39 $this->trxProfiler
= new TransactionProfiler();
40 $this->cliMode
= true;
41 $this->connLogger
= new \Psr\Log\
NullLogger();
42 $this->queryLogger
= new \Psr\Log\
NullLogger();
43 $this->errorLogger
= function ( Exception
$e ) {
44 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
46 $this->currentDomain
= DatabaseDomain
::newUnspecified();
49 protected function closeConnection() {
52 protected function doQuery( $sql ) {
56 protected function mysqlConnect( $realServer ) {
59 protected function mysqlSetCharset( $charset ) {
62 protected function mysqlFreeResult( $res ) {
65 protected function mysqlFetchObject( $res ) {
68 protected function mysqlFetchArray( $res ) {
71 protected function mysqlNumRows( $res ) {
74 protected function mysqlNumFields( $res ) {
77 protected function mysqlFieldName( $res, $n ) {
80 protected function mysqlFieldType( $res, $n ) {
83 protected function mysqlDataSeek( $res, $row ) {
86 protected function mysqlError( $conn = null ) {
89 protected function mysqlFetchField( $res, $n ) {
92 protected function mysqlRealEscapeString( $s ) {
98 function lastErrno() {
101 function affectedRows() {
104 function getServerVersion() {
108 class DatabaseMysqlBaseTest
extends PHPUnit_Framework_TestCase
{
110 * @dataProvider provideDiapers
111 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
113 public function testAddIdentifierQuotes( $expected, $in ) {
114 $db = new FakeDatabaseMysqlBase();
115 $quoted = $db->addIdentifierQuotes( $in );
116 $this->assertEquals( $expected, $quoted );
120 * Feeds testAddIdentifierQuotes
122 * Named per T22281 convention.
124 public static function provideDiapers() {
126 // Format: expected, input
129 // Yeah I really hate loosely typed PHP idiocies nowadays
132 // Dear codereviewer, guess what addIdentifierQuotes()
133 // will return with thoses:
137 // We never know what could happen
141 // Whatchout! Should probably use something more meaningful
142 [ "`'`", "'" ], # single quote
143 [ '`"`', '"' ], # double quote
144 [ '````', '`' ], # backtick
145 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
147 // sneaky NUL bytes are lurking everywhere
149 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
153 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
154 self
::createUnicodeString( '\u0001a\uFFFFb' )
157 self
::createUnicodeString( '`\u0001\uFFFF`' ),
158 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
161 [ '`メインページ`', 'メインページ' ],
162 [ '`Басты_бет`', 'Басты_бет' ],
165 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
166 [ '`Backtick: ```', 'Backtick: `' ],
167 [ '`This is a test`', 'This is a test' ],
171 private static function createUnicodeString( $str ) {
172 return json_decode( '"' . $str . '"' );
175 private function getMockForViews() {
176 $db = $this->getMockBuilder( 'DatabaseMysqli' )
177 ->disableOriginalConstructor()
178 ->setMethods( [ 'fetchRow', 'query' ] )
181 $db->method( 'query' )
182 ->with( $this->anything() )
183 ->willReturn( new FakeResultWrapper( [
184 (object)[ 'Tables_in_' => 'view1' ],
185 (object)[ 'Tables_in_' => 'view2' ],
186 (object)[ 'Tables_in_' => 'myview' ]
193 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
195 public function testListviews() {
196 $db = $this->getMockForViews();
198 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
202 $this->assertEquals( [ 'view1', 'view2' ],
203 $db->listViews( 'view' ) );
204 $this->assertEquals( [ 'myview' ],
205 $db->listViews( 'my' ) );
206 $this->assertEquals( [],
207 $db->listViews( 'UNUSED_PREFIX' ) );
208 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
209 $db->listViews( '' ) );
213 * @dataProvider provideComparePositions
214 * @covers Wikimedia\Rdbms\MySQLMasterPos
216 public function testHasReached( MySQLMasterPos
$lowerPos, MySQLMasterPos
$higherPos, $match ) {
218 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
220 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
221 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
222 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
223 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
224 } else { // channels don't match
225 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
227 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
228 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
232 public static function provideComparePositions() {
236 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
237 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
241 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
242 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
246 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
247 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
252 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
253 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
257 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
258 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
262 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
263 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
266 // MariaDB GTID style
268 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
269 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
273 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
274 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
278 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
279 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
286 * @dataProvider provideChannelPositions
287 * @covers Wikimedia\Rdbms\MySQLMasterPos
289 public function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
290 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
291 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
294 public static function provideChannelPositions() {
297 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
298 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
302 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
303 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
307 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
308 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
312 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
313 new MySQLMasterPos( 'trump2016.000976', '10000' ),
320 * @dataProvider provideLagAmounts
321 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
322 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
324 public function testPtHeartbeat( $lag ) {
325 $db = $this->getMockBuilder( 'DatabaseMysqli' )
326 ->disableOriginalConstructor()
328 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
331 $db->method( 'getLagDetectionMethod' )
332 ->willReturn( 'pt-heartbeat' );
334 $db->method( 'getMasterServerInfo' )
335 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
337 // Fake the current time.
338 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
339 $now = (float)$nowSec +
(float)$nowSecFrac;
340 // Fake the heartbeat time.
341 // Work arounds for weak DataTime microseconds support.
342 $ptTime = $now - $lag;
343 $ptSec = (int)$ptTime;
344 $ptSecFrac = ( $ptTime - $ptSec );
345 $ptDateTime = new DateTime( "@$ptSec" );
346 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
347 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
349 $db->method( 'getHeartbeatData' )
350 ->with( [ 'server_id' => 172 ] )
351 ->willReturn( [ $ptTimeISO, $now ] );
353 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
354 $lagEst = $db->getLag();
356 $this->assertGreaterThan( $lag - 0.010, $lagEst, "Correct heatbeat lag" );
357 $this->assertLessThan( $lag +
0.010, $lagEst, "Correct heatbeat lag" );
360 public static function provideLagAmounts() {
374 * @expectedException UnexpectedValueException
375 * @covers Wikimedia\Rdbms\Database::setFlag
377 public function testDBOIgnoreSet() {
378 $db = new FakeDatabaseMysqlBase();
380 $db->setFlag( Database
::DBO_IGNORE
);
384 * @expectedException UnexpectedValueException
385 * @covers Wikimedia\Rdbms\Database::clearFlag
387 public function testDBOIgnoreClear() {
388 $db = new FakeDatabaseMysqlBase();
390 $db->clearFlag( Database
::DBO_IGNORE
);