/** * append messages to a buffer in chunks * and make sure we properly determine if there is a message in a buffer * and properly read messages */ public function testConnectionReadChunks() { $connection = new Connection(new Stomp\ConnectionFactory\NullFactory()); $frame1 = new Stomp\Message\Map(array("title" => "lorem ipsum \n\n test end of line", "descripiton" => "test1\n\n")); $frame2 = new Stomp\Message\Map(array("title" => "lorem ipsum \n\n test end of line", "descripiton" => "test2\n\n")); $this->assertFalse($connection->_bufferContainsMessage(), 'Do not expect any messages in connection'); $connection->_appendToBuffer($frame1->__toString()); // brake second message apart so we can insert chunks of it in to buffer $frame2_msg = $frame2->__toString(); $frame2_part1 = substr($frame2_msg, 0, 50); $frame2_part2 = substr($frame2_msg, 50); // append just a chunk of a second message $connection->_appendToBuffer("\n\n"); $connection->_appendToBuffer($frame2_part1); $this->assertTrue($connection->_bufferContainsMessage(), 'Expect a message and a half'); //remove first message from buffer $message = $connection->readFrame(); $this->assertNotNull($message, "Expected a message"); $this->assertFalse($connection->_bufferContainsMessage(), "There is only half of message in a buffere"); // put second chank in to a buffer $connection->_appendToBuffer($frame2_part2); $this->assertTrue($connection->_bufferContainsMessage(), "We should have got a message in a buffer"); $message2 = $connection->readFrame(); $this->assertNotNull($message2, "Expected second frame here"); }
public function testExtractNextMessage() { // Make sure the buffer is empty to start $this->assertEquals('', $this->stomp->_getBuffer()); $this->assertFalse($this->stomp->_bufferContainsMessage()); // Fill the buffer up with 3 messages $this->stomp->_appendToBuffer("MESSAGE1\n\nBODY1\n"); $this->stomp->_appendToBuffer("MESSAGE2\n\nBODY2\n"); $this->stomp->_appendToBuffer("MESSAGE3\n\nBODY3\n"); $this->assertEquals("MESSAGE1\n\nBODY1\nMESSAGE2\n\nBODY2\nMESSAGE3\n\nBODY3\n", $this->stomp->_getBuffer()); $this->assertTrue($this->stomp->_bufferContainsMessage()); // Extract all 3 sequentially $this->assertEquals("MESSAGE1\n\nBODY1\n", $this->stomp->_extractNextMessage()); $this->assertEquals("MESSAGE2\n\nBODY2\nMESSAGE3\n\nBODY3\n", $this->stomp->_getBuffer()); $this->assertEquals("MESSAGE2\n\nBODY2\n", $this->stomp->_extractNextMessage()); $this->assertEquals("MESSAGE3\n\nBODY3\n", $this->stomp->_getBuffer()); $this->assertEquals("MESSAGE3\n\nBODY3\n", $this->stomp->_extractNextMessage()); $this->assertEquals("", $this->stomp->_getBuffer()); // Now that the buffer is empty, add another message $this->stomp->_appendToBuffer("MESSAGE4\n\nBODY4\n"); $this->assertEquals("MESSAGE4\n\nBODY4\n", $this->stomp->_getBuffer()); // Extract that message $this->assertEquals("MESSAGE4\n\nBODY4\n", $this->stomp->_extractNextMessage()); $this->assertEquals("", $this->stomp->_getBuffer()); // verify that trying to extract from an empty buffer returns empty string $this->stomp->_appendToBuffer(""); $this->assertEquals("", $this->stomp->_getBuffer()); // Now that the buffer is empty, add another message // This is to verify that extracting a message from an empty buffer doesn't break something $this->stomp->_appendToBuffer("MESSAGE5\n\nBODY5\n"); $this->assertEquals("MESSAGE5\n\nBODY5\n", $this->stomp->_getBuffer()); // Extract that message $this->assertEquals("MESSAGE5\n\nBODY5\n", $this->stomp->_extractNextMessage()); $this->assertEquals("", $this->stomp->_getBuffer()); // verify that trying to extract from an empty buffer returns empty string $this->stomp->_appendToBuffer(""); $this->assertEquals("", $this->stomp->_getBuffer()); }