Skip to content

Commit 389b161

Browse files
committed
Merge branch 'master' into develop
2 parents bfc0f35 + 4817565 commit 389b161

File tree

4 files changed

+79
-29
lines changed

4 files changed

+79
-29
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ lint:
55
find examples -name *.php -exec php -l {} \;
66

77
cs: lint
8-
./bin/phpcbf --standard=PSR2 src tests examples
9-
./bin/phpcs --standard=PSR2 --warning-severity=0 src tests examples
10-
./bin/phpcs --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment src tests examples
8+
./vendor/bin/phpcbf --standard=PSR2 src test examples
9+
./vendor/bin/phpcs --standard=PSR2 --warning-severity=0 src test examples
10+
./vendor/bin/phpcs --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment src test examples
1111

1212
test: tdd bdd
1313

src/Nats/Connection.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class Connection
1818

1919
/**
2020
* Chunk size in bytes to use when reading with fread.
21-
* @var int
21+
* @var integer
2222
*/
23-
private $chunkSize = 8192;
23+
private $chunkSize = 1500;
2424

2525
/**
2626
* Return the number of pings.
@@ -167,12 +167,13 @@ private function receive($len = null)
167167
$receivedBytes = 0;
168168
while ($receivedBytes < $len) {
169169
$bytesLeft = $len - $receivedBytes;
170-
if ( $bytesLeft < $this->chunkSize ) {
170+
if ($bytesLeft < $this->chunkSize) {
171171
$chunkSize = $bytesLeft;
172172
}
173173

174-
$line .= fread($this->streamSocket, $chunkSize);
175-
$receivedBytes += $chunkSize;
174+
$readChunk = fread($this->streamSocket, $chunkSize);
175+
$receivedBytes += strlen($readChunk);
176+
$line .= $readChunk;
176177
}
177178
} else {
178179
$line = fgets($this->streamSocket);
@@ -183,8 +184,8 @@ private function receive($len = null)
183184
/**
184185
* Returns an stream socket to the desired server.
185186
*
186-
* @param string $address Server url string.
187-
* @param float $timeout Number of seconds until the connect() system call should timeout.
187+
* @param string $address Server url string.
188+
* @param float $timeout Number of seconds until the connect() system call should timeout.
188189
*
189190
* @return resource
190191
* @throws \Exception Exception raised if connection fails.
@@ -196,7 +197,7 @@ private function getStream($address, $timeout = null)
196197
}
197198
$errno = null;
198199
$errstr = null;
199-
200+
200201
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
201202
$timeout = number_format($timeout, 3);
202203
$seconds = floor($timeout);
@@ -367,7 +368,7 @@ private function handlePING()
367368
* @param string $line Message command from Nats.
368369
*
369370
* @return void
370-
* @throws Exception
371+
* @throws Exception If subscription not found.
371372
* @codeCoverageIgnore
372373
*/
373374
private function handleMSG($line)
@@ -470,9 +471,11 @@ public function reconnect()
470471
}
471472

472473
/**
473-
* @param integer $chunkSize Set byte chunk len to read when reading from wire
474+
* @param integer $chunkSize Set byte chunk len to read when reading from wire.
475+
* @return void
474476
*/
475-
public function setChunkSize($chunkSize){
477+
public function setChunkSize($chunkSize)
478+
{
476479
$this->chunkSize = $chunkSize;
477480
}
478481

test/ConnectionTest.php

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
1313
/**
1414
* Client.
1515
*
16-
* @var resource Client
16+
* @var Nats\Connection Client
1717
*/
1818
private $c;
1919

@@ -97,21 +97,68 @@ public function testReconnect()
9797
*/
9898
public function testRequest()
9999
{
100-
$this->c->subscribe(
101-
"sayhello",
102-
function ($res) {
103-
$res->reply("Hello, ".$res->getBody(). " !!!");
104-
}
105-
);
106100

107-
$this->c->request(
108-
'sayhello',
109-
'McFly',
110-
function ($message) {
111-
$this->assertNotNull($message);
112-
$this->assertEquals($message, 'Hello, McFly !!!');
113-
}
114-
);
101+
$i = 0;
102+
do {
103+
$this->c->subscribe(
104+
"sayhello$i",
105+
function ($res) {
106+
$res->reply("Hello, ".$res->getBody(). " !!!");
107+
}
108+
);
109+
110+
$this->c->request(
111+
"sayhello$i",
112+
'McFly',
113+
function ($message) {
114+
$this->assertNotNull($message);
115+
$this->assertEquals($message, 'Hello, McFly !!!');
116+
}
117+
);
118+
119+
$i++;
120+
} while ($i < 100);
121+
}
122+
123+
/**
124+
* Test Request command with large payload.
125+
*
126+
* @return void
127+
*/
128+
public function testLargeRequest()
129+
{
130+
131+
$content = file_get_contents(dirname(__FILE__).'/test.pdf');
132+
133+
$contentLen = strlen($content);
134+
135+
$contentSum = md5($content);
136+
137+
$i = 0;
138+
do {
139+
$this->c->subscribe(
140+
"saybighello$i",
141+
function ($res) use ($contentLen, $contentSum) {
142+
$gotLen = strlen($res->getBody());
143+
$gotSum = md5($res->getBody());
144+
$this->assertEquals($contentLen, $gotLen);
145+
$this->assertEquals($contentSum, $gotSum);
146+
$res->reply($gotLen);
147+
}
148+
);
149+
150+
$this->c->request(
151+
"saybighello$i",
152+
$content,
153+
function ($message) use ($contentLen) {
154+
$this->assertNotNull($message);
155+
$this->assertEquals($message->getBody(), $contentLen);
156+
}
157+
);
158+
159+
$i++;
160+
} while ($i < 100);
161+
115162
}
116163

117164
/**

test/test.pdf

65.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)