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