3 * Holds tests for DatabaseMysqlBase MediaWiki 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
23 * @copyright © 2013 Antoine Musso
24 * @copyright © 2013 Bryan Davis
25 * @copyright © 2013 Wikimedia Foundation Inc.
28 use Wikimedia\Rdbms\TransactionProfiler
;
29 use Wikimedia\Rdbms\DatabaseDomain
;
30 use Wikimedia\Rdbms\MySQLMasterPos
;
31 use Wikimedia\Rdbms\DatabaseMysqlBase
;
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 ) {
57 protected function mysqlConnect( $realServer ) {
60 protected function mysqlSetCharset( $charset ) {
63 protected function mysqlFreeResult( $res ) {
66 protected function mysqlFetchObject( $res ) {
69 protected function mysqlFetchArray( $res ) {
72 protected function mysqlNumRows( $res ) {
75 protected function mysqlNumFields( $res ) {
78 protected function mysqlFieldName( $res, $n ) {
81 protected function mysqlFieldType( $res, $n ) {
84 protected function mysqlDataSeek( $res, $row ) {
87 protected function mysqlError( $conn = null ) {
90 protected function mysqlFetchField( $res, $n ) {
93 protected function mysqlRealEscapeString( $s ) {
100 function lastErrno() {
103 function affectedRows() {
106 function getServerVersion() {
110 class DatabaseMysqlBaseTest
extends MediaWikiTestCase
{
112 * @dataProvider provideDiapers
113 * @covers DatabaseMysqlBase::addIdentifierQuotes
115 public function testAddIdentifierQuotes( $expected, $in ) {
116 $db = new FakeDatabaseMysqlBase();
117 $quoted = $db->addIdentifierQuotes( $in );
118 $this->assertEquals( $expected, $quoted );
122 * Feeds testAddIdentifierQuotes
124 * Named per T22281 convention.
126 function provideDiapers() {
128 // Format: expected, input
131 // Yeah I really hate loosely typed PHP idiocies nowadays
134 // Dear codereviewer, guess what addIdentifierQuotes()
135 // will return with thoses:
139 // We never know what could happen
143 // Whatchout! Should probably use something more meaningful
144 [ "`'`", "'" ], # single quote
145 [ '`"`', '"' ], # double quote
146 [ '````', '`' ], # backtick
147 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
149 // sneaky NUL bytes are lurking everywhere
151 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
155 self
::createUnicodeString( '`\u0001a\uFFFFb`' ),
156 self
::createUnicodeString( '\u0001a\uFFFFb' )
159 self
::createUnicodeString( '`\u0001\uFFFF`' ),
160 self
::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
163 [ '`メインページ`', 'メインページ' ],
164 [ '`Басты_бет`', 'Басты_бет' ],
167 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
168 [ '`Backtick: ```', 'Backtick: `' ],
169 [ '`This is a test`', 'This is a test' ],
173 private static function createUnicodeString( $str ) {
174 return json_decode( '"' . $str . '"' );
177 function getMockForViews() {
178 $db = $this->getMockBuilder( 'DatabaseMysqli' )
179 ->disableOriginalConstructor()
180 ->setMethods( [ 'fetchRow', 'query' ] )
183 $db->method( 'query' )
184 ->with( $this->anything() )
185 ->willReturn( new FakeResultWrapper( [
186 (object)[ 'Tables_in_' => 'view1' ],
187 (object)[ 'Tables_in_' => 'view2' ],
188 (object)[ 'Tables_in_' => 'myview' ]
194 * @covers DatabaseMysqlBase::listViews
196 function testListviews() {
197 $db = $this->getMockForViews();
199 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
203 $this->assertEquals( [ 'view1', 'view2' ],
204 $db->listViews( 'view' ) );
205 $this->assertEquals( [ 'myview' ],
206 $db->listViews( 'my' ) );
207 $this->assertEquals( [],
208 $db->listViews( 'UNUSED_PREFIX' ) );
209 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
210 $db->listViews( '' ) );
214 * @dataProvider provideComparePositions
216 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 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
288 function testChannelsMatch( MySQLMasterPos
$pos1, MySQLMasterPos
$pos2, $matches ) {
289 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
290 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
293 function provideChannelPositions() {
296 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
297 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
301 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
302 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
306 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
311 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
312 new MySQLMasterPos( 'trump2016.000976', '10000' ),
319 * @dataProvider provideLagAmounts
321 function testPtHeartbeat( $lag ) {
322 $db = $this->getMockBuilder( 'DatabaseMysqli' )
323 ->disableOriginalConstructor()
325 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
328 $db->method( 'getLagDetectionMethod' )
329 ->willReturn( 'pt-heartbeat' );
331 $db->method( 'getMasterServerInfo' )
332 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
334 // Fake the current time.
335 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
336 $now = (float)$nowSec +
(float)$nowSecFrac;
337 // Fake the heartbeat time.
338 // Work arounds for weak DataTime microseconds support.
339 $ptTime = $now - $lag;
340 $ptSec = (int)$ptTime;
341 $ptSecFrac = ( $ptTime - $ptSec );
342 $ptDateTime = new DateTime( "@$ptSec" );
343 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
344 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
346 $db->method( 'getHeartbeatData' )
347 ->with( [ 'server_id' => 172 ] )
348 ->willReturn( [ $ptTimeISO, $now ] );
350 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
351 $lagEst = $db->getLag();
353 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
354 $this->assertLessThan( $lag +
.010, $lagEst, "Correct heatbeat lag" );
357 function provideLagAmounts() {