karafka-testing 2.2.0 → 2.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84ae796af17d02ace2ed5130aab0d964ea724bcd5b8379f954b3b93d265afbf2
4
- data.tar.gz: 938a1f00fa6b1670d6cf8c6a045426c17ad9964398e6f3e16785ee927f21be93
3
+ metadata.gz: a79a60538d27c37844e17b112d7640e9ee9d975c8537bb2b78d8b8399faf656b
4
+ data.tar.gz: bcd86d7094908a5ac1e261651722f387c4188772890da390cadd19ed874aef04
5
5
  SHA512:
6
- metadata.gz: 7229353c31414e28d97b0d498ec0a92d628432d8544b3a34811d518681607573ab2734c75ea936cb25666e1f12dbcce7c6de8fda2e39e2ce7f5f1c394759ec2a
7
- data.tar.gz: 0a5b5db670109114c6fbb77e6da81fde0215dbe2c2b38dc95cc063654cc349d836fde8dd09a18fa76a52a50c0560529c4ddd0971b5e4da1ae37a50845e103c08
6
+ metadata.gz: abc39e5c005227a77ba7145e263c93e10c8c3e0406cdccf66cc3406c72d479436f8b3f0a1f665d5f793c3b416760d87beb02f25f319594e1bbc9d9093537a7da
7
+ data.tar.gz: 91c4255aa2897199498879a7e400b6609204d322c39307ba131449752d9b6b562fca70d37513170e1c6563ee351018a7c8bb11ec9e833e426020cb2bec011e67
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,6 +1,8 @@
1
1
  name: ci
2
2
 
3
- concurrency: ci-${{ github.ref }}
3
+ concurrency:
4
+ group: ${{ github.workflow }}-${{ github.ref }}
5
+ cancel-in-progress: true
4
6
 
5
7
  on:
6
8
  pull_request:
@@ -16,6 +18,7 @@ jobs:
16
18
  fail-fast: false
17
19
  matrix:
18
20
  ruby:
21
+ - '3.3.0-preview2'
19
22
  - '3.2'
20
23
  - '3.1'
21
24
  - '3.0'
@@ -24,7 +27,7 @@ jobs:
24
27
  - ruby: '3.2'
25
28
  coverage: 'true'
26
29
  steps:
27
- - uses: actions/checkout@v3
30
+ - uses: actions/checkout@v4
28
31
  - name: Install package dependencies
29
32
  run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"
30
33
  - name: Set up Ruby
@@ -43,7 +46,7 @@ jobs:
43
46
  strategy:
44
47
  fail-fast: false
45
48
  steps:
46
- - uses: actions/checkout@v3
49
+ - uses: actions/checkout@v4
47
50
  with:
48
51
  fetch-depth: 0
49
52
  - name: Set up Ruby
@@ -62,7 +65,7 @@ jobs:
62
65
  strategy:
63
66
  fail-fast: false
64
67
  steps:
65
- - uses: actions/checkout@v3
68
+ - uses: actions/checkout@v4
66
69
  with:
67
70
  fetch-depth: 0
68
71
  - name: Run Coditsu
data/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # Karafka Test gem changelog
2
2
 
3
- ## 2.2.0 (2023-09-91)
3
+ ## 2.2.2 (2023-11-16)
4
+ - [**Feature**] Provide support for Minitest (ValentinoRusconi-EH)
5
+
6
+ ## 2.2.1 (2023-10-26)
7
+ - [Enhancement] Support patterns in `#consumer_for` consumer builder.
8
+
9
+ ## 2.2.0 (2023-09-01)
4
10
  - [Maintenance] Ensure that `2.2.0` works with consumers for patterns.
5
11
  - [Maintenance] Replace signing key with a new one (old expired).
6
12
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- karafka-testing (2.2.0)
4
+ karafka-testing (2.2.2)
5
5
  karafka (>= 2.2.0, < 3.0.0)
6
6
  waterdrop (>= 2.6.6)
