Skip to content

Commit

Permalink
GH-37: Add topic prefix while encoding messages
Browse files Browse the repository at this point in the history
  • Loading branch information
angad-singh committed Sep 17, 2020
1 parent 49b0924 commit 78326eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/deimos/producer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def _process_message(message, topic)
nil
else
encoder.encode(message.payload,
topic: "#{config[:topic]}-value")
topic: "#{Deimos.config.producers.topic_prefix}#{config[:topic]}-value")
end
end

Expand All @@ -203,9 +203,9 @@ def _encode_key(key)
end

if config[:key_field]
encoder.encode_key(config[:key_field], key, topic: "#{config[:topic]}-key")
encoder.encode_key(config[:key_field], key, topic: "#{Deimos.config.producers.topic_prefix}#{config[:topic]}-key")
elsif config[:key_schema]
key_encoder.encode(key, topic: "#{config[:topic]}-key")
key_encoder.encode(key, topic: "#{Deimos.config.producers.topic_prefix}#{config[:topic]}-key")
else
key
end
Expand Down
16 changes: 16 additions & 0 deletions spec/producer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def self.partition_key(payload)
end

it 'should encode the key' do
Deimos.configure { |c| c.producers.topic_prefix = nil }
expect(MyProducer.encoder).to receive(:encode_key).with('test_id', 'foo', topic: 'my-topic-key')
expect(MyProducer.encoder).to receive(:encode_key).with('test_id', 'bar', topic: 'my-topic-key')
expect(MyProducer.encoder).to receive(:encode).with({
Expand All @@ -253,6 +254,21 @@ def self.partition_key(payload)
)
end

it 'should encode the key with topic prefix' do
Deimos.configure { |c| c.producers.topic_prefix = 'prefix.' }
expect(MyProducer.encoder).to receive(:encode_key).with('test_id', 'foo', topic: 'prefix.my-topic-key')
expect(MyProducer.encoder).to receive(:encode_key).with('test_id', 'bar', topic: 'prefix.my-topic-key')
expect(MyProducer.encoder).to receive(:encode).with({ 'test_id' => 'foo',
'some_int' => 123 },
{ topic: 'prefix.my-topic-value' })
expect(MyProducer.encoder).to receive(:encode).with({ 'test_id' => 'bar',
'some_int' => 124 },
{ topic: 'prefix.my-topic-value' })

MyProducer.publish_list([{ 'test_id' => 'foo', 'some_int' => 123 },
{ 'test_id' => 'bar', 'some_int' => 124 }])
end

it 'should not encode with plaintext key' do
expect(MyNonEncodedProducer.key_encoder).not_to receive(:encode_key)

Expand Down

0 comments on commit 78326eb

Please sign in to comment.