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