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

Commit e22863d

Browse files
committed
Merge pull request repejota#32 from repejota/ISSUE-4
Issue 4
2 parents ae94c06 + 34ad0ce commit e22863d

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/Connection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ private function getStream($address, $timeout = null)
186186
}
187187
$errno = null;
188188
$errstr = null;
189-
//$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
190189

191190
$fp = $this->streamWrapper->getStreamSocketClient($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
192191

@@ -221,6 +220,17 @@ public function connect($timeout = null)
221220
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
222221
$msg = 'CONNECT '.$this->options;
223222
$this->send($msg);
223+
224+
$response = $this->receive();
225+
226+
$this->ping();
227+
$response = $this->receive();
228+
229+
if ($response !== "PONG") {
230+
if (strpos($response, '-ERR')!== false) {
231+
throw new \Exception("Failing connection: $response");
232+
}
233+
}
224234
}
225235

226236
/**

tests/Unit/ConnectionTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use Nats;
55
use Nats\ConnectionOptions;
66
use Nats\StreamWrapper;
7-
use org\bovigo\vfs\vfsStream;
8-
use org\bovigo\vfs\vfsStreamFile;
97
use Prophecy\Argument;
108

119
/**
@@ -43,20 +41,36 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
4341
public function setUp()
4442
{
4543
$options = new ConnectionOptions();
44+
$streamWrapper = $this->getMockStreamSocketClient();
4645

46+
$this->c = new Nats\Connection($options);
47+
$this->c->setStreamWrapper($streamWrapper->reveal());
48+
$this->c->connect();
49+
}
50+
51+
/**
52+
* Function for building Socket Mock.
53+
*
54+
* @return StreamWrapper
55+
*/
56+
private function getMockStreamSocketClient()
57+
{
4758
$streamWrapper = $this->prophesize("Nats\StreamWrapper");
59+
$streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
4860
$streamWrapper->getStreamSocketClient(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->will(function ($args) {
49-
return fopen("/tmp/".uniqid(), 'w');
61+
$fileName = "/tmp/".uniqid();
62+
$f = fopen($fileName, 'w');
63+
fwrite($f, "INFO: \n");
64+
fwrite($f, "-ERR \n");
5065

51-
});
66+
return $f;
5267

53-
$streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
68+
});
5469

55-
$this->c = new Nats\Connection($options);
56-
$this->c->setStreamWrapper($streamWrapper->reveal());
57-
$this->c->connect();
70+
return $streamWrapper;
5871
}
5972

73+
6074
/**
6175
* Test Connection.
6276
*
@@ -73,6 +87,7 @@ public function testConnection()
7387
$this->assertFalse($this->c->isConnected());
7488
}
7589

90+
7691
/**
7792
* Test Ping command.
7893
*

0 commit comments

Comments
 (0)