Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 5abcbe2

Browse files
authored
Merge branch 'develop' into patch-2
2 parents 9e3eb23 + 614051f commit 5abcbe2

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/Nats/Connection.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,19 @@ private function getStream($address, $timeout)
209209
$errno = null;
210210
$errstr = null;
211211

212+
set_error_handler(function(){return true;});
212213
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
213-
$timeout = number_format($timeout, 3);
214-
$seconds = floor($timeout);
215-
$microseconds = ($timeout - $seconds) * 1000;
216-
stream_set_timeout($fp, $seconds, $microseconds);
214+
restore_error_handler();
217215

218216
if (!$fp) {
219217
throw new \Exception($errstr, $errno);
220218
}
221219

220+
$timeout = number_format($timeout, 3);
221+
$seconds = floor($timeout);
222+
$microseconds = ($timeout - $seconds) * 1000;
223+
stream_set_timeout($fp, $seconds, $microseconds);
224+
222225
return $fp;
223226
}
224227

@@ -506,6 +509,9 @@ public function setChunkSize($chunkSize)
506509
*/
507510
public function close()
508511
{
512+
if ($this->streamSocket === null) {
513+
return;
514+
}
509515
fclose($this->streamSocket);
510516
$this->streamSocket = null;
511517
}

test/ConnectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,23 @@ function ($res) {
205205

206206
$this->assertTrue(true);
207207
}
208+
209+
210+
/**
211+
* Test Connecting when nats server is not available
212+
*
213+
* @return void
214+
*/
215+
public function testRefusedConnection()
216+
{
217+
$this->setExpectedException(\Exception::class);
218+
219+
$options = new ConnectionOptions();
220+
$options->setHost('localhost');
221+
$options->setPort(4223);
222+
223+
$c = new Nats\Connection($options);
224+
$c->connect();
225+
$this->assertFalse($this->c->isConnected());
226+
}
208227
}

0 commit comments

Comments
 (0)