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

Commit 93e114f

Browse files
committed
Merge branch 'release/0.8.0'
2 parents 4817565 + 077c139 commit 93e114f

8 files changed

Lines changed: 87 additions & 19 deletions

File tree

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ cs: lint
99
./vendor/bin/phpcs --standard=PSR2 --warning-severity=0 src test examples
1010
./vendor/bin/phpcs --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment src test examples
1111

12-
test:
12+
test: tdd bdd
13+
14+
tdd:
1315
./vendor/bin/phpunit test
1416

1517
bdd:
16-
./vendor/bin/phpspec run --format=pretty
18+
./vendor/bin/phpspec run --format=pretty -v
1719

1820
cover:
1921
./vendor/bin/phpunit --coverage-html ./cover test

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.2
1+
0.8.0

spec/Nats/ConnectionOptionsSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function it_has_default_lang_value_as_php() {
3333
}
3434

3535
function it_has_default_version_value_as_null() {
36-
$this->getVersion()->shouldEqual("0.0.5");
36+
$this->getVersion()->shouldEqual("0.8.0");
3737
}
3838

3939
function it_has_default_verbose_value_as_null() {

spec/Nats/ConnectionSpec.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function it_is_disconnected()
4141
$this->isConnected()->shouldBe(false);
4242
}
4343

44-
function it_can_connect_and_disconnect_with_default_options()
44+
function it_connects_and_disconnects_with_default_options()
4545
{
4646
$this->connect();
4747
$this->shouldHaveType('Nats\Connection');
@@ -59,7 +59,7 @@ function it_increases_reconnects_count_on_each_reconnection()
5959
$this->close();
6060
}
6161

62-
function it_a_ping_is_sent_after_a_successful_connection()
62+
function it_sends_ping_after_a_successful_connection()
6363
{
6464
$this->connect();
6565
$this->pingsCount()->shouldBe(1);
@@ -94,5 +94,30 @@ function it_decreases_subscriptions_after_unsubscribing_to_a_topic()
9494
$this->close();
9595
}
9696

97+
function it_sends_a_message_with_a_1024c_subject()
98+
{
99+
$this->connect();
100+
$subject = str_pad("", 1024*1, "x");
101+
$this->publish($subject);
102+
$this->pubsCount()->shouldBe(1);
103+
$this->close();
104+
}
105+
106+
function it_sends_a_message_with_a_1024x10c_subject()
107+
{
108+
$this->connect();
109+
$subject = str_pad("", 1024*10, "x");
110+
$this->publish($subject);
111+
$this->pubsCount()->shouldBe(1);
112+
$this->close();
113+
}
97114

115+
function it_sends_a_message_with_a_1024x100c_subject()
116+
{
117+
$this->connect();
118+
$subject = str_pad("", 1024*100, "x");
119+
$this->publish($subject);
120+
$this->pubsCount()->shouldBe(1);
121+
$this->close();
122+
}
98123
}