7
7
 
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.metadata = {
31
31
  'funding_uri' => 'https://karafka.io/#become-pro',
32
32
  'homepage_uri' => 'https://karafka.io',
33
- 'changelog_uri' => 'https://github.com/karafka/karafka-testing/blob/master/CHANGELOG.md',
33
+ 'changelog_uri' => 'https://karafka.io/docs/Changelog-Karafka-Testing',
34
34
  'bug_tracker_uri' => 'https://github.com/karafka/karafka-testing/issues',
35
35
  'source_code_uri' => 'https://github.com/karafka/karafka-testing',
36
36
  'documentation_uri' => 'https://karafka.io/docs',
@@ -13,6 +13,9 @@ module Karafka
13
13
 
14
14
  # Raised when topic is in many consumer groups and not limited by consumer group expectation
15
15
  TopicInManyConsumerGroupsError = Class.new(BaseError)
16
+
17
+ # Raised when you requested a topic from a consumer group that does not exist
18
+ ConsumerGroupNotFound = Class.new(BaseError)
16
19
  end
17
20
  end
18
21
  end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'karafka/testing/errors'
4
+ require 'karafka/testing/spec_consumer_client'
5
+ require 'karafka/testing/spec_producer_client'
6
+ require 'karafka/testing/minitest/proxy'
7
+
8
+ module Karafka
9
+ module Testing
10
+ # All the things related to extra functionalities needed to easier spec out
11
+ # Karafka things using Minitest
12
+ module Minitest
13
+ # Minitest helpers module that needs to be included
14
+ module Helpers
15
+ class << self
16
+ # Adds all the needed extra functionalities to the minitest group
17
+ # @param base [Class] Minitest example group we want to extend
18
+ def included(base)
19
+ base.class_eval do
20
+ setup do
21
+ @karafka = Karafka::Testing::Minitest::Proxy.new(self)
22
+ @_karafka_consumer_messages = []
23
+ @_karafka_consumer_client = Karafka::Testing::SpecConsumerClient.new
24
+ @_karafka_producer_client = Karafka::Testing::SpecProducerClient.new(self)
25
+
26
+ @_karafka_consumer_messages.clear
27
+ @_karafka_producer_client.reset
28
+
29
+ Karafka.producer.stubs(:client).returns(@_karafka_producer_client)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # Creates a consumer instance for a given topic
36
+ #
37
+ # @param requested_topic [String, Symbol] name of the topic for which we want to
38
+ # create a consumer instance
39
+ # @param requested_consumer_group [String, Symbol, nil] optional name of the consumer group
40
+ # if we have multiple consumer groups listening on the same topic
41
+ # @return [Object] Karafka consumer instance
42
+ # @raise [Karafka::Testing::Errors::TopicNotFoundError] raised when we're unable to find
43
+ # topic that was requested
44
+ #
45
+ # @example Creates a consumer instance with settings for `my_requested_topic`
46
+ # consumer = @karafka.consumer_for(:my_requested_topic)
47
+ def _karafka_consumer_for(requested_topic, requested_consumer_group = nil)
48
+ selected_topics = _karafka_consumer_find_candidate_topics(
49
+ requested_topic.to_s,
50
+ requested_consumer_group.to_s
51
+ )
52
+
53
+ raise Errors::TopicInManyConsumerGroupsError, requested_topic if selected_topics.size > 1
54
+ raise Errors::TopicNotFoundError, requested_topic if selected_topics.empty?
55
+
56
+ _karafka_build_consumer_for(selected_topics.first)
57
+ end
58
+
59
+ # Adds a new Karafka message instance if needed with given payload and options into an
60
+ # internal consumer buffer that will be used to simulate messages delivery to the consumer
61
+ #
62
+ # @param message [Hash] message that was sent to Kafka
63
+ #
64
+ # @example Send a json message to consumer
65
+ # @karafka.produce({ 'hello' => 'world' }.to_json)
66
+ #
67
+ # @example Send a json message to consumer and simulate, that it is partition 6
68
+ # @karafka.produce({ 'hello' => 'world' }.to_json, 'partition' => 6)
69
+ def _karafka_add_message_to_consumer_if_needed(message)
70
+ # Consumer needs to be defined in order to pass messages to it
71
+ return unless defined?(@consumer)
72
+ # We're interested in adding message to consumer only when it is a Karafka consumer
73
+ # Users may want to test other things (models producing messages for example) and in
74
+ # their case consumer will not be a consumer
75
+ return unless @consumer.is_a?(Karafka::BaseConsumer)
76
+ # We target to the consumer only messages that were produced to it, since specs may also
77
+ # produce other messages targeting other topics
78
+ return unless message[:topic] == @consumer.topic.name
79
+
80
+ # Build message metadata and copy any metadata that would come from the message
81
+ metadata = _karafka_message_metadata_defaults
82
+
83
+ metadata.keys.each do |key|
84
+ next unless message.key?(key)
85
+
86
+ metadata[key] = message.fetch(key)
87
+ end
88
+ # Add this message to previously produced messages
89
+ @_karafka_consumer_messages << Karafka::Messages::Message.new(
90
+ message[:payload],
91
+ Karafka::Messages::Metadata.new(metadata).freeze
92
+ )
93
+ # Update batch metadata
94
+ batch_metadata = Karafka::Messages::Builders::BatchMetadata.call(
95
+ @_karafka_consumer_messages,
96
+ @consumer.topic,
97
+ 0,
98
+ Time.now
99
+ )
100
+
101
+ # Update consumer messages batch
102
+ @consumer.messages = Karafka::Messages::Messages.new(
103
+ @_karafka_consumer_messages,
104
+ batch_metadata
105
+ )
106
+ end
107
+
108
+ # Produces message with a given payload to the consumer matching topic
109
+ # @param payload [String] payload we want to dispatch
110
+ # @param metadata [Hash] any metadata we want to dispatch alongside the payload
111
+ def _karafka_produce(payload, metadata = {})
112
+ Karafka.producer.produce_sync(
113
+ {
114
+ topic: @consumer.topic.name,
115
+ payload: payload
116
+ }.merge(metadata)
117
+ )
118
+ end
119
+
120
+ # @return [Array<Hash>] messages that were produced
121
+ def _karafka_produced_messages
122
+ @_karafka_producer_client.messages
123
+ end
124
+
125
+ private
126
+
127
+ # @return [Hash] message default options
128
+ def _karafka_message_metadata_defaults
129
+ {
130
+ deserializer: @consumer.topic.deserializer,
131
+ timestamp: Time.now,
132
+ headers: {},
133
+ key: nil,
134
+ offset: @_karafka_consumer_messages.size,
135
+ partition: 0,
136
+ received_at: Time.now,
137
+ topic: @consumer.topic.name
138
+ }
139
+ end
140
+
141
+ # Builds the consumer instance based on the provided topic
142
+ #
143
+ # @param topic [Karafka::Routing::Topic] topic for which we want to build the consumer
144
+ # @return [Object] karafka consumer
145
+ def _karafka_build_consumer_for(topic)
146
+ coordinators = Karafka::Processing::CoordinatorsBuffer.new(
147
+ Karafka::Routing::Topics.new([topic])
148
+ )
149
+ @consumer = topic.consumer.new
150
+ @consumer.producer = Karafka::App.producer
151
+ # Inject appropriate strategy so needed options and components are available
152
+ strategy = Karafka::App.config.internal.processing.strategy_selector.find(topic)
153
+ @consumer.singleton_class.include(strategy)
154
+ @consumer.client = @karafka_consumer_client
155
+ @consumer.coordinator = coordinators.find_or_create(topic.name, 0)
156
+ @consumer.coordinator.seek_offset = 0
157
+ # Indicate usage as for tests no direct enqueuing happens
158
+ @consumer.instance_variable_set('@used', true)
159
+ @consumer
160
+ end
161
+
162
+ # Finds all the routing topics matching requested topic within all topics or within
163
+ # provided consumer group based on name
164
+ #
165
+ # @param requested_topic [String] requested topic name
166
+ # @param requested_consumer_group [String] requested consumer group or nil to look in all
167
+ # @return [Array<Karafka::Routing::Topic>] all matching topics
168
+ #
169
+ # @note Since we run the lookup on subscription groups, the search will automatically
170
+ # expand with matching patterns
171
+ def _karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group)
172
+ _karafka_consumer_find_subscription_groups(requested_consumer_group)
173
+ .map(&:topics)
174
+ .filter_map do |topics|
175
+ topics.find(requested_topic.to_s)
176
+ rescue Karafka::Errors::TopicNotFoundError
177
+ nil
178
+ end
179
+ end
180
+
181
+ # Finds subscription groups from the requested consumer group or selects all if no
182
+ # consumer group specified
183
+ # @param requested_consumer_group [String] requested consumer group or nil to look in all
184
+ # @return [Array<Karafka::Routing::SubscriptionGroup>] requested subscription groups
185
+ def _karafka_consumer_find_subscription_groups(requested_consumer_group)
186
+ if requested_consumer_group && !requested_consumer_group.empty?
187
+ ::Karafka::App
188
+ .subscription_groups
189
+ # Find matching consumer group
190
+ .find { |cg, _sgs| cg.name == requested_consumer_group.to_s }
191
+ # Raise error if not found
192
+ .tap { |cg| cg || raise(Errors::ConsumerGroupNotFound, requested_consumer_group) }
193
+ # Since lookup was on a hash, get the value, that is subscription groups
194
+ .last
195
+ else
196
+ ::Karafka::App
197
+ .subscription_groups
198
+ .values
199
+ .flatten
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Testing
5
+ module Minitest
6
+ # Proxy object for a nicer `karafka.` API within Minitest
7
+ # None other should be used by the end users
8
+ class Proxy
9
+ # @param minitest_example [Minitest::Test] minitest context
10
+ def initialize(minitest_example)
11
+ @minitest_example = minitest_example
12
+ end
13
+
14
+ # @param args Anything that the `#_karafka_consumer_for` accepts
15
+ def consumer_for(*args)
16
+ @minitest_example._karafka_consumer_for(*args)
17
+ end
18
+
19
+ # @param args Anything that `#_karafka_produce` accepts
20
+ def produce(*args)
21
+ @minitest_example._karafka_produce(*args)
22
+ end
23
+
24
+ # @return [Array<Hash>] messages produced via `Karafka#producer`
25
+ def produced_messages
26
+ @minitest_example._karafka_produced_messages
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -57,17 +57,10 @@ module Karafka
57
57
  # subject(:consumer) { karafka.consumer_for(:my_requested_topic) }
