Silently handle socket errors in the UdpAdapter (closes #27), add testcase

This commit is contained in:
Evan Darwin
2015-06-20 13:03:09 -07:00
parent d238c4985c
commit 308aba8817
2 changed files with 33 additions and 0 deletions
+9
View File
@@ -15,9 +15,18 @@ final class UdpAdapter extends AdapterAbstract
public function write($message)
{
// Create a handler in order to handle the 'Host is down' message
set_error_handler(function() {
// Suppress the error, this is the UDP adapter and if we can't send
// it then we shouldn't inturrupt their application.
});
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($socket, $message, strlen($message), 0, $this->getOptions()->getHost(), $this->getOptions()->getPort());
socket_close($socket);
// Remove our error handler.
restore_error_handler();
}
private function serialize(array $message)
+24
View File
@@ -359,6 +359,30 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]);
}
/**
* Test that we handle socket problems correctly in the UDP
* adapter, and that they don't inturrupt the user's application.
*
* @group udp
*/
public function testReplicateIssue27()
{
$options = new \InfluxDB\Options();
// Configure options
$options->setHost('172.16.1.182');
$options->setPort(4444);
$options->setDatabase('...');
$options->setUsername('root');
$options->setPassword('root');
$httpAdapter = new \InfluxDB\Adapter\UdpAdapter($options);
$client = new \InfluxDB\Client();
$client->setAdapter($httpAdapter);
$client->mark("udp.test", ["mark" => "element"]);
}
public function testListActiveDatabses()
{
$databases = $this->object->getDatabases();