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