summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/tests/Liuggio/StatsdClient/StatsdClientTest.php
blob: 865f1b2d1a90738b25c4098908573fad1b32fb43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php

namespace Liuggio\StatsdClient;

use Liuggio\StatsdClient\StatsdClient;
use Liuggio\StatsdClient\Entity\StatsdData;

class StatsdClientTest extends \PHPUnit_Framework_TestCase
{

    public function mockSenderWithAssertionOnWrite($messageToAssert=null) {

        $mock = $this->getMockBuilder('\Liuggio\StatsdClient\Sender\SocketSender') ->disableOriginalConstructor() ->getMock();

        $phpUnit = $this;
        $mock->expects($this->any())
            ->method('open')
            ->will($this->returnValue(true));
        // if the input is an array expects a call foreach item
        if (is_array($messageToAssert)) {
            $index = 0;
            foreach ($messageToAssert as $oneMessage) {
                $index++;
                $mock->expects($this->at($index))
                    ->method('write')
                    ->will($this->returnCallBack(function($fp, $message) use ($phpUnit, $oneMessage) {
                      $phpUnit->assertEquals($message, $oneMessage);
                }));
            }
        } else if (null !== $messageToAssert){
            // if the input is a string expects only once
            $mock->expects($this->once())
                ->method('write')
                ->will($this->returnCallBack(function($fp, $message) use ($phpUnit, $messageToAssert) {
                 $phpUnit->assertEquals($message, $messageToAssert);
            }));
        }
        return $mock;
    }

    public function mockStatsdClientWithAssertionOnWrite($messageToAssert) {

        $mockSender = $this->mockSenderWithAssertionOnWrite($messageToAssert);

        return new StatsdClient($mockSender, false, false);
    }

    public function mockFactory() {

        $mock =  $this->getMock('\Liuggio\StatsdClient\Factory\StatsdDataFactory', array('timing'));

        $statsData = new StatsdData();
        $statsData->setKey('key');
        $statsData->setValue('1');
        $statsData->setMetric('ms');

        $phpUnit = $this;
        $mock->expects($this->any())
            ->method('timing')
            ->will($this->returnValue($statsData));

        return $mock;
    }

    public static function provider()
    {
        /**
         * First
         */
        $statsData0 = new StatsdData();
        $statsData0->setKey('keyTiming');
        $statsData0->setValue('1');
        $statsData0->setMetric('ms');
        /**
         * Second
         */
        $stats1 = array();
        $statsData1 = new StatsdData();
        $statsData1->setKey('keyTiming');
        $statsData1->setValue('1');
        $statsData1->setMetric('ms');
        $stats1[] = $statsData1;

        $statsData1 = new StatsdData();
        $statsData1->setKey('keyIncrement');
        $statsData1->setValue('1');
        $statsData1->setMetric('c');
        $stats1[] = $statsData1;

        return array(
            array($statsData0, "keyTiming:1|ms"),
            array($stats1, array("keyTiming:1|ms", "keyIncrement:1|c")),
        );
    }
    public static function providerSend()
    {
        return array(
            array(array('gauge:value|g'), 'gauge:value|g'),
            array(array("keyTiming:1|ms", "keyIncrement:1|c"), array("keyTiming:1|ms", "keyIncrement:1|c")),
        );
    }

    /**
     * @dataProvider provider
     */
    public function testPrepareAndSend($statsdInput, $assertion) {

        $statsdMock = $this->mockStatsdClientWithAssertionOnWrite($assertion);
        $statsdMock->send($statsdInput);
    }

    /**
     * @dataProvider providerSend
     */
    public function testSend($array, $assertion) {

        $statsdMock = $this->mockStatsdClientWithAssertionOnWrite($assertion);
        $statsdMock->send($array);
    }

    public function testReduceCount()
    {
        $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

        $entity0 = new StatsdData();
        $entity0->setKey('key1');
        $entity0->setValue('1');
        $entity0->setMetric('c');
        $array0[] = $entity0;

        $entity0 = new StatsdData();
        $entity0->setKey('key2');
        $entity0->setValue('2');
        $entity0->setMetric('ms');
        $array0[] = $entity0;

        $reducedMessage = array('key1:1|c' . PHP_EOL . 'key2:2|ms');

        $this->assertEquals($statsd->reduceCount($array0), $reducedMessage);

    }

    public function testReduceWithString()
    {
        $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

        $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:';
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
        $array0[] = $msg;

        $msg = 'B3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789:';
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
        $array0[] = $msg;
        $reduced = $statsd->reduceCount($array0);
        $combined = $array0[0] . PHP_EOL . $array0[1];
        $this->assertEquals($combined, $reduced[0]);
    }


    public function testReduceWithMaxUdpPacketSplitInTwoPacket()
    {
        $statsd = $this->mockStatsdClientWithAssertionOnWrite(null);

        $msg = 'A3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789';    //1
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //2
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //3
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '; //4
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c'; //500
        $array0[] = $msg;

        $msg = 'Bkey:';
        $msg .= '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789|c';
        $array0[] = $msg;

        $reduced = $statsd->reduceCount($array0);

        $this->assertEquals($array0[0], $reduced[0]);
        $this->assertEquals($array0[1], $reduced[1]);
    }



    public function testMultiplePacketsWithReducing()
    {

        $msg = 'A23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789';
        $array0[] = $msg;
        $array0[] = $msg;
        $array0[] = $msg;
        $array0[] = $msg;
        $array0[] = $msg;
        $array0[] = $msg;
        $array0[] = $msg;
        $array0[] = $msg;

        $total = count($array0) * strlen($msg);

        $reducedPacketsAssertion = (int) ceil($total / StatsdClientInterface::MAX_UDP_SIZE_STR);


        $mockSender = $this->mockSenderWithAssertionOnWrite();
        $statsd = new  StatsdClient($mockSender, true, false);
        $reduced = $statsd->reduceCount($array0);

        $this->assertEquals($reducedPacketsAssertion, count($reduced));
    }

    public function testSampleRate()
    {
        $senderMock = $this->getMock('Liuggio\StatsdClient\Sender\SenderInterface');
        $senderMock
            ->expects($this->once())
            ->method('open')
            ->will($this->returnValue(true))
        ;
        $senderMock
            ->expects($this->once())
            ->method('write')
            ->with($this->anything(), 'foo|@0.2')
        ;
        $client = new StatsdClient($senderMock, false, false);

        $client->send(
            'foo',
            0.2
        );
    }
}