Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow either subscribe or assign in RdKafkaConsumer #508

Merged
merged 2 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions pkg/rdkafka/RdKafkaConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function __construct(KafkaConsumer $consumer, RdKafkaContext $context, Rd
$this->topic = $topic;
$this->subscribed = false;
$this->commitAsync = false;
$this->offset = null;

$this->setSerializer($serializer);
}
Expand Down Expand Up @@ -98,13 +97,16 @@ public function getQueue()
*/
public function receive($timeout = 0)
{
if (false == $this->subscribed) {
$this->consumer->assign([new TopicPartition(
$this->getQueue()->getQueueName(),
$this->getQueue()->getPartition(),
$this->offset
)]);

if (false === $this->subscribed) {
if (null === $this->offset) {
$this->consumer->subscribe([$this->getQueue()->getQueueName()]);
} else {
$this->consumer->assign([new TopicPartition(
$this->getQueue()->getQueueName(),
$this->getQueue()->getPartition(),
$this->offset
)]);
}
$this->subscribed = true;
}

Expand Down
40 changes: 36 additions & 4 deletions pkg/rdkafka/Tests/RdKafkaConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testShouldReceiveFromQueueAndReturnNullIfNoMessageInQueue()
$kafkaConsumer = $this->createKafkaConsumerMock();
$kafkaConsumer
->expects($this->once())
->method('assign')
->method('subscribe')
;
$kafkaConsumer
->expects($this->once())
Expand Down Expand Up @@ -79,7 +79,7 @@ public function testShouldPassProperlyConfiguredTopicPartitionOnAssign()
$kafkaConsumer = $this->createKafkaConsumerMock();
$kafkaConsumer
->expects($this->once())
->method('assign')
->method('subscribe')
;
$kafkaConsumer
->expects($this->any())
Expand All @@ -106,6 +106,36 @@ public function testShouldSubscribeOnFirstReceiveOnly()
$kafkaMessage = new Message();
$kafkaMessage->err = RD_KAFKA_RESP_ERR__TIMED_OUT;

$kafkaConsumer = $this->createKafkaConsumerMock();
$kafkaConsumer
->expects($this->once())
->method('subscribe')
;
$kafkaConsumer
->expects($this->any())
->method('consume')
->willReturn($kafkaMessage)
;

$consumer = new RdKafkaConsumer(
$kafkaConsumer,
$this->createContextMock(),
$destination,
$this->createSerializerMock()
);

$consumer->receive(1000);
$consumer->receive(1000);
$consumer->receive(1000);
}

public function testShouldAssignWhenOffsetIsSet()
{
$destination = new RdKafkaTopic('dest');

$kafkaMessage = new Message();
$kafkaMessage->err = RD_KAFKA_RESP_ERR__TIMED_OUT;

$kafkaConsumer = $this->createKafkaConsumerMock();
$kafkaConsumer
->expects($this->once())
Expand All @@ -124,6 +154,8 @@ public function testShouldSubscribeOnFirstReceiveOnly()
$this->createSerializerMock()
);

$consumer->setOffset(123);

$consumer->receive(1000);
$consumer->receive(1000);
$consumer->receive(1000);
Expand All @@ -139,7 +171,7 @@ public function testThrowOnOffsetChangeAfterSubscribing()
$kafkaConsumer = $this->createKafkaConsumerMock();
$kafkaConsumer
->expects($this->once())
->method('assign')
->method('subscribe')
;
$kafkaConsumer
->expects($this->any())
Expand Down Expand Up @@ -174,7 +206,7 @@ public function testShouldReceiveFromQueueAndReturnMessageIfMessageInQueue()
$kafkaConsumer = $this->createKafkaConsumerMock();
$kafkaConsumer
->expects($this->once())
->method('assign')
->method('subscribe')
;
$kafkaConsumer
->expects($this->once())
Expand Down