|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/dog-statsd |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/dog-statsd |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DogStatsD\Test\Unit; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DogStatsD\Test\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
class ConnectionTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Non-integer ports are not acceptable |
|
22
|
|
|
* |
|
23
|
|
|
* @expectedException \Graze\DogStatsD\Exception\ConnectionException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testInvalidHost() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->client->configure([ |
|
28
|
|
|
'host' => 'hostdoesnotexiststalleverlol.stupidtld', |
|
29
|
|
|
]); |
|
30
|
|
|
$this->client->increment('test'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->client->configure([ |
|
36
|
|
|
'host' => 'localhost', |
|
37
|
|
|
'timeout' => 123.425, |
|
38
|
|
|
]); |
|
39
|
|
|
$this->assertAttributeSame(123.425, 'timeout', $this->client); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testCanBeConfiguredToThrowErrors() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->client->configure([ |
|
45
|
|
|
'host' => 'hostdoesnotexiststalleverlol.stupidtld', |
|
46
|
|
|
'onError' => 'error', |
|
47
|
|
|
]); |
|
48
|
|
|
$handlerInvoked = false; |
|
49
|
|
|
|
|
50
|
|
|
$testCase = $this; |
|
51
|
|
|
|
|
52
|
|
|
set_error_handler( |
|
53
|
|
|
function ($errno, $errstr) use ($testCase, &$handlerInvoked) { |
|
54
|
|
|
$handlerInvoked = true; |
|
55
|
|
|
|
|
56
|
|
|
$testCase->assertSame(E_USER_WARNING, $errno); |
|
57
|
|
|
$testCase->assertSame( |
|
58
|
|
|
'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)', |
|
59
|
|
|
$errstr |
|
60
|
|
|
); |
|
61
|
|
|
}, |
|
62
|
|
|
E_USER_WARNING |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$this->client->increment('test'); |
|
66
|
|
|
restore_error_handler(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertTrue($handlerInvoked); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testCanBeConfiguredToNotThrowOnError() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->client->configure([ |
|
74
|
|
|
'host' => 'hostdoesnotexiststalleverlol.stupidtld', |
|
75
|
|
|
'onError' => 'ignore', |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$this->client->increment('test'); |
|
79
|
|
|
$this->assertFalse($this->client->wasSuccessful()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertAttributeEquals(ini_get('default_socket_timeout'), 'timeout', $this->client); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|