| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 6 |
use Wikimedia\Rdbms\LBFactory; |
| 7 |
use Wikimedia\Stats\StatsFactory; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class GetLagTimesTest extends MaintenanceBaseTestCase { |
| 14 |
|
| 15 |
public function getMaintenanceClass() { |
| 16 |
return \GetLagTimes::class; |
| 17 |
} |
| 18 |
|
| 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; |
| 25 |
} |
| 26 |
|
| 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' ], |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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( [] ); |
| 47 |
|
| 48 |
$this->setService( 'DBLoadBalancerFactory', $lbFactory ); |
| 49 |
|
| 50 |
$this->maintenance->execute(); |
| 51 |
|
| 52 |
$this->expectOutputRegex( $expected ); |
| 53 |
} |
| 54 |
|
| 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 ); |
| 67 |
|
| 68 |
$dummyGauge = StatsFactory::newNull()->getGauge( 'dummy' ); |
| 69 |
$sfmock = $this->createConfiguredMock( StatsFactory::class, [ |
| 70 |
'getGauge' => $dummyGauge |
| 71 |
] ); |
| 72 |
$this->setService( 'StatsFactory', $sfmock ); |
| 73 |
|
| 74 |
$this->maintenance->setOption( 'report', true ); |
| 75 |
$this->maintenance->execute(); |
| 76 |
|
| 77 |
$expectedSamples = [ |
| 78 |
|
| 79 |
[ 'cluster1.localhost', 1000.0 ], |
| 80 |
[ 'cluster2.localhost', 0.0 ], |
| 81 |
[ 'external.localhost', 14000.0 ], |
| 82 |
|
| 83 |
[ 'cluster1.localhost', 1.0 ], |
| 84 |
[ 'cluster2.localhost', 0.0 ], |
| 85 |
[ 'external.localhost', 14.0 ], |
| 86 |
]; |
| 87 |
foreach ( $dummyGauge->getSamples() as $sample ) { |
| 88 |
$namespaced = implode( '.', $sample->getLabelValues() ); |
| 89 |
$this->assertContains( [ $namespaced, $sample->getValue() ], $expectedSamples ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} |