1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
<?php
namespace MediaWiki\Tests\Maintenance;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Stats\StatsFactory;
/**
* @covers \GetLagTimes
* @group Maintenance
*/
class GetLagTimesTest extends MaintenanceBaseTestCase {
public function getMaintenanceClass() {
return \GetLagTimes::class;
}
private function getLB( $lag ) {
$lb = $this->createMock( ILoadBalancer::class );
$lb->method( 'getServerCount' )->willReturn( 2 );
$lb->method( 'getLagTimes' )->willReturn( [ $lag ] );
$lb->method( 'getServerName' )->willReturn( 'localhost' );
return $lb;
}
public static function provideLagTimes() {
return [
'No lag' => [ 0, '/localhost\s+0 $/m' ],
'Some lag' => [ 7, '/localhost\s+7 \*{7}$/m' ],
'More than 40 seconds lag' => [ 41, '/localhost\s+41 \*{40}$/m' ],
'Not replicating' => [
false, '/localhost\s+0 replication stopped or errored$/m' ],
];
}
/**
* @dataProvider provideLagTimes
*/
public function testReportedOutput( $lag, $expected ) {
$lbFactory = $this->createMock( LBFactory::class );
$lbFactory->method( 'getAllMainLBs' )
->willReturn( [
'cluster1' => $this->getLB( $lag ),
] );
$lbFactory->method( 'getAllExternalLBs' )->willReturn( [] );
$this->setService( 'DBLoadBalancerFactory', $lbFactory );
$this->maintenance->execute();
$this->expectOutputRegex( $expected );
}
public function testStats() {
$lbFactory = $this->createMock( LBFactory::class );
$lbFactory->method( 'getAllMainLBs' )
->willReturn( [
'cluster1' => $this->getLB( 1 ),
'cluster2' => $this->getLB( false ),
] );
$lbFactory->method( 'getAllExternalLBs' )
->willReturn( [
'externalCluster' => $this->getLB( 14 ),
] );
$this->setService( 'DBLoadBalancerFactory', $lbFactory );
$dummyGauge = StatsFactory::newNull()->getGauge( 'dummy' );
$sfmock = $this->createConfiguredMock( StatsFactory::class, [
'getGauge' => $dummyGauge
] );
$this->setService( 'StatsFactory', $sfmock );
$this->maintenance->setOption( 'report', true );
$this->maintenance->execute();
$expectedSamples = [
// milliseconds
[ 'cluster1.localhost', 1000.0 ],
[ 'cluster2.localhost', 0.0 ],
[ 'external.localhost', 14000.0 ],
// seconds
[ 'cluster1.localhost', 1.0 ],
[ 'cluster2.localhost', 0.0 ],
[ 'external.localhost', 14.0 ],
];
foreach ( $dummyGauge->getSamples() as $sample ) {
$namespaced = implode( '.', $sample->getLabelValues() );
$this->assertContains( [ $namespaced, $sample->getValue() ], $expectedSamples );
}
}
}
|