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

[1.7.x] Fix serialize #91

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/Producers/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function produce(KafkaProducerMessage $message): bool
{
$topic = $this->producer->newTopic($this->topic);

$message = clone $message;

$message = $this->serializer->serialize($message);

$this->produceMessage($topic, $message);
Expand All @@ -75,6 +77,8 @@ public function produceBatch(MessageBatch $messageBatch): int

$produced = 0;
foreach ($messagesIterator as $message) {
$message = clone $message;

$message = $this->serializer->serialize($message);

$this->produceMessage($topic, $message);
Expand Down
16 changes: 16 additions & 0 deletions tests/LaravelKafkaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use RdKafka\KafkaConsumer;
use RdKafka\Message;
use RdKafka\Producer as KafkaProducer;
use RdKafka\Conf;

class LaravelKafkaTestCase extends Orchestra
{
Expand Down Expand Up @@ -57,10 +58,25 @@ protected function mockProducer()
$this->app->bind(Producer::class, function () use ($mockedProducer) {
return $mockedProducer->getMock();
});
$this->mockKafkaProducer();
}

protected function mockKafkaProducer()
{
// We have to get a topic object as a valid response for the mock
// We stub out this code here to achieve that
$conf = new Conf();
$conf->set('log_level', 0);
$kafka = new KafkaProducer($conf);
$topic = $kafka->newTopic('test-topic');

$mockedKafkaProducer = m::mock(KafkaProducer::class)
->shouldReceive('flush')
->andReturn(RD_KAFKA_RESP_ERR_NO_ERROR)
->shouldReceive('newTopic')
->andReturn($topic)
->shouldReceive('poll')
->andReturn(RD_KAFKA_RESP_ERR_NO_ERROR)
->getMock();

$this->app->bind(KafkaProducer::class, function () use ($mockedKafkaProducer) {
Expand Down
47 changes: 47 additions & 0 deletions tests/Producers/ProducerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Junges\Kafka\Tests\Producers;

use Junges\Kafka\Config\Config;
use Junges\Kafka\Message\Serializers\JsonSerializer;
use Junges\Kafka\Producers\MessageBatch;
use Junges\Kafka\Producers\Producer;
use Junges\Kafka\Tests\LaravelKafkaTestCase;
use Junges\Kafka\Message\Message;

class ProducerTest extends LaravelKafkaTestCase
{
public function test_it_does_not_double_serialize_a_message_when_using_json_serializer()
{
$this->mockKafkaProducer();
$producer = new Producer(new Config('broker', ['test-topic']), 'test-topic', new JsonSerializer());
$payload = ['key' => 'value'];

$message = new Message(
body: $payload,
);
$producer->produce($message);
$producer->produce($message);

$this->assertSame($payload, $message->getBody());
}

public function test_it_does_not_double_serialize_a_batch_message_when_using_json_serializer()
{
$this->mockKafkaProducer();
$producer = new Producer(new Config('broker', ['test-topic']), 'test-topic', new JsonSerializer());
$payload = ['key' => 'value'];

$message = new Message(
body: $payload,
);

$messageBatch = new MessageBatch();
$messageBatch->push($message);

$producer->produceBatch($messageBatch);
$producer->produceBatch($messageBatch);

$this->assertSame($payload, $message->getBody());
}
}