karafka-testing 2.3.1 → 2.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +1 -1
- data/lib/karafka/testing/errors.rb +1 -11
- data/lib/karafka/testing/helpers.rb +55 -0
- data/lib/karafka/testing/minitest/helpers.rb +18 -53
- data/lib/karafka/testing/rspec/helpers.rb +2 -45
- data/lib/karafka/testing/version.rb +1 -1
- data/lib/karafka/testing.rb +0 -27
- data.tar.gz.sig +0 -0
- metadata +3 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91a12f6c61514bc890bdfd8f6c91e1a834dd1516299f8a6d421c54037d3b55b3
|
4
|
+
data.tar.gz: 70d467cb5994992d5a3046511bca0676fabc673b2b36c45d366b98d6aeeeae06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebb18327ff48e4443377a54a09d35611c84f6806bc298ceb0792d1b2862f7f1c35e2c528bdb8b7e611d61998201f851dc8648530d398fdbbd33ec504d7fedae7
|
7
|
+
data.tar.gz: 9898b5f1bd9264b696361cfd60039226864eb26743722c21a251fad26d2bd9d633b9be0cadcf62dbc7638a5287b69a0aaf720b12ad70595cc03c6bc1c4fe920f
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Karafka Test gem changelog
|
2
2
|
|
3
|
+
## 2.3.3 (2024-04-19)
|
4
|
+
- [Refactor] Extract common components for Minitest and RSpec.
|
5
|
+
- [Fix] Fix a case where multiplexed SG would fail with `TopicInManyConsumerGroupsError`
|
6
|
+
|
7
|
+
## 2.3.2 (2024-04-03)
|
8
|
+
- [Enhancement] Support `Minitest::Spec`.
|
9
|
+
|
3
10
|
## 2.3.1 (2024-03-07)
|
4
11
|
- [Enhancement] Prevent usage of testing when Karafka is not loaded.
|
5
12
|
- [Enhancement] Prevent usage of testing when Karafka is not initialized.
|
data/Gemfile.lock
CHANGED
@@ -15,17 +15,7 @@ module Karafka
|
|
15
15
|
TopicInManyConsumerGroupsError = Class.new(BaseError)
|
16
16
|
|
17
17
|
# Raised when you requested a topic from a consumer group that does not exist
|
18
|
-
|
19
|
-
|
20
|
-
# Raised when trying to use testing without Karafka app being visible
|
21
|
-
# If you are seeing this error, then you tried to use testing helpers without Karafka being
|
22
|
-
# loaded prior to this happening.
|
23
|
-
KarafkaNotLoadedError = Class.new(BaseError)
|
24
|
-
|
25
|
-
# Raised when there is an attempt to use the testing primitives without Karafka app being
|
26
|
-
# configured. Prior to initialization process, most of config values are nils, etc and
|
27
|
-
# mocks will not work.
|
28
|
-
KarafkaNotInitializedError = Class.new(BaseError)
|
18
|
+
ConsumerGroupNotFound = Class.new(BaseError)
|
29
19
|
end
|
30
20
|
end
|
31
21
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
module Testing
|
5
|
+
# Common helper methods that are shared in between RSpec and Minitest
|
6
|
+
module Helpers
|
7
|
+
class << self
|
8
|
+
# Finds all the routing topics matching requested topic within all topics or within
|
9
|
+
# provided consumer group based on name
|
10
|
+
#
|
11
|
+
# @param requested_topic [String] requested topic name
|
12
|
+
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
13
|
+
# @return [Array<Karafka::Routing::Topic>] all matching topics
|
14
|
+
#
|
15
|
+
# @note Since we run the lookup on subscription groups, the search will automatically
|
16
|
+
# expand with matching patterns
|
17
|
+
def karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group)
|
18
|
+
karafka_consumer_find_subscription_groups(requested_consumer_group)
|
19
|
+
# multiplexed subscriptions of the same origin share name, thus we can reduced
|
20
|
+
# multiplexing to the first one as during testing, there is no multiplexing parallel
|
21
|
+
# execution anyhow
|
22
|
+
.uniq(&:name)
|
23
|
+
.map(&:topics)
|
24
|
+
.filter_map do |topics|
|
25
|
+
topics.find(requested_topic.to_s)
|
26
|
+
rescue Karafka::Errors::TopicNotFoundError
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Finds subscription groups from the requested consumer group or selects all if no
|
32
|
+
# consumer group specified
|
33
|
+
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
34
|
+
# @return [Array<Karafka::Routing::SubscriptionGroup>] requested subscription groups
|
35
|
+
def karafka_consumer_find_subscription_groups(requested_consumer_group)
|
36
|
+
if requested_consumer_group && !requested_consumer_group.empty?
|
37
|
+
::Karafka::App
|
38
|
+
.subscription_groups
|
39
|
+
# Find matching consumer group
|
40
|
+
.find { |cg, _sgs| cg.name == requested_consumer_group.to_s }
|
41
|
+
# Raise error if not found
|
42
|
+
.tap { |cg| cg || raise(Errors::ConsumerGroupNotFound, requested_consumer_group) }
|
43
|
+
# Since lookup was on a hash, get the value, that is subscription groups
|
44
|
+
.last
|
45
|
+
else
|
46
|
+
::Karafka::App
|
47
|
+
.subscription_groups
|
48
|
+
.values
|
49
|
+
.flatten
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'karafka/testing/errors'
|
4
|
+
require 'karafka/testing/helpers'
|
4
5
|
require 'karafka/testing/spec_consumer_client'
|
5
6
|
require 'karafka/testing/spec_producer_client'
|
6
7
|
require 'karafka/testing/minitest/proxy'
|
@@ -16,19 +17,25 @@ module Karafka
|
|
16
17
|
# Adds all the needed extra functionalities to the minitest group
|
17
18
|
# @param base [Class] Minitest example group we want to extend
|
18
19
|
def included(base)
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
eval_flow = lambda 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)
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
@_karafka_consumer_client = Karafka::Testing::SpecConsumerClient.new
|
26
|
-
@_karafka_producer_client = Karafka::Testing::SpecProducerClient.new(self)
|
26
|
+
@_karafka_consumer_messages.clear
|
27
|
+
@_karafka_producer_client.reset
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
Karafka.producer.stubs(:client).returns(@_karafka_producer_client)
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
+
if base.to_s == 'Minitest::Spec'
|
33
|
+
base.class_eval do
|
34
|
+
before(&eval_flow)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
base.class_eval do
|
38
|
+
setup(&eval_flow)
|
32
39
|
end
|
33
40
|
end
|
34
41
|
end
|
@@ -47,7 +54,7 @@ module Karafka
|
|
47
54
|
# @example Creates a consumer instance with settings for `my_requested_topic`
|
48
55
|
# consumer = @karafka.consumer_for(:my_requested_topic)
|
49
56
|
def _karafka_consumer_for(requested_topic, requested_consumer_group = nil)
|
50
|
-
selected_topics =
|
57
|
+
selected_topics = Testing::Helpers.karafka_consumer_find_candidate_topics(
|
51
58
|
requested_topic.to_s,
|
52
59
|
requested_consumer_group.to_s
|
53
60
|
)
|
@@ -160,48 +167,6 @@ module Karafka
|
|
160
167
|
@consumer.instance_variable_set('@used', true)
|
161
168
|
@consumer
|
162
169
|
end
|
163
|
-
|
164
|
-
# Finds all the routing topics matching requested topic within all topics or within
|
165
|
-
# provided consumer group based on name
|
166
|
-
#
|
167
|
-
# @param requested_topic [String] requested topic name
|
168
|
-
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
169
|
-
# @return [Array<Karafka::Routing::Topic>] all matching topics
|
170
|
-
#
|
171
|
-
# @note Since we run the lookup on subscription groups, the search will automatically
|
172
|
-
# expand with matching patterns
|
173
|
-
def _karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group)
|
174
|
-
_karafka_consumer_find_subscription_groups(requested_consumer_group)
|
175
|
-
.map(&:topics)
|
176
|
-
.filter_map do |topics|
|
177
|
-
topics.find(requested_topic.to_s)
|
178
|
-
rescue Karafka::Errors::TopicNotFoundError
|
179
|
-
nil
|
180
|
-
end
|
181
|
-
.uniq(&:consumer_group)
|
182
|
-
end
|
183
|
-
|
184
|
-
# Finds subscription groups from the requested consumer group or selects all if no
|
185
|
-
# consumer group specified
|
186
|
-
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
187
|
-
# @return [Array<Karafka::Routing::SubscriptionGroup>] requested subscription groups
|
188
|
-
def _karafka_consumer_find_subscription_groups(requested_consumer_group)
|
189
|
-
if requested_consumer_group && !requested_consumer_group.empty?
|
190
|
-
::Karafka::App
|
191
|
-
.subscription_groups
|
192
|
-
# Find matching consumer group
|
193
|
-
.find { |cg, _sgs| cg.name == requested_consumer_group.to_s }
|
194
|
-
# Raise error if not found
|
195
|
-
.tap { |cg| cg || raise(Errors::ConsumerGroupNotFound, requested_consumer_group) }
|
196
|
-
# Since lookup was on a hash, get the value, that is subscription groups
|
197
|
-
.last
|
198
|
-
else
|
199
|
-
::Karafka::App
|
200
|
-
.subscription_groups
|
201
|
-
.values
|
202
|
-
.flatten
|
203
|
-
end
|
204
|
-
end
|
205
170
|
end
|
206
171
|
end
|
207
172
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'waterdrop'
|
4
4
|
require 'karafka/testing/errors'
|
5
|
+
require 'karafka/testing/helpers'
|
5
6
|
require 'karafka/testing/spec_consumer_client'
|
6
7
|
require 'karafka/testing/spec_producer_client'
|
7
8
|
require 'karafka/testing/rspec/proxy'
|
@@ -30,8 +31,6 @@ module Karafka
|
|
30
31
|
base.let(:_karafka_producer_client) { Karafka::Testing::SpecProducerClient.new(self) }
|
31
32
|
|
32
33
|
base.prepend_before do
|
33
|
-
Karafka::Testing.ensure_karafka_initialized!
|
34
|
-
|
35
34
|
_karafka_consumer_messages.clear
|
36
35
|
_karafka_producer_client.reset
|
37
36
|
|
@@ -59,7 +58,7 @@ module Karafka
|
|
59
58
|
# subject(:consumer) { karafka.consumer_for(:my_requested_topic) }
|
60
59
|
# end
|
61
60
|
def _karafka_consumer_for(requested_topic, requested_consumer_group = nil)
|
62
|
-
selected_topics =
|
61
|
+
selected_topics = Testing::Helpers.karafka_consumer_find_candidate_topics(
|
63
62
|
requested_topic.to_s,
|
64
63
|
requested_consumer_group.to_s
|
65
64
|
)
|
@@ -178,48 +177,6 @@ module Karafka
|
|
178
177
|
consumer.instance_variable_set('@used', true)
|
179
178
|
consumer
|
180
179
|
end
|
181
|
-
|
182
|
-
# Finds all the routing topics matching requested topic within all topics or within
|
183
|
-
# provided consumer group based on name
|
184
|
-
#
|
185
|
-
# @param requested_topic [String] requested topic name
|
186
|
-
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
187
|
-
# @return [Array<Karafka::Routing::Topic>] all matching topics
|
188
|
-
#
|
189
|
-
# @note Since we run the lookup on subscription groups, the search will automatically
|
190
|
-
# expand with matching patterns
|
191
|
-
def _karafka_consumer_find_candidate_topics(requested_topic, requested_consumer_group)
|
192
|
-
_karafka_consumer_find_subscription_groups(requested_consumer_group)
|
193
|
-
.map(&:topics)
|
194
|
-
.filter_map do |topics|
|
195
|
-
topics.find(requested_topic.to_s)
|
196
|
-
rescue Karafka::Errors::TopicNotFoundError
|
197
|
-
nil
|
198
|
-
end
|
199
|
-
.uniq(&:consumer_group)
|
200
|
-
end
|
201
|
-
|
202
|
-
# Finds subscription groups from the requested consumer group or selects all if no
|
203
|
-
# consumer group specified
|
204
|
-
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
205
|
-
# @return [Array<Karafka::Routing::SubscriptionGroup>] requested subscription groups
|
206
|
-
def _karafka_consumer_find_subscription_groups(requested_consumer_group)
|
207
|
-
if requested_consumer_group && !requested_consumer_group.empty?
|
208
|
-
::Karafka::App
|
209
|
-
.subscription_groups
|
210
|
-
# Find matching consumer group
|
211
|
-
.find { |cg, _sgs| cg.name == requested_consumer_group.to_s }
|
212
|
-
# Raise error if not found
|
213
|
-
.tap { |cg| cg || raise(Errors::ConsumerGroupNotFound, requested_consumer_group) }
|
214
|
-
# Since lookup was on a hash, get the value, that is subscription groups
|
215
|
-
.last
|
216
|
-
else
|
217
|
-
::Karafka::App
|
218
|
-
.subscription_groups
|
219
|
-
.values
|
220
|
-
.flatten
|
221
|
-
end
|
222
|
-
end
|
223
180
|
end
|
224
181
|
end
|
225
182
|
end
|
data/lib/karafka/testing.rb
CHANGED
@@ -4,32 +4,5 @@
|
|
4
4
|
module Karafka
|
5
5
|
# Testing lib module
|
6
6
|
module Testing
|
7
|
-
class << self
|
8
|
-
# Makes sure, that we do not use the testing stubs, etc when Karafka app is not loaded
|
9
|
-
#
|
10
|
-
# You should never use karafka-testing primitives when framework is not loaded because
|
11
|
-
# testing lib stubs certain pieces of Karafka that need to be initialized.
|
12
|
-
def ensure_karafka_loaded!
|
13
|
-
return if ::Karafka.const_defined?('App', false)
|
14
|
-
|
15
|
-
raise(
|
16
|
-
Karafka::Testing::Errors::KarafkaNotLoadedError,
|
17
|
-
'Make sure to load Karafka framework prior to usage of the testing components.'
|
18
|
-
)
|
19
|
-
end
|
20
|
-
|
21
|
-
# If you do not initialize Karafka always within your specs, do not include/use this lib
|
22
|
-
# in places where Karafka would not be loaded.
|
23
|
-
def ensure_karafka_initialized!
|
24
|
-
ensure_karafka_loaded!
|
25
|
-
|
26
|
-
return unless Karafka::App.initializing?
|
27
|
-
|
28
|
-
raise(
|
29
|
-
Karafka::Testing::Errors::KarafkaNotInitializedError,
|
30
|
-
'Make sure to initialize Karafka framework prior to usage of the testing components.'
|
31
|
-
)
|
32
|
-
end
|
33
|
-
end
|
34
7
|
end
|
35
8
|
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.3.
|
4
|
+
version: 2.3.3
|
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: 2024-
|
38
|
+
date: 2024-04-22 00:00:00.000000000 Z
|
39
39
|
dependencies:
|
40
40
|
- !ruby/object:Gem::Dependency
|
41
41
|
name: karafka
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- lib/karafka-testing.rb
|
98
98
|
- lib/karafka/testing.rb
|
99
99
|
- lib/karafka/testing/errors.rb
|
100
|
+
- lib/karafka/testing/helpers.rb
|
100
101
|
- lib/karafka/testing/minitest/helpers.rb
|
101
102
|
- lib/karafka/testing/minitest/proxy.rb
|
102
103
|
- lib/karafka/testing/rspec/helpers.rb
|
metadata.gz.sig
CHANGED
Binary file
|