Module: Karafka::Testing::RSpec::Helpers
- Defined in:
- lib/karafka/testing/rspec/helpers.rb
Overview
RSpec helpers module that needs to be included
Class Method Summary collapse
-
.included(base) ⇒ Object
Adds all the needed extra functionalities to the rspec group.
Instance Method Summary collapse
-
#_karafka_add_message_to_consumer_if_needed(message) ⇒ Object
Adds a new Karafka message instance if needed with given payload and options into an internal consumer buffer that will be used to simulate messages delivery to the consumer.
-
#_karafka_consumer_for(requested_topic, requested_consumer_group = nil) ⇒ Object
Creates a consumer instance for a given topic.
-
#_karafka_produce(payload, metadata = {}) ⇒ Object
Produces message with a given payload to the consumer matching topic.
-
#_karafka_produced_messages ⇒ Array<Hash>
Messages that were produced.
Class Method Details
.included(base) ⇒ Object
Adds all the needed extra functionalities to the rspec group
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 |
# File 'lib/karafka/testing/rspec/helpers.rb', line 29 def included(base) # RSpec local reference to Karafka proxy that allows us to build the consumer instance base.let(:karafka) { Karafka::Testing::RSpec::Proxy.new(self) } # Messages that are targeted to the consumer # You can produce many messages from Karafka during specs and not all should go to the # consumer for processing. This buffer holds only those that should go to consumer base.let(:_karafka_consumer_messages) { [] } # Consumer fake client to mock communication with Kafka base.let(:_karafka_consumer_client) { Karafka::Testing::SpecConsumerClient.new } # Producer fake client to mock communication with Kafka base.let(:_karafka_producer_client) { Karafka::Testing::SpecProducerClient.new(self) } base.prepend_before do Karafka::Testing.ensure_karafka_initialized! .clear _karafka_producer_client.reset @_karafka_consumer_mappings = {} if Object.const_defined?('Mocha', false) Karafka.producer.stubs(:client).returns(_karafka_producer_client) else allow(Karafka.producer).to receive(:client).and_return(_karafka_producer_client) end end end |
Instance Method Details
#_karafka_add_message_to_consumer_if_needed(message) ⇒ Object
Adds a new Karafka message instance if needed with given payload and options into an internal consumer buffer that will be used to simulate messages delivery to the consumer
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/karafka/testing/rspec/helpers.rb', line 97 def () consumer_obj = if defined?(consumer) consumer else @_karafka_consumer_mappings&.dig([:topic]) end # Consumer needs to be defined in order to pass messages to it return unless consumer_obj # We're interested in adding message to consumer only when it is a Karafka consumer # Users may want to test other things (models producing messages for example) and in # their case consumer will not be a consumer return unless consumer_obj.is_a?(Karafka::BaseConsumer) # We target to the consumer only messages that were produced to it, since specs may also # produce other messages targeting other topics return unless [:topic] == consumer_obj.topic.name # Build message metadata and copy any metadata that would come from the message = (consumer_obj) .keys.each do |key| = METADATA_DISPATCH_MAPPINGS.fetch(key, key) next unless .key?() [key] = .fetch() end # Add this message to previously produced messages << Karafka::Messages::Message.new( [:payload], Karafka::Messages::Metadata.new() ) # Update batch metadata = Karafka::Messages::Builders::BatchMetadata.call( , consumer_obj.topic, 0, Time.now ) # Update consumer messages batch consumer_obj. = Karafka::Messages::Messages.new( , ) end |
#_karafka_consumer_for(requested_topic, requested_consumer_group = nil) ⇒ Object
Creates a consumer instance for a given topic
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/karafka/testing/rspec/helpers.rb', line 72 def _karafka_consumer_for(requested_topic, requested_consumer_group = nil) selected_topics = Testing::Helpers.karafka_consumer_find_candidate_topics( requested_topic.to_s, requested_consumer_group.to_s ) raise Errors::TopicInManyConsumerGroupsError, requested_topic if selected_topics.size > 1 raise Errors::TopicNotFoundError, requested_topic if selected_topics.empty? _karafka_build_consumer_for(selected_topics.first) end |
#_karafka_produce(payload, metadata = {}) ⇒ Object
Produces message with a given payload to the consumer matching topic
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/karafka/testing/rspec/helpers.rb', line 148 def _karafka_produce(payload, = {}) topic = if [:topic] [:topic] elsif defined?(consumer) consumer.topic.name else @_karafka_consumer_mappings&.keys&.last end Karafka.producer.produce_sync( { topic: topic, payload: payload }.merge() ) end |
#_karafka_produced_messages ⇒ Array<Hash>
Returns messages that were produced.
165 166 167 |
# File 'lib/karafka/testing/rspec/helpers.rb', line 165 def _karafka_producer_client. end |