Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / GetLagTimesTest.php
blob8ba5b0ffb883d87dd5b56acf65dbd84180c84719
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use Wikimedia\Rdbms\ILoadBalancer;
6 use Wikimedia\Rdbms\LBFactory;
7 use Wikimedia\Stats\StatsFactory;
9 /**
10 * @covers \GetLagTimes
11 * @group Maintenance
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' );
24 return $lb;
27 public static function provideLagTimes() {
28 return [
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' ],
37 /**
38 * @dataProvider provideLagTimes
40 public function testReportedOutput( $lag, $expected ) {
41 $lbFactory = $this->createMock( LBFactory::class );
42 $lbFactory->method( 'getAllMainLBs' )
43 ->willReturn( [
44 'cluster1' => $this->getLB( $lag ),
45 ] );
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' )
58 ->willReturn( [
59 'cluster1' => $this->getLB( 1 ),
60 'cluster2' => $this->getLB( false ),
61 ] );
62 $lbFactory->method( 'getAllExternalLBs' )
63 ->willReturn( [
64 'externalCluster' => $this->getLB( 14 ),
65 ] );
66 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
68 $dummyGauge = StatsFactory::newNull()->getGauge( 'dummy' );
69 $sfmock = $this->createConfiguredMock( StatsFactory::class, [
70 'getGauge' => $dummyGauge
71 ] );
72 $this->setService( 'StatsFactory', $sfmock );
74 $this->maintenance->setOption( 'report', true );
75 $this->maintenance->execute();
77 $expectedSamples = [
78 // milliseconds
79 [ 'cluster1.localhost', 1000.0 ],
80 [ 'cluster2.localhost', 0.0 ],
81 [ 'external.localhost', 14000.0 ],
82 // seconds
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 );