Skip to content

[rdkafka] do not pass config if it was not set explisitly. #263

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

Merged
merged 1 commit into from
Nov 14, 2017
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
11 changes: 9 additions & 2 deletions pkg/rdkafka/RdKafkaTopic.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class RdKafkaTopic implements PsrTopic, PsrQueue
public function __construct($name)
{
$this->name = $name;
$this->conf = new TopicConf();
}

/**
Expand All @@ -54,13 +53,21 @@ public function getQueueName()
}

/**
* @return TopicConf
* @return TopicConf|null
*/
public function getConf()
{
return $this->conf;
}

/**
* @param TopicConf|null $conf
*/
public function setConf(TopicConf $conf = null)
{
$this->conf = $conf;
}

/**
* @return int
*/
Expand Down
70 changes: 69 additions & 1 deletion pkg/rdkafka/Tests/RdKafkaProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testShouldUseSerializerToEncodeMessageAndPutToExpectedTube()
$kafkaProducer
->expects($this->once())
->method('newTopic')
->with('theQueueName', $this->isInstanceOf(TopicConf::class))
->with('theQueueName')
->willReturn($kafkaTopic)
;

Expand All @@ -81,6 +81,74 @@ public function testShouldUseSerializerToEncodeMessageAndPutToExpectedTube()
$producer->send(new RdKafkaTopic('theQueueName'), $message);
}

public function testShouldPassNullAsTopicConfigIfNotSetOnTopic()
{
// guard
$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
;

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

$serializer = $this->createSerializerMock();
$serializer
->expects($this->once())
->method('toString')
->willReturn('aSerializedMessage')
;

$producer = new RdKafkaProducer($kafkaProducer, $serializer);

$topic = new RdKafkaTopic('theQueueName');

// guard
$this->assertNull($topic->getConf());

$producer->send($topic, new RdKafkaMessage());
}

public function testShouldPassCustomConfAsTopicConfigIfSetOnTopic()
{
$conf = new TopicConf();

// guard
$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
;

$kafkaProducer = $this->createKafkaProducerMock();
$kafkaProducer
->expects($this->once())
->method('newTopic')
->with('theQueueName', $this->identicalTo($conf))
->willReturn($kafkaTopic)
;

$serializer = $this->createSerializerMock();
$serializer
->expects($this->once())
->method('toString')
->willReturn('aSerializedMessage')
;

$producer = new RdKafkaProducer($kafkaProducer, $serializer);

$topic = new RdKafkaTopic('theQueueName');
$topic->setConf($conf);

$producer->send($topic, new RdKafkaMessage());
}

public function testShouldAllowGetPreviouslySetSerializer()
{
$producer = new RdKafkaProducer($this->createKafkaProducerMock(), $this->createSerializerMock());
Expand Down
14 changes: 12 additions & 2 deletions pkg/rdkafka/Tests/RdKafkaTopicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ public function testCouldSetGetKey()
$this->assertSame('key', $topic->getKey());
}

public function testShouldReturnConfInstance()
public function testShouldReturnNullAsConfIfNotSet()
{
$topic = new RdKafkaTopic('topic');

$this->assertInstanceOf(TopicConf::class, $topic->getConf());
$this->assertNull($topic->getConf());
}

public function testShouldAllowGetPreviouslySetConf()
{
$topic = new RdKafkaTopic('topic');

$conf = new TopicConf();
$topic->setConf($conf);

$this->assertSame($conf, $topic->getConf());
}
}