karafka-testing 2.4.3 → 2.4.4

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: fe477f5bd2b71d1114928a4db25c35f6fffe0a4eed5265eca11bbdaf631be953
4
- data.tar.gz: 30fa033178bdb55bcca8cbce705a5f91224380bb6900c2998b78da50af2291d5
3
+ metadata.gz: 7dbd023d8dfb5cb1f11dce4a3ddf893c005228b9f8bc9f0bf199d37eaad6a5be
4
+ data.tar.gz: 17754c2c15f2b975241294fd6eff2e0ff40f64188b0bffcf110f019e52fc3e84
5
5
  SHA512:
6
- metadata.gz: dbcb1174a90f12d0743474462802e495674627d34f60499cdbc28d48214512371a2532c71e7d94a2a565d4513c3161807e82f117f11d83e38f1aad11fc0f5f6a
7
- data.tar.gz: f621736a76fd8c2f20ab8908e1f1addc43e1913027cdd67d9a077e93a014f581f9a97d3b028d8b18eaee1246feb3a6d14abaddf3de766e17b7c3b47541f974c5
6
+ metadata.gz: 2ee758d81cc2ddf98fedafeac9105cf3c50aa0ad6845bca729aa3dde5c461f82328c91a6dd449faa460ee28c8e27e5b26ea87f1bbdc541f4be8c11aae6388020
7
+ data.tar.gz: 80d66738edd43d2e8a373f29b3658f12fce556505c5185af5929bc4e721c80eec54af6479661129cb1b57f67c7bc506fca0ddd410aea5418f85be8b71e6b6608
checksums.yaml.gz.sig CHANGED
Binary file
@@ -18,6 +18,7 @@ jobs:
18
18
  fail-fast: false
19
19
  matrix:
20
20
  ruby:
21
+ - '3.4.0-preview1'
21
22
  - '3.3'
22
23
  - '3.2'
23
24
  - '3.1'
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.3.1
1
+ 3.3.3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Karafka Test gem changelog
2
2
 
3
+ ## 2.4.4 (2024-07-02)
4
+ - [Enhancement] Memoize `consumer_for` so consumers can be set up for multiple topics and `let(:consumer)` is no longer a requirement. (dorner)
5
+
3
6
  ## 2.4.3 (2024-05-06)
4
7
  - [Fix] Fix: raw_key is not being assigned for rspec (CaioPenhalver)
5
8
  - [Fix] Fix: raw_key is not being assigned for minitest
data/Gemfile.lock CHANGED
@@ -1,30 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- karafka-testing (2.4.3)
4
+ karafka-testing (2.4.4)
5
5
  karafka (>= 2.4.0, < 2.5.0)
