Module: Karafka::Testing::Helpers

Defined in:
lib/karafka/testing/helpers.rb

Overview

Common helper methods that are shared in between RSpec and Minitest

Class Method Summary collapse

Class Method Details

.karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group) ⇒ Array<Karafka::Routing::Topic>

Note:

Since we run the lookup on subscription groups, the search will automatically expand with matching patterns

Finds all the routing topics matching requested topic within all topics or within provided consumer group based on name

Parameters:

  • requested_topic (String)

    requested topic name

  • requested_consumer_group (String)

    requested consumer group or nil to look in all

Returns:

  • (Array<Karafka::Routing::Topic>)

    all matching topics



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/karafka/testing/helpers.rb', line 17

def karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group)
  karafka_consumer_find_subscription_groups(requested_consumer_group)
    # multiplexed subscriptions of the same origin share name, thus we can reduced
    # multiplexing to the first one as during testing, there is no multiplexing parallel
    # execution anyhow
    .uniq(&:name)
    .map(&:topics)
    .filter_map do |topics|
      topics.find(requested_topic.to_s)
    rescue Karafka::Errors::TopicNotFoundError
      nil
    end
end

.karafka_consumer_find_subscription_groups(requested_consumer_group) ⇒ Array<Karafka::Routing::SubscriptionGroup>

Finds subscription groups from the requested consumer group or selects all if no consumer group specified

Parameters:

  • requested_consumer_group (String)

    requested consumer group or nil to look in all

Returns:

  • (Array<Karafka::Routing::SubscriptionGroup>)

    requested subscription groups



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/karafka/testing/helpers.rb', line 35

def karafka_consumer_find_subscription_groups(requested_consumer_group)
  if requested_consumer_group && !requested_consumer_group.empty?
    ::Karafka::App
      .subscription_groups
      # Find matching consumer group
      .find { |cg, _sgs| cg.name == requested_consumer_group.to_s }
      # Raise error if not found
      .tap { |cg| cg || raise(Errors::ConsumerGroupNotFound, requested_consumer_group) }
      # Since lookup was on a hash, get the value, that is subscription groups
      .last
  else
    ::Karafka::App
      .subscription_groups
      .values
      .flatten
  end
end