58
58
  # end
59
59
  def _karafka_consumer_for(requested_topic, requested_consumer_group = nil)
60
- all_topics = ::Karafka::App.consumer_groups.map(&:topics).flat_map(&:to_a)
61
-
62
- # First select topics that match what we are looking for
63
- selected_topics = all_topics.select do |topic|
64
- topic.name == requested_topic.to_s
65
- end
66
-
67
- # And then narrow it down based on the consumer group criteria (if present)
68
- selected_topics.delete_if do |topic|
69
- requested_consumer_group && topic.consumer_group.name != requested_consumer_group.to_s
70
- end
60
+ selected_topics = _karafka_consumer_find_candidate_topics(
61
+ requested_topic.to_s,
62
+ requested_consumer_group.to_s
63
+ )
71
64
 
72
65
  raise Errors::TopicInManyConsumerGroupsError, requested_topic if selected_topics.size > 1
73
66
  raise Errors::TopicNotFoundError, requested_topic if selected_topics.empty?
@@ -183,6 +176,47 @@ module Karafka
183
176
  consumer.instance_variable_set('@used', true)
184
177
  consumer
185
178
  end
179
+
180
+ # Finds all the routing topics matching requested topic within all topics or within
181
+ # provided consumer group based on name
182
+ #
183
+ # @param requested_topic [String] requested topic name
184
+ # @param requested_consumer_group [String] requested consumer group or nil to look in all
185
+ # @return [Array<Karafka::Routing::Topic>] all matching topics
186
+ #
187
+ # @note Since we run the lookup on subscription groups, the search will automatically
188
+ # expand with matching patterns
189
+ def _karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group)
190
+ _karafka_consumer_find_subscription_groups(requested_consumer_group)
191
+ .map(&:topics)
192
+ .filter_map do |topics|
193
+ topics.find(requested_topic.to_s)
194
+ rescue Karafka::Errors::TopicNotFoundError
195
+ nil
196
+ end
197
+ end
198
+
199
+ # Finds subscription groups from the requested consumer group or selects all if no
200
+ # consumer group specified
201
+ # @param requested_consumer_group [String] requested consumer group or nil to look in all
202
+ # @return [Array<Karafka::Routing::SubscriptionGroup>] requested subscription groups
203
+ def _karafka_consumer_find_subscription_groups(requested_consumer_group)
204
+ if requested_consumer_group && !requested_consumer_group.empty?
205
+ ::Karafka::App
206
+ .subscription_groups
207
+ # Find matching consumer group
208
+ .find { |cg, _sgs| cg.name == requested_consumer_group.to_s }
209
+ # Raise error if not found
210
+ .tap { |cg| cg || raise(Errors::ConsumerGroupNotFound, requested_consumer_group) }
211
+ # Since lookup was on a hash, get the value, that is subscription groups
212
+ .last
213
+ else
214
+ ::Karafka::App
215
+ .subscription_groups
216
+ .values
217
+ .flatten
218
+ end
219
+ end
186
220
  end
