Skip to content

Commit

Permalink
Update StreamSocketClient with methods to set/get stream-context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bzikarsky authored and Benjamin Zikarsky committed Oct 25, 2018
1 parent 9ca9c45 commit eb0ca33
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Gelf/Transport/StreamSocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
}
53 changes: 53 additions & 0 deletions tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,23 @@ 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));
}

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);
Expand All @@ -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());
Expand Down

0 comments on commit eb0ca33

Please sign in to comment.