forked from confluentinc/bottledwater-pg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkafka_helpers.rb
38 lines (33 loc) · 919 Bytes
/
kafka_helpers.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
require 'kafka-consumer'
require 'timeout'
module KafkaHelpers
def kafka_take_messages(topic, expected, wait: 5, collect_partitions: false)
consumer = Kafka::Consumer.new(
'test',
[topic],
zookeeper: TEST_CLUSTER.zookeeper_hostport,
initial_offset: :earliest_offset,
logger: logger)
messages = []
partitions = Hash.new do |h, k|
h[k] = []
end
timeout(wait) do
consumer.each do |message|
messages << message
partitions[message.partition] << message
consumer.interrupt if messages.size >= expected
end
end
if collect_partitions
partitions
else
messages
end
rescue Timeout::Error
problem = messages.empty? ? "didn't see any" : "only saw #{messages.size}"
raise "expected #{expected} messages, but #{problem} after #{wait} seconds"
ensure
consumer.interrupt if consumer
end
end