rdbms: Use more narrow type hints in LBFactoryTest
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
1 <?php
2 /**
3 * Holds tests for LBFactory abstract MediaWiki class.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation Inc.
24 */
25
26 use Wikimedia\Rdbms\Database;
27 use Wikimedia\Rdbms\IDatabase;
28 use Wikimedia\Rdbms\IMaintainableDatabase;
29 use Wikimedia\Rdbms\LBFactory;
30 use Wikimedia\Rdbms\LBFactorySimple;
31 use Wikimedia\Rdbms\LBFactoryMulti;
32 use Wikimedia\Rdbms\LoadBalancer;
33 use Wikimedia\Rdbms\ChronologyProtector;
34 use Wikimedia\Rdbms\DatabaseMysqli;
35 use Wikimedia\Rdbms\MySQLMasterPos;
36 use Wikimedia\Rdbms\DatabaseDomain;
37
38 /**
39 * @group Database
40 * @covers \Wikimedia\Rdbms\LBFactory
41 * @covers \Wikimedia\Rdbms\LBFactorySimple
42 * @covers \Wikimedia\Rdbms\LBFactoryMulti
43 */
44 class LBFactoryTest extends MediaWikiTestCase {
45
46 /**
47 * @covers MWLBFactory::getLBFactoryClass()
48 * @dataProvider getLBFactoryClassProvider
49 */
50 public function testGetLBFactoryClass( $expected, $deprecated ) {
51 $mockDB = $this->getMockBuilder( DatabaseMysqli::class )
52 ->disableOriginalConstructor()
53 ->getMock();
54
55 $config = [
56 'class' => $deprecated,
57 'connection' => $mockDB,
58 # Various other parameters required:
59 'sectionsByDB' => [],
60 'sectionLoads' => [],
61 'serverTemplate' => [],
62 ];
63
64 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
65 $result = MWLBFactory::getLBFactoryClass( $config );
66
67 $this->assertEquals( $expected, $result );
68 }
69
70 public function getLBFactoryClassProvider() {
71 return [
72 # Format: new class, old class
73 [ Wikimedia\Rdbms\LBFactorySimple::class, 'LBFactory_Simple' ],
74 [ Wikimedia\Rdbms\LBFactorySingle::class, 'LBFactory_Single' ],
75 [ Wikimedia\Rdbms\LBFactoryMulti::class, 'LBFactory_Multi' ],
76 [ Wikimedia\Rdbms\LBFactorySimple::class, 'LBFactorySimple' ],
77 [ Wikimedia\Rdbms\LBFactorySingle::class, 'LBFactorySingle' ],
78 [ Wikimedia\Rdbms\LBFactoryMulti::class, 'LBFactoryMulti' ],
79 ];
80 }
81
82 /**
83 * @covers \Wikimedia\Rdbms\LBFactory::getLocalDomainID()
84 * @covers \Wikimedia\Rdbms\LBFactory::resolveDomainID()
85 */
86 public function testLBFactorySimpleServer() {
87 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
88
89 $servers = [
90 [
91 'host' => $wgDBserver,
92 'dbname' => $wgDBname,
93 'user' => $wgDBuser,
94 'password' => $wgDBpassword,
95 'type' => $wgDBtype,
96 'dbDirectory' => $wgSQLiteDataDir,
97 'load' => 0,
98 'flags' => DBO_TRX // REPEATABLE-READ for consistency
99 ],
100 ];
101
102 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
103 $lb = $factory->getMainLB();
104
105 $dbw = $lb->getConnection( DB_MASTER );
106 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
107
108 $dbr = $lb->getConnection( DB_REPLICA );
109 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
110
111 $this->assertSame( 'my_test_wiki', $factory->resolveDomainID( 'my_test_wiki' ) );
112 $this->assertSame( $factory->getLocalDomainID(), $factory->resolveDomainID( false ) );
113
114 $factory->shutdown();
115 $lb->closeAll();
116 }
117
118 public function testLBFactorySimpleServers() {
119 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
120
121 $servers = [
122 [ // master
123 'host' => $wgDBserver,
124 'dbname' => $wgDBname,
125 'user' => $wgDBuser,
126 'password' => $wgDBpassword,
127 'type' => $wgDBtype,
128 'dbDirectory' => $wgSQLiteDataDir,
129 'load' => 0,
130 'flags' => DBO_TRX // REPEATABLE-READ for consistency
131 ],
132 [ // emulated replica
133 'host' => $wgDBserver,
134 'dbname' => $wgDBname,
135 'user' => $wgDBuser,
136 'password' => $wgDBpassword,
137 'type' => $wgDBtype,
138 'dbDirectory' => $wgSQLiteDataDir,
139 'load' => 100,
140 'flags' => DBO_TRX // REPEATABLE-READ for consistency
141 ]
142 ];
143
144 $factory = new LBFactorySimple( [
145 'servers' => $servers,
146 'loadMonitorClass' => LoadMonitorNull::class
147 ] );
148 $lb = $factory->getMainLB();
149
150 $dbw = $lb->getConnection( DB_MASTER );
151 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
152 $this->assertEquals(
153 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
154 $dbw->getLBInfo( 'clusterMasterHost' ),
155 'cluster master set' );
156
157 $dbr = $lb->getConnection( DB_REPLICA );
158 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
159 $this->assertEquals(
160 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
161 $dbr->getLBInfo( 'clusterMasterHost' ),
162 'cluster master set' );
163
164 $factory->shutdown();
165 $lb->closeAll();
166 }
167
168 public function testLBFactoryMultiConns() {
169 $factory = $this->newLBFactoryMultiLBs();
170
171 $dbw = $factory->getMainLB()->getConnection( DB_MASTER );
172 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
173
174 $dbr = $factory->getMainLB()->getConnection( DB_REPLICA );
175 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
176
177 // Destructor should trigger without round stage errors
178 unset( $factory );
179 }
180
181 public function testLBFactoryMultiRoundCallbacks() {
182 $called = 0;
183 $countLBsFunc = function ( LBFactoryMulti $factory ) {
184 $count = 0;
185 $factory->forEachLB( function () use ( &$count ) {
186 ++$count;
187 } );
188
189 return $count;
190 };
191
192 $factory = $this->newLBFactoryMultiLBs();
193 $this->assertEquals( 0, $countLBsFunc( $factory ) );
194 $dbw = $factory->getMainLB()->getConnection( DB_MASTER );
195 $this->assertEquals( 1, $countLBsFunc( $factory ) );
196 // Test that LoadBalancer instances made during pre-commit callbacks in do not
197 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.
198 $factory->beginMasterChanges( __METHOD__ );
199 $dbw->onTransactionPreCommitOrIdle( function () use ( $factory, &$called ) {
200 ++$called;
201 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
202 // There is no s1wiki DB to select so it is not in getConnection(),
203 // but this fools getMainLB() at least.
204 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER );
205 } );
206 $factory->commitMasterChanges( __METHOD__ );
207 $this->assertEquals( 1, $called );
208 $this->assertEquals( 2, $countLBsFunc( $factory ) );
209 $factory->shutdown();
210 $factory->closeAll();
211
212 $called = 0;
213 $factory = $this->newLBFactoryMultiLBs();
214 $this->assertEquals( 0, $countLBsFunc( $factory ) );
215 $dbw = $factory->getMainLB()->getConnection( DB_MASTER );
216 $this->assertEquals( 1, $countLBsFunc( $factory ) );
217 // Test that LoadBalancer instances made during pre-commit callbacks in do not
218 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.hrow
219 // DBTransactionError due to transaction ROUND_* stages being mismatched.
220 $factory->beginMasterChanges( __METHOD__ );
221 $dbw->query( "SELECT 1 as t", __METHOD__ );
222 $dbw->onTransactionResolution( function () use ( $factory, &$called ) {
223 ++$called;
224 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
225 // There is no s1wiki DB to select so it is not in getConnection(),
226 // but this fools getMainLB() at least.
227 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER );
228 } );
229 $factory->commitMasterChanges( __METHOD__ );
230 $this->assertEquals( 1, $called );
231 $this->assertEquals( 2, $countLBsFunc( $factory ) );
232 $factory->shutdown();
233 $factory->closeAll();
234
235 $factory = $this->newLBFactoryMultiLBs();
236 $dbw = $factory->getMainLB()->getConnection( DB_MASTER );
237 // DBTransactionError should not be thrown
238 $ran = 0;
239 $dbw->onTransactionPreCommitOrIdle( function () use ( &$ran ) {
240 ++$ran;
241 } );
242 $factory->commitAll( __METHOD__ );
243 $this->assertEquals( 1, $ran );
244
245 $factory->shutdown();
246 $factory->closeAll();
247 }
248
249 private function newLBFactoryMultiLBs() {
250 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
251
252 return new LBFactoryMulti( [
253 'sectionsByDB' => [
254 's1wiki' => 's1',
255 ],
256 'sectionLoads' => [
257 's1' => [
258 'test-db3' => 0,
259 'test-db4' => 100,
260 ],
261 'DEFAULT' => [
262 'test-db1' => 0,
263 'test-db2' => 100,
264 ]
265 ],
266 'serverTemplate' => [
267 'dbname' => $wgDBname,
268 'user' => $wgDBuser,
269 'password' => $wgDBpassword,
270 'type' => $wgDBtype,
271 'dbDirectory' => $wgSQLiteDataDir,
272 'flags' => DBO_DEFAULT
273 ],
274 'hostsByName' => [
275 'test-db1' => $wgDBserver,
276 'test-db2' => $wgDBserver,
277 'test-db3' => $wgDBserver,
278 'test-db4' => $wgDBserver
279 ],
280 'loadMonitorClass' => LoadMonitorNull::class
281 ] );
282 }
283
284 /**
285 * @covers \Wikimedia\Rdbms\ChronologyProtector
286 */
287 public function testChronologyProtector() {
288 $now = microtime( true );
289
290 // (a) First HTTP request
291 $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
292 $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
293
294 // Master DB 1
295 $mockDB1 = $this->getMockBuilder( DatabaseMysqli::class )
296 ->disableOriginalConstructor()
297 ->getMock();
298 $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
299 $mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
300 $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
301 // Load balancer for master DB 1
302 $lb1 = $this->getMockBuilder( LoadBalancer::class )
303 ->disableOriginalConstructor()
304 ->getMock();
305 $lb1->method( 'getConnection' )->willReturn( $mockDB1 );
306 $lb1->method( 'getServerCount' )->willReturn( 2 );
307 $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
308 $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
309 function () use ( $mockDB1 ) {
310 $p = 0;
311 $p |= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
312 $p |= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
313
314 return (bool)$p;
315 }
316 ) );
317 $lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
318 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
319 // Master DB 2
320 $mockDB2 = $this->getMockBuilder( DatabaseMysqli::class )
321 ->disableOriginalConstructor()
322 ->getMock();
323 $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
324 $mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
325 $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
326 // Load balancer for master DB 2
327 $lb2 = $this->getMockBuilder( LoadBalancer::class )
328 ->disableOriginalConstructor()
329 ->getMock();
330 $lb2->method( 'getConnection' )->willReturn( $mockDB2 );
331 $lb2->method( 'getServerCount' )->willReturn( 2 );
332 $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
333 $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
334 function () use ( $mockDB2 ) {
335 $p = 0;
336 $p |= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
337 $p |= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
338
339 return (bool)$p;
340 }
341 ) );
342 $lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
343 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
344
345 $bag = new HashBagOStuff();
346 $cp = new ChronologyProtector(
347 $bag,
348 [
349 'ip' => '127.0.0.1',
350 'agent' => "Totally-Not-FireFox"
351 ]
352 );
353
354 $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
355 $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
356 $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
357 $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
358
359 // Nothing to wait for on first HTTP request start
360 $cp->initLB( $lb1 );
361 $cp->initLB( $lb2 );
362 // Record positions in stash on first HTTP request end
363 $cp->shutdownLB( $lb1 );
364 $cp->shutdownLB( $lb2 );
365 $cpIndex = null;
366 $cp->shutdown( null, 'sync', $cpIndex );
367
368 $this->assertEquals( 1, $cpIndex, "CP write index set" );
369
370 // (b) Second HTTP request
371
372 // Load balancer for master DB 1
373 $lb1 = $this->getMockBuilder( LoadBalancer::class )
374 ->disableOriginalConstructor()
375 ->getMock();
376 $lb1->method( 'getServerCount' )->willReturn( 2 );
377 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
378 $lb1->expects( $this->once() )
379 ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
380 // Load balancer for master DB 2
381 $lb2 = $this->getMockBuilder( LoadBalancer::class )
382 ->disableOriginalConstructor()
383 ->getMock();
384 $lb2->method( 'getServerCount' )->willReturn( 2 );
385 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
386 $lb2->expects( $this->once() )
387 ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
388
389 $cp = new ChronologyProtector(
390 $bag,
391 [
392 'ip' => '127.0.0.1',
393 'agent' => "Totally-Not-FireFox"
394 ],
395 $cpIndex
396 );
397
398 // Wait for last positions to be reached on second HTTP request start
399 $cp->initLB( $lb1 );
400 $cp->initLB( $lb2 );
401 // Shutdown (nothing to record)
402 $cp->shutdownLB( $lb1 );
403 $cp->shutdownLB( $lb2 );
404 $cpIndex = null;
405 $cp->shutdown( null, 'sync', $cpIndex );
406
407 $this->assertEquals( null, $cpIndex, "CP write index retained" );
408
409 $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
410 }
411
412 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
413 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype;
414 global $wgSQLiteDataDir;
415
416 return new LBFactoryMulti( $baseOverride + [
417 'sectionsByDB' => [],
418 'sectionLoads' => [
419 'DEFAULT' => [
420 'test-db1' => 1,
421 ],
422 ],
423 'serverTemplate' => $serverOverride + [
424 'dbname' => $wgDBname,
425 'tablePrefix' => $wgDBprefix,
426 'user' => $wgDBuser,
427 'password' => $wgDBpassword,
428 'type' => $wgDBtype,
429 'dbDirectory' => $wgSQLiteDataDir,
430 'flags' => DBO_DEFAULT
431 ],
432 'hostsByName' => [
433 'test-db1' => $wgDBserver,
434 ],
435 'loadMonitorClass' => LoadMonitorNull::class,
436 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
437 'agent' => 'MW-UNIT-TESTS'
438 ] );
439 }
440
441 public function testNiceDomains() {
442 global $wgDBname;
443
444 if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
445 self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
446 return;
447 }
448
449 $factory = $this->newLBFactoryMulti(
450 [],
451 []
452 );
453 $lb = $factory->getMainLB();
454
455 $db = $lb->getConnectionRef( DB_MASTER );
456 $this->assertEquals(
457 wfWikiID(),
458 $db->getDomainID()
459 );
460 unset( $db );
461
462 /** @var IMaintainableDatabase $db */
463 $db = $lb->getConnection( DB_MASTER, [], '' );
464
465 $this->assertEquals(
466 '',
467 $db->getDomainId(),
468 'Null domain ID handle used'
469 );
470 $this->assertEquals(
471 '',
472 $db->getDBname(),
473 'Null domain ID handle used'
474 );
475 $this->assertEquals(
476 '',
477 $db->tablePrefix(),
478 'Main domain ID handle used; prefix is empty though'
479 );
480 $this->assertEquals(
481 $this->quoteTable( $db, 'page' ),
482 $db->tableName( 'page' ),
483 "Correct full table name"
484 );
485 $this->assertEquals(
486 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
487 $db->tableName( "$wgDBname.page" ),
488 "Correct full table name"
489 );
490 $this->assertEquals(
491 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
492 $db->tableName( 'nice_db.page' ),
493 "Correct full table name"
494 );
495
496 $lb->reuseConnection( $db ); // don't care
497
498 $db = $lb->getConnection( DB_MASTER ); // local domain connection
499 $factory->setLocalDomainPrefix( 'my_' );
500
501 $this->assertEquals( $wgDBname, $db->getDBname() );
502 $this->assertEquals(
503 "$wgDBname-my_",
504 $db->getDomainID()
505 );
506 $this->assertEquals(
507 $this->quoteTable( $db, 'my_page' ),
508 $db->tableName( 'page' ),
509 "Correct full table name"
510 );
511 $this->assertEquals(
512 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
513 $db->tableName( 'other_nice_db.page' ),
514 "Correct full table name"
515 );
516
517 $factory->closeAll();
518 $factory->destroy();
519 }
520
521 public function testTrickyDomain() {
522 global $wgDBname;
523
524 if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
525 self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
526 return;
527 }
528
529 $dbname = 'unittest-domain'; // explodes if DB is selected
530 $factory = $this->newLBFactoryMulti(
531 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
532 [
533 'dbName' => 'do_not_select_me' // explodes if DB is selected
534 ]
535 );
536 $lb = $factory->getMainLB();
537 /** @var IMaintainableDatabase $db */
538 $db = $lb->getConnection( DB_MASTER, [], '' );
539
540 $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
541
542 $this->assertEquals(
543 $this->quoteTable( $db, 'page' ),
544 $db->tableName( 'page' ),
545 "Correct full table name"
546 );
547
548 $this->assertEquals(
549 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
550 $db->tableName( "$dbname.page" ),
551 "Correct full table name"
552 );
553
554 $this->assertEquals(
555 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
556 $db->tableName( 'nice_db.page' ),
557 "Correct full table name"
558 );
559
560 $lb->reuseConnection( $db ); // don't care
561
562 $factory->setLocalDomainPrefix( 'my_' );
563 $db = $lb->getConnection( DB_MASTER, [], "$wgDBname-my_" );
564
565 $this->assertEquals(
566 $this->quoteTable( $db, 'my_page' ),
567 $db->tableName( 'page' ),
568 "Correct full table name"
569 );
570 $this->assertEquals(
571 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
572 $db->tableName( 'other_nice_db.page' ),
573 "Correct full table name"
574 );
575 $this->assertEquals(
576 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
577 $db->tableName( 'garbage-db.page' ),
578 "Correct full table name"
579 );
580
581 $lb->reuseConnection( $db ); // don't care
582
583 $factory->closeAll();
584 $factory->destroy();
585 }
586
587 public function testInvalidSelectDB() {
588 // FIXME: fails under sqlite
589 $this->markTestSkippedIfDbType( 'sqlite' );
590 $dbname = 'unittest-domain'; // explodes if DB is selected
591 $factory = $this->newLBFactoryMulti(
592 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
593 [
594 'dbName' => 'do_not_select_me' // explodes if DB is selected
595 ]
596 );
597 $lb = $factory->getMainLB();
598 /** @var IDatabase $db */
599 $db = $lb->getConnection( DB_MASTER, [], '' );
600
601 if ( $db->getType() === 'sqlite' ) {
602 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
603 } elseif ( $db->databasesAreIndependent() ) {
604 try {
605 $e = null;
606 $db->selectDB( 'garbage-db' );
607 } catch ( \Wikimedia\Rdbms\DBConnectionError $e ) {
608 // expected
609 }
610 $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError::class, $e );
611 $this->assertFalse( $db->isOpen() );
612 } else {
613 \Wikimedia\suppressWarnings();
614 try {
615 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
616 $this->fail( "No error thrown." );
617 } catch ( \Wikimedia\Rdbms\DBExpectedError $e ) {
618 $this->assertEquals(
619 "Could not select database 'garbage-db'.",
620 $e->getMessage()
621 );
622 }
623 \Wikimedia\restoreWarnings();
624 }
625 }
626
627 public function testRedefineLocalDomain() {
628 global $wgDBname;
629
630 if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
631 self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
632 return;
633 }
634
635 $factory = $this->newLBFactoryMulti(
636 [],
637 []
638 );
639 $lb = $factory->getMainLB();
640
641 $conn1 = $lb->getConnectionRef( DB_MASTER );
642 $this->assertEquals(
643 wfWikiID(),
644 $conn1->getDomainID()
645 );
646 unset( $conn1 );
647
648 $factory->redefineLocalDomain( 'somedb-prefix' );
649 $this->assertEquals( 'somedb-prefix', $factory->getLocalDomainID() );
650
651 $domain = new DatabaseDomain( $wgDBname, null, 'pref' );
652 $factory->redefineLocalDomain( $domain );
653
654 $n = 0;
655 $lb->forEachOpenConnection( function () use ( &$n ) {
656 ++$n;
657 } );
658 $this->assertEquals( 0, $n, "Connections closed" );
659
660 $conn2 = $lb->getConnectionRef( DB_MASTER );
661 $this->assertEquals(
662 $domain->getId(),
663 $conn2->getDomainID()
664 );
665 unset( $conn2 );
666
667 $factory->closeAll();
668 $factory->destroy();
669 }
670
671 private function quoteTable( Database $db, $table ) {
672 if ( $db->getType() === 'sqlite' ) {
673 return $table;
674 } else {
675 return $db->addIdentifierQuotes( $table );
676 }
677 }
678
679 /**
680 * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
681 * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
682 */
683 public function testCPPosIndexCookieValues() {
684 $time = 1526522031;
685 $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
686
687 $this->assertEquals(
688 '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
689 LBFactory::makeCookieValueFromCPIndex( 3, 542, $agentId )
690 );
691
692 $lbFactory = $this->newLBFactoryMulti();
693 $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
694 $this->assertEquals(
695 '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
696 LBFactory::makeCookieValueFromCPIndex( 1, 542, $agentId )
697 );
698
699 $this->assertSame(
700 null,
701 LBFactory::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
702 'No time set'
703 );
704 $this->assertSame(
705 null,
706 LBFactory::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
707 'No agent set'
708 );
709 $this->assertSame(
710 null,
711 LBFactory::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
712 'Bad index'
713 );
714
715 $this->assertSame(
716 2,
717 LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
718 'Fresh'
719 );
720 $this->assertSame(
721 2,
722 LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 9 - 10 )['index'],
723 'Almost stale'
724 );
725 $this->assertSame(
726 null,
727 LBFactory::getCPInfoFromCookieValue( "0@$time#$agentId", $time + 9 - 10 )['index'],
728 'Almost stale; bad index'
729 );
730 $this->assertSame(
731 null,
732 LBFactory::getCPInfoFromCookieValue( "2@$time#$agentId", $time + 11 - 10 )['index'],
733 'Stale'
734 );
735
736 $this->assertSame(
737 $agentId,
738 LBFactory::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
739 'Live (client ID)'
740 );
741 $this->assertSame(
742 null,
743 LBFactory::getCPInfoFromCookieValue( "5@$time#$agentId", $time + 11 - 10 )['clientId'],
744 'Stale (client ID)'
745 );
746 }
747 }