Skip to content

Commit

Permalink
Merge pull request #254 from flix-tech/feature-enable-key-serialization
Browse files Browse the repository at this point in the history
[RdKafka] Enable serializers to serialize message keys
  • Loading branch information
makasim committed Nov 5, 2017
2 parents 53040ff + 58c7d4f commit 781eb0d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/rdkafka/RdKafkaProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function send(PsrDestination $destination, PsrMessage $message)
InvalidMessageException::assertMessageInstanceOf($message, RdKafkaMessage::class);

$partition = $message->getPartition() ?: $destination->getPartition() ?: RD_KAFKA_PARTITION_UA;
$key = $message->getKey() ?: $destination->getKey() ?: null;
$payload = $this->serializer->toString($message);
$key = $message->getKey() ?: $destination->getKey() ?: null;

$topic = $this->producer->newTopic($destination->getTopicName(), $destination->getConf());
$topic->produce($partition, 0 /* must be 0 */, $payload, $key);
Expand Down
39 changes: 39 additions & 0 deletions pkg/rdkafka/Tests/RdKafkaProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,45 @@ public function testShouldAllowGetPreviouslySetSerializer()
$this->assertSame($expectedSerializer, $producer->getSerializer());
}

public function testShouldAllowSerializersToSerializeKeys()
{
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
$message->setKey('key');

$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
->with(
RD_KAFKA_PARTITION_UA,
0,
'theSerializedMessage',
'theSerializedKey'
)
;

$kafkaProducer = $this->createKafkaProducerMock();
$kafkaProducer
->expects($this->once())
->method('newTopic')
->willReturn($kafkaTopic)
;

$serializer = $this->createSerializerMock();
$serializer
->expects($this->once())
->method('toString')
->willReturnCallback(function () use ($message) {
$message->setKey('theSerializedKey');

return 'theSerializedMessage';
})
;

$producer = new RdKafkaProducer($kafkaProducer, $serializer);
$producer->send(new RdKafkaTopic('theQueueName'), $message);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|ProducerTopic
*/
Expand Down

0 comments on commit 781eb0d

Please sign in to comment.