From eb0ca335f380b71feb682ed2d825a0cd0c282038 Mon Sep 17 00:00:00 2001 From: Benjamin Zikarsky Date: Thu, 25 Oct 2018 08:33:08 +0200 Subject: [PATCH] Update StreamSocketClient with methods to set/get stream-context If the connection is not currently opened, setContext allows to set the stream-context for the next connection. This feature is the basis for the use of more abstract, optional stream options, like http proxies. --- src/Gelf/Transport/StreamSocketClient.php | 38 +++++++++++++ .../Transport/StreamSocketClientTcpTest.php | 53 +++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/src/Gelf/Transport/StreamSocketClient.php b/src/Gelf/Transport/StreamSocketClient.php index c445c32..2b6add1 100644 --- a/src/Gelf/Transport/StreamSocketClient.php +++ b/src/Gelf/Transport/StreamSocketClient.php @@ -258,6 +258,16 @@ public function close() $this->socket = null; } + /** + * Checks if the socket is closed + * + * @return bool + */ + public function isClosed() + { + return $this->socket === null; + } + /** * Returns the current connect-timeout * @@ -275,6 +285,34 @@ public function getConnectTimeout() */ public function setConnectTimeout($timeout) { + if (!$this->isClosed()) { + throw new \LogicException("Cannot change socket properties with an open connection"); + } + $this->connectTimeout = $timeout; } + + /** + * Returns the stream context + * + * @return array + */ + public function getContext() + { + return $this->context; + } + + /** + * Sets the stream context + * + * @param array $context + */ + public function setContext(array $context) + { + if (!$this->isClosed()) { + throw new \LogicException("Cannot change socket properties with an open connection"); + } + + $this->context = $context; + } } diff --git a/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php b/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php index 42d88c4..f096397 100644 --- a/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php +++ b/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php @@ -182,7 +182,10 @@ public function testCloseWithoutConnectionWrite() { // close unopened stream $this->socketClient->close(); + $this->assertTrue($this->socketClient->isClosed()); + $this->socketClient->write("abcd"); + $this->assertFalse($this->socketClient->isClosed()); $client = stream_socket_accept($this->serverSocket); $this->assertEquals("abcd", fread($client, 4)); } @@ -190,10 +193,12 @@ public function testCloseWithoutConnectionWrite() public function testCloseWrite() { $this->socketClient->write("abcd"); + $this->assertFalse($this->socketClient->isClosed()); $client = stream_socket_accept($this->serverSocket); $this->assertEquals("abcd", fread($client, 4)); $this->socketClient->close(); + $this->assertTrue($this->socketClient->isClosed()); $this->socketClient->write("efgh"); $client2 = stream_socket_accept($this->serverSocket); @@ -215,10 +220,58 @@ public function testStreamContext() ); $client = new StreamSocketClient("tcp", $this->host, $this->port, $context); + $this->assertEquals($context, $client->getContext()); + $this->assertEquals($testName, stream_socket_get_name($client->getSocket(), false)); $this->assertNotEquals($testName, stream_socket_get_name($this->socketClient->getSocket(), false)); } + /** + * @group hhvm-failures + */ + public function testUpdateStreamContext() + { + $this->failsOnHHVM(); + + $testName = '127.0.0.1:12345'; + $context = array( + 'socket' => array( + 'bindto' => $testName + ) + ); + + $this->assertEquals(array(), $this->socketClient->getContext()); + $this->assertNotEquals($testName, stream_socket_get_name($this->socketClient->getSocket(), false)); + $this->socketClient->close(); + + $this->socketClient->setContext($context); + $this->assertEquals($context, $this->socketClient->getContext()); + + $this->assertEquals($testName, stream_socket_get_name($this->socketClient->getSocket(), false)); + } + + /** + * @expectedException \LogicException + */ + public function testSetContextFailsAfterConnect() + { + // enforce connect + $this->socketClient->getSocket(); + + $this->socketClient->setContext(array("foo" => "bar")); + } + + /** + * @expectedException \LogicException + */ + public function testSetConnectTimeoutFailsAfterConnect() + { + // enforce connect + $this->socketClient->getSocket(); + + $this->socketClient->setConnectTimeout(1); + } + public function testConnectTimeout() { $this->assertEquals(StreamSocketClient::SOCKET_TIMEOUT, $this->socketClient->getConnectTimeout());