Skip to content

Commit 4817565

Browse files
authored
Merge pull request repejota#73 from byrnedo/hotfix/remove-byte-trim
Removed incorrect trim of fread contents.
2 parents f2bdad6 + ce89039 commit 4817565

File tree

4 files changed

+78
-31
lines changed

4 files changed

+78
-31
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:
1313
./vendor/bin/phpunit test

src/Nats/Connection.php

Lines changed: 13 additions & 13 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,15 +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;
176-
}
177-
if (strlen($line) > 2) {
178-
$line = substr($line, 0, -2);
174+
$readChunk = fread($this->streamSocket, $chunkSize);
175+
$receivedBytes += strlen($readChunk);
176+
$line .= $readChunk;
179177
}
180178
} else {
181179
$line = fgets($this->streamSocket);
@@ -186,8 +184,8 @@ private function receive($len = null)
186184
/**
187185
* Returns an stream socket to the desired server.
188186
*
189-
* @param string $address Server url string.
190-
* @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.
191189
*
192190
* @return resource
193191
* @throws \Exception Exception raised if connection fails.
@@ -370,7 +368,7 @@ private function handlePING()
370368
* @param string $line Message command from Nats.
371369
*
372370
* @return void
373-
* @throws Exception
371+
* @throws Exception If subscription not found.
374372
* @codeCoverageIgnore
375373
*/
376374
private function handleMSG($line)
@@ -473,9 +471,11 @@ public function reconnect()
473471
}
474472

475473
/**
476-
* @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
477476
*/
478-
public function setChunkSize($chunkSize){
477+
public function setChunkSize($chunkSize)
478+
{
479479
$this->chunkSize = $chunkSize;
480480
}
481481

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)