src/Nats/Connection.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,11 @@ private function receive($len = null)
190190
* @return resource
191191
* @throws \Exception Exception raised if connection fails.
192192
*/
193-
private function getStream($address, $timeout = null)
193+
private function getStream($address, $timeout)
194194
{
195-
if (is_null($timeout)) {
196-
$timeout = intval(ini_get('default_socket_timeout'));
197-
}
198195
$errno = null;
199196
$errstr = null;
200-
197+
201198
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
202199
$timeout = number_format($timeout, 3);
203200
$seconds = floor($timeout);
@@ -231,9 +228,13 @@ public function isConnected()
231228
*/
232229
public function connect($timeout = null)
233230
{
231+
if ($timeout === null) {
232+
$timeout = intval(ini_get('default_socket_timeout'));
233+
}
234234

235235
$this->timeout = $timeout;
236236
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
237+
$this->setStreamTimeout($timeout);
237238

238239
$msg = 'CONNECT '.$this->options;
239240
$this->send($msg);
@@ -413,7 +414,8 @@ private function handleMSG($line)
413414
public function wait($quantity = 0)
414415
{
415416
$count = 0;
416-
while (!feof($this->streamSocket)) {
417+
$info = stream_get_meta_data($this->streamSocket);
418+
while (is_resource($this->streamSocket) && !feof($this->streamSocket) && !$info['timed_out']) {
417419
$line = $this->receive();
418420

419421
if ($line === false) {
@@ -431,6 +433,7 @@ public function wait($quantity = 0)
431433
return $this;
432434
}
433435
}
436+
$info = stream_get_meta_data($this->streamSocket);
434437
}
435438
$this->close();
436439

@@ -447,9 +450,12 @@ public function wait($quantity = 0)
447450
public function setStreamTimeout($seconds)
448451
{
449452
if ($this->isConnected()) {
450-
if (is_int($seconds)) {
453+
if (is_numeric($seconds)) {
451454
try {
452-
return $this->streamWrapper->setStreamTimeout($this->streamSocket, $seconds);
455+
$timeout = number_format($seconds, 3);
456+
$seconds = floor($timeout);
457+
$microseconds = ($timeout - $seconds) * 1000;
458+
return stream_set_timeout($this->streamSocket, $seconds, $microseconds);
453459
} catch (\Exception $e) {
454460
return false;
455461
}
@@ -489,4 +495,13 @@ public function close()
489495
fclose($this->streamSocket);
490496
$this->streamSocket = null;
491497
}
498+
499+
500+
/**
501+
* @return resource
502+
*/
503+
public function streamSocket()
504+
{
505+
return $this->streamSocket;
506+
}
492507
}

src/Nats/ConnectionOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ConnectionOptions
4747
*
4848
* @var string
4949
*/
50-
private $version = "0.0.5";
50+
private $version = "0.8.0";
5151

5252
/**
5353
* If verbose mode is enabled.

test/ConnectionOptionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testSettersAndGettersWithoutCredentials()
7474
public function testStringRepresentation()
7575
{
7676
$options = new ConnectionOptions();
77-
$this->assertEquals("{\"lang\":\"php\",\"version\":\"0.0.5\",\"verbose\":false,\"pedantic\":false}", $options->__toString());
77+
$this->assertEquals("{\"lang\":\"php\",\"version\":\"0.8.0\",\"verbose\":false,\"pedantic\":false}", $options->__toString());
7878
}
7979

8080
/**
@@ -87,6 +87,6 @@ public function testStringRepresentationWithCredentials()
8787
$options = new ConnectionOptions();
8888
$options->setUser("username");
8989
$options->setPass("password");
90-
$this->assertEquals("{\"lang\":\"php\",\"version\":\"0.0.5\",\"verbose\":false,\"pedantic\":false,\"user\":\"username\",\"pass\":\"password\"}", $options->__toString());
90+
$this->assertEquals("{\"lang\":\"php\",\"version\":\"0.8.0\",\"verbose\":false,\"pedantic\":false,\"user\":\"username\",\"pass\":\"password\"}", $options->__toString());
9191
}
9292
}

test/ConnectionTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ function ($message) {
128128
public function testLargeRequest()
129129
{
130130

131-
$content = file_get_contents(dirname(__FILE__).'/test.pdf');
132-
131+
$content = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 51200);
132+
133133
$contentLen = strlen($content);
134134

135135
$contentSum = md5($content);
@@ -161,6 +161,32 @@ function ($message) use ($contentLen) {
161161

162162
}
163163

164+
/**
165+
* Test setting a timeout on the stream
166+
*
167+
* @return void
168+
*/
169+
public function testSetStreamTimeout()
170+
{
171+
$this->c->setStreamTimeout(1);
172+
$before = time();
173+
$this->c->request(
174+
"nonexistantsubject",
175+
"test",
176+
function ($message) {
177+
$this->fail("should never have gotten here");
178+
}
179+
);
180+
$timeTaken = time() - $before;
181+
182+
$this->assertGreaterThan(0, $timeTaken);
183+
$this->assertLessThan(3, $timeTaken);
184+
185+
$meta = stream_get_meta_data($this->c->streamSocket());
186+
187+
$this->assertTrue($meta["timed_out"]);
188+
}
189+
164190
/**
165191
* Test Unsubscribe command.
166192
*

0 commit comments

Comments
 (0)