Merge "Add @covers tags for more tests"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / SamplingStatsdClientTest.php
1 <?php
2
3 use Liuggio\StatsdClient\Entity\StatsdData;
4 use Liuggio\StatsdClient\Sender\SenderInterface;
5
6 /**
7 * @covers SamplingStatsdClient
8 */
9 class SamplingStatsdClientTest extends PHPUnit_Framework_TestCase {
10
11 use MediaWikiCoversValidator;
12
13 /**
14 * @dataProvider samplingDataProvider
15 */
16 public function testSampling( $data, $sampleRate, $seed, $expectWrite ) {
17 $sender = $this->getMockBuilder( SenderInterface::class )->getMock();
18 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
19 if ( $expectWrite ) {
20 $sender->expects( $this->once() )->method( 'write' )
21 ->with( $this->anything(), $this->equalTo( $data ) );
22 } else {
23 $sender->expects( $this->never() )->method( 'write' );
24 }
25 mt_srand( $seed );
26 $client = new SamplingStatsdClient( $sender );
27 $client->send( $data, $sampleRate );
28 }
29
30 public function samplingDataProvider() {
31 $unsampled = new StatsdData();
32 $unsampled->setKey( 'foo' );
33 $unsampled->setValue( 1 );
34
35 $sampled = new StatsdData();
36 $sampled->setKey( 'foo' );
37 $sampled->setValue( 1 );
38 $sampled->setSampleRate( '0.1' );
39
40 return [
41 // $data, $sampleRate, $seed, $expectWrite
42 [ $unsampled, 1, 0 /*0.44*/, true ],
43 [ $sampled, 1, 0 /*0.44*/, false ],
44 [ $sampled, 1, 4 /*0.03*/, true ],
45 [ $unsampled, 0.1, 0 /*0.44*/, false ],
46 [ $sampled, 0.5, 0 /*0.44*/, false ],
47 [ $sampled, 0.5, 4 /*0.03*/, false ],
48 ];
49 }
50
51 public function testSetSamplingRates() {
52 $matching = new StatsdData();
53 $matching->setKey( 'foo.bar' );
54 $matching->setValue( 1 );
55
56 $nonMatching = new StatsdData();
57 $nonMatching->setKey( 'oof.bar' );
58 $nonMatching->setValue( 1 );
59
60 $sender = $this->getMockBuilder( SenderInterface::class )->getMock();
61 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
62 $sender->expects( $this->once() )->method( 'write' )->with( $this->anything(),
63 $this->equalTo( $nonMatching ) );
64
65 $client = new SamplingStatsdClient( $sender );
66 $client->setSamplingRates( [ 'foo.*' => 0.2 ] );
67
68 mt_srand( 0 ); // next random is 0.44
69 $client->send( $matching );
70 mt_srand( 0 );
71 $client->send( $nonMatching );
72 }
73 }