187
221
  end
188
222
  end
@@ -4,6 +4,6 @@
4
4
  module Karafka
5
5
  module Testing
6
6
  # Current version of gem. It should match Karafka framework version
7
- VERSION = '2.2.0'
7
+ VERSION = '2.2.2'
8
8
  end
9
9
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karafka-testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Mensfeld
@@ -35,7 +35,7 @@ cert_chain:
35
35
  AnG1dJU+yL2BK7vaVytLTstJME5mepSZ46qqIJXMuWob/YPDmVaBF39TDSG9e34s
36
36
  msG3BiCqgOgHAnL23+CN3Rt8MsuRfEtoTKpJVcCfoEoNHOkc
37
37
  -----END CERTIFICATE-----
38
- date: 2023-09-01 00:00:00.000000000 Z
38
+ date: 2023-11-16 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: karafka
@@ -97,6 +97,8 @@ files:
97
97
  - lib/karafka-testing.rb
98
98
  - lib/karafka/testing.rb
99
99
  - lib/karafka/testing/errors.rb
100
+ - lib/karafka/testing/minitest/helpers.rb
101
+ - lib/karafka/testing/minitest/proxy.rb
100
102
  - lib/karafka/testing/rspec/helpers.rb
101
103
  - lib/karafka/testing/rspec/proxy.rb
102
104
  - lib/karafka/testing/spec_consumer_client.rb
@@ -109,7 +111,7 @@ licenses:
109
111
  metadata:
110
112
  funding_uri: https://karafka.io/#become-pro
111
113
  homepage_uri: https://karafka.io
112
- changelog_uri: https://github.com/karafka/karafka-testing/blob/master/CHANGELOG.md
114
+ changelog_uri: https://karafka.io/docs/Changelog-Karafka-Testing
113
115
  bug_tracker_uri: https://github.com/karafka/karafka-testing/issues
114
116
  source_code_uri: https://github.com/karafka/karafka-testing
115
117
  documentation_uri: https://karafka.io/docs
metadata.gz.sig CHANGED
Binary file