/** * Read the next message. * Override the parent method: we don't want to increment the byte offset * * @return string Message (raw) * @throws Kafka_Exception when the message cannot be read from the stream buffer */ protected function getMessage() { $msg = parent::getMessage(); // do not increment the offset for internal iterators $this->validByteCount = 0; return $msg; }
public function testMixedMessages() { $stream = fopen('php://temp', 'w+b'); $messages = array('message #1', 'message #2', 'message #3'); $this->writeDummyCompressedMessageSet($stream, $messages, Kafka_Encoder::COMPRESSION_GZIP); $messages2 = array('message #4', 'message #5', 'message #6'); $this->writeDummyMessageSet($stream, $messages2, Kafka_Encoder::COMPRESSION_NONE); $this->writeDummyCompressedMessageSet($stream, $messages, Kafka_Encoder::COMPRESSION_GZIP); rewind($stream); $allMessages = $messages; foreach ($messages2 as $msg) { $allMessages[] = $msg; } foreach ($messages as $msg) { $allMessages[] = $msg; } $socket = Kafka_Socket::createFromStream($stream); $set = new Kafka_MessageSet($socket, 0, 0); $idx = 0; foreach ($set as $offset => $msg) { $this->assertEquals($allMessages[$idx++], $msg->payload()); } $this->assertEquals(count($allMessages), $idx); // test new offset $readBytes = $set->validBytes(); $this->assertEquals(198, $readBytes); // no more data $set = new Kafka_MessageSet($socket, $readBytes, 0); $cnt = 0; foreach ($set as $offset => $msg) { $cnt++; } $this->assertEquals(0, $cnt); fclose($stream); }