Created
May 5, 2023 11:14
-
-
Save id-ilych/55281afe1035f593adb23958a809cc66 to your computer and use it in GitHub Desktop.
Dummy client to use in specs for apps using waterdrop gem to produce kafka messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Spec producer client used to buffer messages that we send out in specs | |
class KafkaProducerClient < WaterDrop::Producer::DummyClient | |
attr_accessor :messages | |
# Sync fake response for the message delivery to Kafka, since we do not dispatch anything | |
class SyncResponse | |
# @param _args Handler wait arguments (irrelevant as waiting is fake here) | |
def wait(*_args) | |
false | |
end | |
end | |
def initialize | |
super() | |
@messages = [] | |
@topics = Hash.new { |k, v| k[v] = [] } | |
end | |
# "Produces" message to Kafka. That is, it acknowledges it locally, adds it to the internal buffer | |
# @param message [Hash] `WaterDrop::Producer#produce_sync` message hash | |
def produce(message) | |
topic = message.fetch(:topic) { raise ArgumentError, ':topic is missing' } | |
@topics[topic] << message | |
@messages << message | |
SyncResponse.new | |
end | |
# Returns messages produced to a given topic | |
# @param topic [String] | |
def messages_for_topic(topic) | |
@topics[topic] | |
end | |
# Clears internal buffer | |
# Used in between specs so messages do not leak out | |
def reset | |
@messages.clear | |
@topics.each_value(&:clear) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment