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

Commit cdc8b6b

Browse files
committed
Removed incorrect trim of fread contents
Also fixed trunctated read when read bytes are less than expected. Changed mtu to 1500 as this seems like the default mtu in most networks.
1 parent f2bdad6 commit cdc8b6b

3 files changed

Lines changed: 64 additions & 21 deletions

File tree

src/Nats/Connection.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Connection
2020
* Chunk size in bytes to use when reading with fread.
2121
* @var int
2222
*/
23-
private $chunkSize = 8192;
23+
private $chunkSize = 1500;
2424

2525
/**
2626
* Return the number of pings.
@@ -171,11 +171,9 @@ private function receive($len = null)
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);

test/ConnectionTest.php

Lines changed: 60 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,66 @@ 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+
121+
} while ($i < 100);
122+
}
123+
124+
/**
125+
* Test Request command with large payload.
126+
*
127+
* @return void
128+
*/
129+
public function testLargeRequest()
130+
{
131+
132+
$content = file_get_contents(dirname(__FILE__).'/test.pdf');
133+
134+
$contentLen = strlen($content);
135+
136+
$i = 0;
137+
do {
138+
139+
$this->c->subscribe(
140+
"saybighello$i",
141+
function ($res) use ($contentLen) {
142+
$gotLen = strlen($res->getBody());
143+
$this->assertEquals($contentLen, $gotLen);
144+
$res->reply($gotLen);
145+
}
146+
);
147+
148+
$this->c->request(
149+
"saybighello$i",
150+
$content,
151+
function ($message) use ($contentLen) {
152+
$this->assertNotNull($message);
153+
$this->assertEquals($message->getBody(), $contentLen);
154+
}
155+
);
156+
157+
$i++;
158+
} while ($i < 100);
159+
115160
}
116161

117162
/**

test/test.pdf

65.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)