Debian Codegraph @

vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdClientTest.php

raw
1 <?php
2
3 namespace Liuggio\StatsdClient;
4
5 use Liuggio\StatsdClient\StatsdClient;
6 use Liuggio\StatsdClient\Entity\StatsdData;
7
8 class StatsdClientTest extends \PHPUnit_Framework_TestCase
9 {
10
11 public function mockSenderWithAssertionOnWrite($messageToAssert=null) {
12
13 $mock = $this->getMockBuilder('\Liuggio\StatsdClient\Sender\SocketSender') ->disableOriginalConstructor() ->getMock();
14
15 $phpUnit = $this;
16 $mock->expects($this->any())
17 ->method('open')
18 ->will($this->returnValue(true));
19 // if the input is an array expects a call foreach item
20 if (is_array($messageToAssert)) {
21 $index = 0;
22 foreach ($messageToAssert as $oneMessage) {
23 $index++;
24 $mock->expects($this->at($index))
25 ->method('write')
26 ->will($this->returnCallBack(function($fp, $message) use ($phpUnit, $oneMessage) {
27 $phpUnit->assertEquals($message, $oneMessage);
28 }));
29 }
30 } else if (null !== $messageToAssert){
31 // if the input is a string expects only once
32 $mock->expects($this->once())
33 ->method('write')
34 ->will($this->returnCallBack(function($fp, $message) use ($phpUnit, $messageToAssert) {
35 $phpUnit->assertEquals($message, $messageToAssert);
36 }));
37 }
38 return $mock;
39 }
40
41 public function mockStatsdClientWithAssertionOnWrite($messageToAssert) {
42
43 $mockSender = $this->mockSenderWithAssertionOnWrite($messageToAssert);
44
45 return new StatsdClient($mockSender, false, false);
46 }
47
48 public function mockFactory() {
49
50 $mock = $this->getMock('\Liuggio\StatsdClient\Factory\StatsdDataFactory', array('timing'));
51
52 $statsData = new StatsdData();
53 $statsData->setKey('key');
54 $statsData->setValue('1');
55 $statsData->setMetric('ms');
56
57 $phpUnit = $this;
58 $mock->expects($this->any())
59 ->method('timing')
60 ->will($this->returnValue($statsData));
61
62 return $mock;
63 }
64
65 public static function provider()
66 {
67 /**
68 * First
69 */
70 $statsData0 = new StatsdData();
71 $statsData0->setKey('keyTiming');
72 $statsData0->setValue('1');
73 $statsData0->setMetric('ms');
74 /**
75 * Second
76 */
77 $stats1 = array();
78 $statsData1 = new StatsdData();
79 $statsData1->setKey('keyTiming');
80 $statsData1->setValue('1');
81 $statsData1->setMetric('ms');
82 $stats1[] = $statsData1;
83
84 $statsData1 = new StatsdData();
85 $statsData1->setKey('keyIncrement');
86 $statsData1->setValue('1');
87 $statsData1->setMetric('c');
88 $stats1[] = $statsData1;
89
90 return array(
91 array($statsData0, "keyTiming:1|ms"),
92 array($stats1, array("keyTiming:1|ms", "keyIncrement:1|c")),
93 );
94 }
95 public static function providerSend()
96 {
97 return array(
98 array(array('gauge:value|g'), 'gauge:value|g'),
99 array(array("keyTiming:1|ms", "keyIncrement:1|c"), array("keyTiming:1|ms", "keyIncrement:1|c")),
100 );
101 }
102
103 /**
104 * @dataProvider provider
105 */
106 public function testPrepareAndSend($statsdInput, $assertion) {
107
108 $statsdMock = $this->mockStatsdClientWithAssertionOnWrite($assertion);
109 $statsdMock->send($statsdInput);
110 }
111
112 /**
113 * @dataProvider providerSend
114 */
115 public function testSend($array, $assertion) {
116
117 $statsdMock = $this->mockStatsdClientWithAssertionOnWrite($assertion);
118 $statsdMock->send($array);
119 }
120
121 public function testReduceCount()
122 {
123 $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);
124
125 $entity0 = new StatsdData();
126 $entity0->setKey('key1');
127 $entity0->setValue('1');
128 $entity0->setMetric('c');
129 $array0[] = $entity0;
130
131 $entity0 = new StatsdData();
132 $entity0->setKey('key2');
133 $entity0->setValue('2');
134 $entity0->setMetric('ms');
135 $array0[] = $entity0;
136
137 $reducedMessage = array('key1:1|c' . PHP_EOL . 'key2:2|ms');
138
139 $this->assertEquals($statsd->reduceCount($array0), $reducedMessage);
140
141 }
142
143 public function testReduceWithString()
144 {
145 $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);
146
147 $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:';
148 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
149 $array0[] = $msg;
150
151 $msg = 'B3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:';
152 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
153 $array0[] = $msg;
154 $reduced = $statsd->reduceCount($array0);
155 $combined = $array0[0] . PHP_EOL . $array0[1];
156 $this->assertEquals($combined, $reduced[0]);
157 }
158
159
160 public function testReduceWithMaxUdpPacketSplitInTwoPacket()
161 {
162 $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);
163
164 $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'; //1
165 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //2
166 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //3
167 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //4
168 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; //500
169 $array0[] = $msg;
170
171 $msg = 'Bkey:';
172 $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
173 $array0[] = $msg;
174
175 $reduced = $statsd->reduceCount($array0);
176
177 $this->assertEquals($array0[0], $reduced[0]);
178 $this->assertEquals($array0[1], $reduced[1]);
179 }
180
181
182
183 public function testMultiplePacketsWithReducing()
184 {
185
186 $msg = 'A23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789';
187 $array0[] = $msg;
188 $array0[] = $msg;
189 $array0[] = $msg;
190 $array0[] = $msg;
191 $array0[] = $msg;
192 $array0[] = $msg;
193 $array0[] = $msg;
194 $array0[] = $msg;
195
196 $total = count($array0) * strlen($msg);
197
198 $reducedPacketsAssertion = (int) ceil($total / StatsdClientInterface::MAX_UDP_SIZE_STR);
199
200
201 $mockSender = $this->mockSenderWithAssertionOnWrite();
202 $statsd = new StatsdClient($mockSender, true, false);
203 $reduced = $statsd->reduceCount($array0);
204
205 $this->assertEquals($reducedPacketsAssertion, count($reduced));
206 }
207
208 public function testSampleRate()
209 {
210 $senderMock = $this->getMock('Liuggio\StatsdClient\Sender\SenderInterface');
211 $senderMock
212 ->expects($this->once())
213 ->method('open')
214 ->will($this->returnValue(true))
215 ;
216 $senderMock
217 ->expects($this->once())
218 ->method('write')
219 ->with($this->anything(), 'foo|@0.2')
220 ;
221 $client = new StatsdClient($senderMock, false, false);
222
223 $client->send(
224 'foo',
225 0.2
226 );
227 }
228 }