3 namespace MediaWiki\Tests\Maintenance
;
5 use Wikimedia\Rdbms\ILoadBalancer
;
6 use Wikimedia\Rdbms\LBFactory
;
7 use Wikimedia\Stats\StatsFactory
;
10 * @covers \GetLagTimes
13 class GetLagTimesTest
extends MaintenanceBaseTestCase
{
15 public function getMaintenanceClass() {
16 return \GetLagTimes
::class;
19 private function getLB( $lag ) {
20 $lb = $this->createMock( ILoadBalancer
::class );
21 $lb->method( 'getServerCount' )->willReturn( 2 );
22 $lb->method( 'getLagTimes' )->willReturn( [ $lag ] );
23 $lb->method( 'getServerName' )->willReturn( 'localhost' );
27 public static function provideLagTimes() {
29 'No lag' => [ 0, '/localhost\s+0 $/m' ],
30 'Some lag' => [ 7, '/localhost\s+7 \*{7}$/m' ],
31 'More than 40 seconds lag' => [ 41, '/localhost\s+41 \*{40}$/m' ],
32 'Not replicating' => [
33 false, '/localhost\s+0 replication stopped or errored$/m' ],
38 * @dataProvider provideLagTimes
40 public function testReportedOutput( $lag, $expected ) {
41 $lbFactory = $this->createMock( LBFactory
::class );
42 $lbFactory->method( 'getAllMainLBs' )
44 'cluster1' => $this->getLB( $lag ),
46 $lbFactory->method( 'getAllExternalLBs' )->willReturn( [] );
48 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
50 $this->maintenance
->execute();
52 $this->expectOutputRegex( $expected );
55 public function testStats() {
56 $lbFactory = $this->createMock( LBFactory
::class );
57 $lbFactory->method( 'getAllMainLBs' )
59 'cluster1' => $this->getLB( 1 ),
60 'cluster2' => $this->getLB( false ),
62 $lbFactory->method( 'getAllExternalLBs' )
64 'externalCluster' => $this->getLB( 14 ),
66 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
68 $dummyGauge = StatsFactory
::newNull()->getGauge( 'dummy' );
69 $sfmock = $this->createConfiguredMock( StatsFactory
::class, [
70 'getGauge' => $dummyGauge
72 $this->setService( 'StatsFactory', $sfmock );
74 $this->maintenance
->setOption( 'report', true );
75 $this->maintenance
->execute();
79 [ 'cluster1.localhost', 1000.0 ],
80 [ 'cluster2.localhost', 0.0 ],
81 [ 'external.localhost', 14000.0 ],
83 [ 'cluster1.localhost', 1.0 ],
84 [ 'cluster2.localhost', 0.0 ],
85 [ 'external.localhost', 14.0 ],
87 foreach ( $dummyGauge->getSamples() as $sample ) {
88 $namespaced = implode( '.', $sample->getLabelValues() );
89 $this->assertContains( [ $namespaced, $sample->getValue() ], $expectedSamples );