This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
forked from zendesk/ruby-kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fake_broker.rb
64 lines (54 loc) · 1.7 KB
/
fake_broker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
class FakeBroker
def initialize
@messages = {}
@partition_errors = {}
end
def messages
messages = []
@messages.each do |topic, messages_for_topic|
messages_for_topic.each do |partition, messages_for_partition|
messages_for_partition.each do |message|
messages << message
end
end
end
messages
end
def produce(messages_for_topics:, required_acks:, timeout:, transactional_id: nil, compressor: nil)
messages_for_topics.each do |topic, messages_for_topic|
messages_for_topic.each do |partition, record_batch|
@messages[topic] ||= {}
@messages[topic][partition] ||= []
@messages[topic][partition].concat(record_batch.records)
end
end
topics = messages_for_topics.map {|topic, messages_for_topic|
Kafka::Protocol::ProduceResponse::TopicInfo.new(
topic: topic,
partitions: messages_for_topic.map {|partition, record_batch|
Kafka::Protocol::ProduceResponse::PartitionInfo.new(
partition: partition,
error_code: error_code_for_partition(topic, partition),
offset: record_batch.records.size,
timestamp: (Time.now.to_f * 1000).to_i,
)
}
)
}
if required_acks != 0
Kafka::Protocol::ProduceResponse.new(topics: topics)
else
nil
end
end
def mark_partition_with_error(topic:, partition:, error_code:)
@partition_errors[topic] ||= Hash.new { 0 }
@partition_errors[topic][partition] = error_code
end
private
def error_code_for_partition(topic, partition)
@partition_errors[topic] ||= Hash.new { 0 }
@partition_errors[topic][partition]
end
end