Skip to content

Commit

Permalink
Add support to each_batch (#2)
Browse files Browse the repository at this point in the history
* Add support to each_batch

* Allow to set key on deliver_message

* require batch
  • Loading branch information
sarcilav authored Dec 7, 2018
1 parent 9552393 commit df824ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/fake/kafka.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'fake/kafka/version'
require 'fake/kafka/batch'
require 'fake/kafka/message'
require 'fake/kafka/consumer'
require 'fake/kafka/producer'
require 'fake/kafka/message'

module Fake
class Kafka
Expand All @@ -17,8 +18,8 @@ def paused?(topic, partition)
!!@paused_partitions[topic][partition]
end

def deliver_message(value, topic:)
@messages << Message.new(value, nil, topic, 0, 0)
def deliver_message(value, topic:, key: nil)
@messages << Message.new(value, key, topic, 0, 0)
end

def messages_in(topic)
Expand Down
10 changes: 10 additions & 0 deletions lib/fake/kafka/batch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Fake::Kafka::Batch
attr_reader :topic, :partition, :messages, :highwater_mark_offset

def initialize(topic:, partition:, messages:, highwater_mark_offset:)
@topic = topic
@partition = partition
@messages = messages
@highwater_mark_offset = highwater_mark_offset
end
end
14 changes: 14 additions & 0 deletions lib/fake/kafka/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def each_message(*options, &block)
end
end

def each_batch(*options, &block)
begin
batch = Fake::Kafka::Batch.new(
topic: @kafka.messages.first.topic,
partition: @kafka.messages.first.partition,
messages: @kafka.messages,
highwater_mark_offset: @kafka.messages.first.offset
)
block.call(batch)
rescue StandardError => e
raise Kafka::ProcessingError.new(batch.topic, batch.partition, batch.highwater_mark_offset)
end
end

def pause(topic, partition, timeout:, max_timeout: nil, exponential_backoff: false)
@kafka.paused_partitions[topic] ||= {}
@kafka.paused_partitions[topic][partition] = true
Expand Down

0 comments on commit df824ed

Please sign in to comment.