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

Implement RdKafka getAssignment Method #1376

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions pkg/rdkafka/RdKafkaConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Interop\Queue\Queue;
use RdKafka\KafkaConsumer;
use RdKafka\TopicPartition;
use RdKafka\Exception as RdKafkaException;

class RdKafkaConsumer implements Consumer
{
Expand Down Expand Up @@ -88,6 +89,22 @@ public function getQueue(): Queue
return $this->topic;
}

/**
* @return RdKafkaTopic[]
*/
public function getAssignment(): array
{
try {
return array_map(function (TopicPartition $partition) {
$topic = new RdKafkaTopic($partition->getTopic());
$topic->setPartition($partition->getPartition());
return $topic;
}, $this->consumer->getAssignment());
} catch (RdKafkaException) {
return [];
}
}

/**
* @return RdKafkaMessage
*/
Expand Down
66 changes: 66 additions & 0 deletions pkg/rdkafka/Tests/RdKafkaConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use PHPUnit\Framework\TestCase;
use RdKafka\KafkaConsumer;
use RdKafka\Message;
use RdKafka\TopicPartition;
use RdKafka\Exception as RdKafkaException;

class RdKafkaConsumerTest extends TestCase
{
Expand Down Expand Up @@ -258,6 +260,62 @@ public function testShouldAllowGetPreviouslySetSerializer()
$this->assertSame($expectedSerializer, $consumer->getSerializer());
}

public function testShouldGetAssignmentWhenThereAreNoPartitions(): void
{
$rdKafka = $this->createKafkaConsumerMock();
$rdKafka->expects($this->once())
->method('getAssignment')
->willReturn([]);

$consumer = new RdKafkaConsumer(
$rdKafka,
$this->createContextMock(),
new RdKafkaTopic(''),
$this->createSerializerMock()
);

$this->assertEquals([], $consumer->getAssignment());
}

public function testShouldGetAssignmentWhenThereArePartitions(): void
{
$partition = new TopicPartition('', 0);

$rdKafka = $this->createKafkaConsumerMock();
$rdKafka->expects($this->once())
->method('getAssignment')
->willReturn([$partition]);

$consumer = new RdKafkaConsumer(
$rdKafka,
$this->createContextMock(),
new RdKafkaTopic(''),
$this->createSerializerMock()
);

$expected = new RdKafkaTopic('');
$expected->setPartition(0);

$this->assertEquals([$expected], $consumer->getAssignment());
}

public function testShouldGetAssignmentReturnEmptyArrayWhenThrowException(): void
{
$rdKafka = $this->createKafkaConsumerMock();
$rdKafka->expects($this->once())
->method('getAssignment')
->willThrowException($this->createExceptionMock());

$consumer = new RdKafkaConsumer(
$rdKafka,
$this->createContextMock(),
new RdKafkaTopic(''),
$this->createSerializerMock()
);

$this->assertEquals([], $consumer->getAssignment());
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|KafkaConsumer
*/
Expand All @@ -281,4 +339,12 @@ private function createSerializerMock()
{
return $this->createMock(Serializer::class);
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|RdKafkaException
*/
private function createExceptionMock()
{
return $this->createMock(RdKafkaException::class);
}
}
Loading