Skip to content

Commit ebb356b

Browse files
committed
Merge pull request #1 from centraldesktop/bugfix/read-frame-from-buffer
Don't return false in readFrame if we have a buffered message.
2 parents 38c0213 + b07e47b commit ebb356b

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

src/CentralDesktop/Stomp/Connection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,12 @@ function setBufferSize($bytes) {
595595
*/
596596
public
597597
function readFrame() {
598-
if (!$this->hasFrameToRead()) {
598+
/**
599+
* If the buffer is empty, we might have a frame in the socket. Check
600+
* the buffer first because if we have a buffered message we don't
601+
* want to waste CPU on the socket read timeout.
602+
*/
603+
if (!$this->_bufferContainsMessage() && !$this->hasFrameToRead()) {
599604
return false;
600605
}
601606

test/src/CentralDesktop/Stomp/Test/BufferTest.php

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ class NullFactory implements Stomp\ConnectionFactory\FactoryI {
1313
/**
1414
* Gets the next URL to connect to
1515
*
16-
* @return
16+
* @return \InfiniteIterator
1717
*/
1818
public
1919
function getHostIterator() {
2020
return new \InfiniteIterator(new \ArrayIterator(array()));
2121
}
22+
23+
public
24+
function __toString() {
25+
return __CLASS__;
26+
}
2227
}
2328

2429

2530
class BufferTest extends \PHPUnit_Framework_TestCase {
31+
/**
32+
* @var Stomp\Connection
33+
*/
2634
private $stomp = null;
2735

2836
function setUp() {
@@ -112,5 +120,116 @@ function testExtractNextMessage() {
112120
$this->stomp->_appendToBuffer("");
113121
$this->assertEquals("", $this->stomp->_getBuffer());
114122
}
123+
124+
/**
125+
* @dataProvider read_frame_provider
126+
*
127+
* @param Stomp\Connection $stomp
128+
* @param array $expected_frames
129+
* @param $message
130+
*/
131+
public
132+
function testReadFrame(Stomp\Connection $stomp, array $expected_frames, $message) {
133+
foreach($expected_frames as $frame) {
134+
$this->assertEquals($frame, $stomp->readFrame(), $message);
135+
}
136+
}
137+
138+
public
139+
function read_frame_provider() {
140+
return array(
141+
$this->read_frame_empty_buffer_empty_socket(),
142+
$this->read_frame_single_message_buffer_empty_socket(),
143+
$this->read_frame_multi_message_buffer_empty_socket()
144+
);
145+
}
146+
147+
protected
148+
function read_frame_empty_buffer_empty_socket() {
149+
$stomp = $this->getMockBuilder('\CentralDesktop\Stomp\Connection')
150+
->setConstructorArgs(array(new NullFactory()))
151+
->setMethods(array('_bufferContainsMessage', 'hasFrameToRead'))
152+
->getMock();
153+
154+
$stomp->expects($this->once())
155+
->method('_bufferContainsMessage')
156+
->will($this->returnValue(false));
157+
158+
$stomp->expects($this->once())
159+
->method('hasFrameToRead')
160+
->will($this->returnValue(false));
161+
162+
return array(
163+
$stomp,
164+
array(
165+
false
166+
),
167+
"Expects no frame when buffer and socket are empty."
168+
);
169+
}
170+
171+
protected
172+
function read_frame_single_message_buffer_empty_socket() {
173+
$stomp = $this->getMockBuilder('\CentralDesktop\Stomp\Connection')
174+
->setConstructorArgs(array(new NullFactory()))
175+
->setMethods(array('hasFrameToRead'))
176+
->getMock();
177+
178+
$stomp->expects($this->once())
179+
->method('hasFrameToRead')
180+
->will($this->returnValue(false));
181+
182+
$stomp->_appendToBuffer("MESSAGE1\n\nBODY1\n\x00");
183+
184+
return array(
185+
$stomp,
186+
array(
187+
new Stomp\Frame(
188+
"MESSAGE1",
189+
array(),
190+
"BODY1"
191+
)
192+
),
193+
"Expected a frame for MESSAGE1."
194+
);
195+
}
196+
197+
protected
198+
function read_frame_multi_message_buffer_empty_socket() {
199+
$stomp = $this->getMockBuilder('\CentralDesktop\Stomp\Connection')
200+
->setConstructorArgs(array(new NullFactory()))
201+
->setMethods(array('hasFrameToRead'))
202+
->getMock();
203+
204+
$stomp->expects($this->once())
205+
->method('hasFrameToRead')
206+
->will($this->returnValue(false));
207+
208+
$stomp->_appendToBuffer("MESSAGE1\n\nBODY1\n\x00");
209+
$stomp->_appendToBuffer("MESSAGE2\n\nBODY2\n\x00");
210+
$stomp->_appendToBuffer("MESSAGE3\n\nBODY3\n\x00");
211+
212+
return array(
213+
$stomp,
214+
array(
215+
new Stomp\Frame(
216+
"MESSAGE1",
217+
array(),
218+
"BODY1"
219+
),
220+
new Stomp\Frame(
221+
"MESSAGE2",
222+
array(),
223+
"BODY2"
224+
),
225+
new Stomp\Frame(
226+
"MESSAGE3",
227+
array(),
228+
"BODY3"
229+
)
230+
),
231+
"Expected a frame for MESSAGE1, MESSAGE2 and MESSAGE3."
232+
);
233+
}
115234
}
116235

0 commit comments

Comments
 (0)