rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / libs / rdbms / database / DatabaseMysqlBaseTest.php
blobb56431042959a7941de3f07a638298f7b5875127
1 <?php
2 /**
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
20 * @file
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;
31 /**
32 * Fake class around abstract class so we can call concrete methods.
34 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
35 // From Database
36 function __construct() {
37 $this->profiler = new ProfilerStub( [] );
38 $this->trxProfiler = new TransactionProfiler();
39 $this->cliMode = true;
40 $this->connLogger = new \Psr\Log\NullLogger();
41 $this->queryLogger = new \Psr\Log\NullLogger();
42 $this->errorLogger = function ( Exception $e ) {
43 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
45 $this->currentDomain = DatabaseDomain::newUnspecified();
48 protected function closeConnection() {
51 protected function doQuery( $sql ) {
54 // From DatabaseMysql
55 protected function mysqlConnect( $realServer ) {
58 protected function mysqlSetCharset( $charset ) {
61 protected function mysqlFreeResult( $res ) {
64 protected function mysqlFetchObject( $res ) {
67 protected function mysqlFetchArray( $res ) {
70 protected function mysqlNumRows( $res ) {
73 protected function mysqlNumFields( $res ) {
76 protected function mysqlFieldName( $res, $n ) {
79 protected function mysqlFieldType( $res, $n ) {
82 protected function mysqlDataSeek( $res, $row ) {
85 protected function mysqlError( $conn = null ) {
88 protected function mysqlFetchField( $res, $n ) {
91 protected function mysqlRealEscapeString( $s ) {
94 function insertId() {
97 function lastErrno() {
100 function affectedRows() {
103 function getServerVersion() {
107 class DatabaseMysqlBaseTest extends PHPUnit_Framework_TestCase {
109 * @dataProvider provideDiapers
110 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::addIdentifierQuotes
112 public function testAddIdentifierQuotes( $expected, $in ) {
113 $db = new FakeDatabaseMysqlBase();
114 $quoted = $db->addIdentifierQuotes( $in );
115 $this->assertEquals( $expected, $quoted );
119 * Feeds testAddIdentifierQuotes
121 * Named per T22281 convention.
123 public static function provideDiapers() {
124 return [
125 // Format: expected, input
126 [ '``', '' ],
128 // Yeah I really hate loosely typed PHP idiocies nowadays
129 [ '``', null ],
131 // Dear codereviewer, guess what addIdentifierQuotes()
132 // will return with thoses:
133 [ '``', false ],
134 [ '`1`', true ],
136 // We never know what could happen
137 [ '`0`', 0 ],
138 [ '`1`', 1 ],
140 // Whatchout! Should probably use something more meaningful
141 [ "`'`", "'" ], # single quote
142 [ '`"`', '"' ], # double quote
143 [ '````', '`' ], # backtick
144 [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
146 // sneaky NUL bytes are lurking everywhere
147 [ '``', "\0" ],
148 [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
150 // unicode chars
152 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
153 self::createUnicodeString( '\u0001a\uFFFFb' )
156 self::createUnicodeString( '`\u0001\uFFFF`' ),
157 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
159 [ '`☃`', '☃' ],
160 [ '`メインページ`', 'メインページ' ],
161 [ '`Басты_бет`', 'Басты_бет' ],
163 // Real world:
164 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
165 [ '`Backtick: ```', 'Backtick: `' ],
166 [ '`This is a test`', 'This is a test' ],
170 private static function createUnicodeString( $str ) {
171 return json_decode( '"' . $str . '"' );
174 private function getMockForViews() {
175 $db = $this->getMockBuilder( 'DatabaseMysqli' )
176 ->disableOriginalConstructor()
177 ->setMethods( [ 'fetchRow', 'query' ] )
178 ->getMock();
180 $db->method( 'query' )
181 ->with( $this->anything() )
182 ->willReturn( new FakeResultWrapper( [
183 (object)[ 'Tables_in_' => 'view1' ],
184 (object)[ 'Tables_in_' => 'view2' ],
185 (object)[ 'Tables_in_' => 'myview' ]
186 ] ) );
188 return $db;
192 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::listViews
194 public function testListviews() {
195 $db = $this->getMockForViews();
197 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
198 $db->listViews() );
200 // Prefix filtering
201 $this->assertEquals( [ 'view1', 'view2' ],
202 $db->listViews( 'view' ) );
203 $this->assertEquals( [ 'myview' ],
204 $db->listViews( 'my' ) );
205 $this->assertEquals( [],
206 $db->listViews( 'UNUSED_PREFIX' ) );
207 $this->assertEquals( [ 'view1', 'view2', 'myview' ],
208 $db->listViews( '' ) );
212 * @dataProvider provideComparePositions
213 * @covers Wikimedia\Rdbms\MySQLMasterPos
215 public function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
216 if ( $match ) {
217 $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
219 $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
220 $this->assertTrue( $higherPos->hasReached( $higherPos ) );
221 $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
222 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
223 } else { // channels don't match
224 $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
226 $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
227 $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
231 public static function provideComparePositions() {
232 return [
233 // Binlog style
235 new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
236 new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
237 true
240 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
241 new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
242 true
245 new MySQLMasterPos( 'db1034-bin.000976', '999' ),
246 new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
247 false
249 // MySQL GTID style
251 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
252 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
253 true
256 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
257 new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
258 true
261 new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
262 new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
263 false
265 // MariaDB GTID style
267 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
268 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
269 true
272 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
273 new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
274 true
277 new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
278 new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
279 false
285 * @dataProvider provideChannelPositions
286 * @covers Wikimedia\Rdbms\MySQLMasterPos
288 public function testChannelsMatch( MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches ) {
289 $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
290 $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
293 public static function provideChannelPositions() {
294 return [
296 new MySQLMasterPos( 'db1034-bin.000876', '44' ),
297 new MySQLMasterPos( 'db1034-bin.000976', '74' ),
298 true
301 new MySQLMasterPos( 'db1052-bin.000976', '999' ),
302 new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
303 true
306 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307 new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
308 false
311 new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
312 new MySQLMasterPos( 'trump2016.000976', '10000' ),
313 false
319 * @dataProvider provideLagAmounts
320 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLag
321 * @covers Wikimedia\Rdbms\DatabaseMysqlBase::getLagFromPtHeartbeat
323 public function testPtHeartbeat( $lag ) {
324 $db = $this->getMockBuilder( 'DatabaseMysqli' )
325 ->disableOriginalConstructor()
326 ->setMethods( [
327 'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
328 ->getMock();
330 $db->method( 'getLagDetectionMethod' )
331 ->willReturn( 'pt-heartbeat' );
333 $db->method( 'getMasterServerInfo' )
334 ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
336 // Fake the current time.
337 list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
338 $now = (float)$nowSec + (float)$nowSecFrac;
339 // Fake the heartbeat time.
340 // Work arounds for weak DataTime microseconds support.
341 $ptTime = $now - $lag;
342 $ptSec = (int)$ptTime;
343 $ptSecFrac = ( $ptTime - $ptSec );
344 $ptDateTime = new DateTime( "@$ptSec" );
345 $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
346 $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
348 $db->method( 'getHeartbeatData' )
349 ->with( [ 'server_id' => 172 ] )
350 ->willReturn( [ $ptTimeISO, $now ] );
352 $db->setLBInfo( 'clusterMasterHost', 'db1052' );
353 $lagEst = $db->getLag();
355 $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
356 $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
359 public static function provideLagAmounts() {
360 return [
361 [ 0 ],
362 [ 0.3 ],
363 [ 6.5 ],
364 [ 10.1 ],
365 [ 200.2 ],
366 [ 400.7 ],
367 [ 600.22 ],
368 [ 1000.77 ],