6
6
  waterdrop (>= 2.7.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- ffi (1.16.3)
12
- karafka (2.4.0)
11
+ base64 (0.2.0)
12
+ ffi (1.17.0)
13
+ ffi (1.17.0-x86_64-linux-gnu)
14
+ karafka (2.4.3)
15
+ base64 (~> 0.2)
13
16
  karafka-core (>= 2.4.0, < 2.5.0)
14
- waterdrop (>= 2.7.0, < 3.0.0)
17
+ waterdrop (>= 2.7.3, < 3.0.0)
15
18
  zeitwerk (~> 2.3)
16
- karafka-core (2.4.0)
17
- karafka-rdkafka (>= 0.15.0, < 0.16.0)
18
- karafka-rdkafka (0.15.0)
19
+ karafka-core (2.4.3)
20
+ karafka-rdkafka (>= 0.15.0, < 0.17.0)
21
+ karafka-rdkafka (0.16.0)
19
22
  ffi (~> 1.15)
20
23
  mini_portile2 (~> 2.6)
21
24
  rake (> 12)
22
- mini_portile2 (2.8.5)
25
+ mini_portile2 (2.8.7)
23
26
  rake (13.2.1)
24
- waterdrop (2.7.0)
27
+ waterdrop (2.7.3)
25
28
  karafka-core (>= 2.4.0, < 3.0.0)
29
+ karafka-rdkafka (>= 0.15.1)
26
30
  zeitwerk (~> 2.3)
27
- zeitwerk (2.6.13)
31
+ zeitwerk (2.6.16)
28
32
 
29
33
  PLATFORMS
30
34
  ruby
@@ -34,4 +38,4 @@ DEPENDENCIES
34
38
  karafka-testing!
35
39
 
36
40
  BUNDLED WITH
37
- 2.5.7
41
+ 2.5.14
@@ -37,6 +37,8 @@ module Karafka
37
37
  @_karafka_consumer_messages.clear
38
38
  @_karafka_producer_client.reset
39
39
 
40
+ @_karafka_consumer_mappings = {}
41
+
40
42
  Karafka.producer.stubs(:client).returns(@_karafka_producer_client)
41
43
  end
42
44
 
@@ -87,18 +89,23 @@ module Karafka
87
89
  # @example Send a json message to consumer and simulate, that it is partition 6
88
90
  # @karafka.produce({ 'hello' => 'world' }.to_json, 'partition' => 6)
89
91
  def _karafka_add_message_to_consumer_if_needed(message)
92
+ consumer_obj = if defined?(@consumer)
93
+ @consumer
94
+ else
95
+ @_karafka_consumer_mappings&.dig(message[:topic])
96
+ end
90
97
  # Consumer needs to be defined in order to pass messages to it
91
- return unless defined?(@consumer)
98
+ return unless defined?(consumer_obj)
92
99
  # We're interested in adding message to consumer only when it is a Karafka consumer
93
100
  # Users may want to test other things (models producing messages for example) and in
94
101
  # their case consumer will not be a consumer
95
- return unless @consumer.is_a?(Karafka::BaseConsumer)
102
+ return unless consumer_obj.is_a?(Karafka::BaseConsumer)
96
103
  # We target to the consumer only messages that were produced to it, since specs may also
97
104
  # produce other messages targeting other topics
98
- return unless message[:topic] == @consumer.topic.name
105
+ return unless message[:topic] == consumer_obj.topic.name
99
106
 
100
107
  # Build message metadata and copy any metadata that would come from the message
101
- metadata = _karafka_message_metadata_defaults
108
+ metadata = _karafka_message_metadata_defaults(consumer_obj)
102
109
 
103
110
  metadata.keys.each do |key|
104
111
  message_key = METADATA_DISPATCH_MAPPINGS.fetch(key, key)
@@ -116,13 +123,13 @@ module Karafka
116
123
  # Update batch metadata
117
124
  batch_metadata = Karafka::Messages::Builders::BatchMetadata.call(
118
125
  @_karafka_consumer_messages,
119
- @consumer.topic,
126
+ consumer_obj.topic,
120
127
  0,
121
128
  Time.now
122
129
  )
123
130
 
124
131
  # Update consumer messages batch
125
- @consumer.messages = Karafka::Messages::Messages.new(
132
+ consumer_obj.messages = Karafka::Messages::Messages.new(
126
133
  @_karafka_consumer_messages,
127
134
  batch_metadata
128
135
  )
@@ -132,9 +139,16 @@ module Karafka
132
139
  # @param payload [String] payload we want to dispatch
133
140
  # @param metadata [Hash] any metadata we want to dispatch alongside the payload
134
141
  def _karafka_produce(payload, metadata = {})
142
+ topic = if metadata[:topic]
143
+ metadata[:topic]
144
+ elsif defined?(@consumer)
145
+ @consumer.topic.name
146
+ else
147
+ @_karafka_consumer_mappings&.keys&.last
148
+ end
135
149
  Karafka.producer.produce_sync(
136
150
  {
137
- topic: @consumer.topic.name,
151
+ topic: topic,
138
152
  payload: payload
139
153
  }.merge(metadata)
140
154
  )
@@ -147,17 +161,18 @@ module Karafka
147
161
 
148
162
  private
149
163
 
164
+ # @param consumer_obj [Karafka::BaseConsumer] consumer reference
150
165
  # @return [Hash] message default options
151
- def _karafka_message_metadata_defaults
166
+ def _karafka_message_metadata_defaults(consumer_obj)
152
167
  {
153
- deserializers: @consumer.topic.deserializers,
168
+ deserializers: consumer_obj.topic.deserializers,
154
169
  timestamp: Time.now,
155
170
  raw_headers: {},
156
171
  raw_key: nil,
157
172
  offset: @_karafka_consumer_messages.size,
158
173
  partition: 0,
159
174
  received_at: Time.now,
160
- topic: @consumer.topic.name
175
+ topic: consumer_obj.topic.name
161
176
  }
162
177
  end
163
178
 
@@ -179,6 +194,7 @@ module Karafka
179
194
  @consumer.coordinator.seek_offset = 0
180
195
  # Indicate usage as for tests no direct enqueuing happens
181
196
  @consumer.instance_variable_set('@used', true)
197
+ @_karafka_consumer_mappings[topic.name] = @consumer
182
198
  @consumer
183
199
  end
184
200
  end
@@ -44,6 +44,7 @@ module Karafka
44
44
 
45
45
  _karafka_consumer_messages.clear
46
46
  _karafka_producer_client.reset
47
+ @_karafka_consumer_mappings = {}
47
48
 
48
49
  if Object.const_defined?('Mocha', false)
49
50
  Karafka.producer.stubs(:client).returns(_karafka_producer_client)
@@ -94,18 +95,23 @@ module Karafka
94
95
  # karafka.produce({ 'hello' => 'world' }.to_json, 'partition' => 6)
95
96
  # end
96
97
  def _karafka_add_message_to_consumer_if_needed(message)
98
+ consumer_obj = if defined?(consumer)
99
+ consumer
100
+ else
101
+ @_karafka_consumer_mappings&.dig(message[:topic])
102
+ end
97
103
  # Consumer needs to be defined in order to pass messages to it
98
- return unless defined?(consumer)
104
+ return unless consumer_obj
99
105
  # We're interested in adding message to consumer only when it is a Karafka consumer
100
106
  # Users may want to test other things (models producing messages for example) and in
101
107
  # their case consumer will not be a consumer
102
- return unless consumer.is_a?(Karafka::BaseConsumer)
108
+ return unless consumer_obj.is_a?(Karafka::BaseConsumer)
103
109
  # We target to the consumer only messages that were produced to it, since specs may also
104
110
  # produce other messages targeting other topics
105
- return unless message[:topic] == consumer.topic.name
111
+ return unless message[:topic] == consumer_obj.topic.name
106
112
 
107
113
  # Build message metadata and copy any metadata that would come from the message
108
- metadata = _karafka_message_metadata_defaults
114
+ metadata = _karafka_message_metadata_defaults(consumer_obj)
109
115
 
110
116
  metadata.keys.each do |key|
111
117
  message_key = METADATA_DISPATCH_MAPPINGS.fetch(key, key)
@@ -124,13 +130,13 @@ module Karafka
124
130
  # Update batch metadata
125
131
  batch_metadata = Karafka::Messages::Builders::BatchMetadata.call(
126
132
  _karafka_consumer_messages,
127
- consumer.topic,
133
+ consumer_obj.topic,
128
134
  0,
129
135
  Time.now
130
136
  )
131
137
 
132
138
  # Update consumer messages batch
133
- consumer.messages = Karafka::Messages::Messages.new(
139
+ consumer_obj.messages = Karafka::Messages::Messages.new(
134
140
  _karafka_consumer_messages,
135
141
  batch_metadata
136
142
  )
@@ -140,9 +146,16 @@ module Karafka
140
146
  # @param payload [String] payload we want to dispatch
141
147
  # @param metadata [Hash] any metadata we want to dispatch alongside the payload
142
148
  def _karafka_produce(payload, metadata = {})
149
+ topic = if metadata[:topic]
150
+ metadata[:topic]
151
+ elsif defined?(consumer)
152
+ consumer.topic.name
153
+ else
154
+ @_karafka_consumer_mappings&.keys&.last
155
+ end
143
156
  Karafka.producer.produce_sync(
144
157
  {
145
- topic: consumer.topic.name,
158
+ topic: topic,
146
159
  payload: payload
147
160
  }.merge(metadata)
148
161
  )
@@ -155,17 +168,18 @@ module Karafka
155
168
 
156
169
  private
157
170
 
171
+ # @param consumer_obj [Karafka::BaseConsumer] consumer reference
158
172
  # @return [Hash] message default options
159
- def _karafka_message_metadata_defaults
173
+ def _karafka_message_metadata_defaults(consumer_obj)
160
174
  {
161
- deserializers: consumer.topic.deserializers,
175
+ deserializers: consumer_obj.topic.deserializers,
162
176
  timestamp: Time.now,
163
177
  raw_headers: {},
164
178
  raw_key: nil,
165
179
  offset: _karafka_consumer_messages.size,
166
180
  partition: 0,
167
181
  received_at: Time.now,
168
- topic: consumer.topic.name
182
+ topic: consumer_obj.topic.name
169
183
  }
170
184
  end
171
185
 
@@ -188,6 +202,8 @@ module Karafka
188
202
  consumer.coordinator.seek_offset = 0
189
203
  # Indicate usage as for tests no direct enqueuing happens
190
204
  consumer.instance_variable_set('@used', true)
205
+
206
+ @_karafka_consumer_mappings[topic.name] = consumer
191
207
  consumer
192
208
  end
193
209
  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.4.3'
7
+ VERSION = '2.4.4'
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.4.3
4
+ version: 2.4.4
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-05-06 00:00:00.000000000 Z
38
+ date: 2024-07-02 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: karafka
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
134
  requirements: []
135
- rubygems_version: 3.5.9
135
+ rubygems_version: 3.5.11
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Library which provides helpers for easier Karafka consumers tests
metadata.gz.sig CHANGED
@@ -1 +1,2 @@
1
- Z���KIW��{�[�����:���>��nn�^�y =Ϝ�x:�G���< ��N���(E��c���m��zSlv�;=�|P�� ���D��z��2��J��JeR�Z-�8"����V��$�JP��Pn�寡�"�ʎV��𳯃�Fy4ե�c*�;�ȩ)������ot��e;�]���n��U�/v8xް$c\oѳͼ~mH�t2��~�8��0�)ZC����� �j}�RSz��D+c-�}2"w�δ�/����!t�qQUA�R\n� ��D0:��,6�ʹr�)L��1�@�M^��}Y.�ó�]��Yc��Ȑ����0�NkZ��oMYIo�wx+S��e��S/qU��J!�������-��X�O>�
1
+ H�ó�ֳ�2Yu�j������8+���q4���~����間j� ���-�O��zM(>(�.;^�&� U{B�#x��셎�������;��ɨ��+5/Nt/��ٲ��آV��3���-Qmx^��E]QB�# ���?ksr���H���}��rԤǘ��o[E������F�‡�\�9��?.ܪ���D���/c��teT
2
+ l