Skip to content

Commit

Permalink
Merge pull request #30 from jmoo/fix-for-blocking-ops
Browse files Browse the repository at this point in the history
Null out message buffer before calling onMessage handler
  • Loading branch information
cboden authored Mar 10, 2019
2 parents 0cb7c02 + 6d7d1b8 commit c62f7cd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Messaging/MessageBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ private function processData($data) {

if ($this->messageBuffer->isCoalesced()) {
$msgCheck = $this->checkMessage($this->messageBuffer);

$msgBuffer = $this->messageBuffer;
$this->messageBuffer = null;

if (true !== $msgCheck) {
$onControl($this->newCloseFrame($msgCheck, 'Ratchet detected an invalid UTF-8 payload'));
} else {
$onMessage($this->messageBuffer);
$onMessage($msgBuffer);
}

$this->messageBuffer = null;
}

return $overflow;
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/Messaging/MessageBufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Ratchet\RFC6455\Messaging\Frame;
use Ratchet\RFC6455\Messaging\Message;
use Ratchet\RFC6455\Messaging\MessageBuffer;
use React\EventLoop\Factory;

class MessageBufferTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -36,4 +37,36 @@ function (Message $message) use (&$messageCount) {

$this->assertEquals(1000, $messageCount);
}

public function testProcessingMessagesAsynchronouslyWhileBlockingInMessageHandler() {
$loop = Factory::create();

$frameA = new Frame('a', true, Frame::OP_TEXT);
$frameB = new Frame('b', true, Frame::OP_TEXT);

$bReceived = false;

$messageBuffer = new MessageBuffer(
new CloseFrameChecker(),
function (Message $message) use (&$messageCount, &$bReceived, $loop) {
$payload = $message->getPayload();
$bReceived = $payload === 'b';

if (!$bReceived) {
$loop->run();
}
},
null,
false
);

$loop->addPeriodicTimer(0.1, function () use ($messageBuffer, $frameB, $loop) {
$loop->stop();
$messageBuffer->onData($frameB->getContents());
});

$messageBuffer->onData($frameA->getContents());

$this->assertTrue($bReceived);
}
}

0 comments on commit c62f7cd

Please sign in to comment.