karafka-testing 2.6.4 → 2.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08d462de8e80468e64eef6f71a2c17e47a4a1ca597dd9e290feaf0ccfc5f49d6'
4
- data.tar.gz: d7971f8a44ed5f442de78467dce1bbd243596c5bc2bd05f2046b7778b3dfa2c7
3
+ metadata.gz: 5d7fa60a0c89667ad25106ddea370a231d99eaaef8f53869c2fa8823cb0b91f9
4
+ data.tar.gz: 7e6ff59e10fd3786f33e74c8b0c389a497bfbf2295b582f25b0e873b87dba801
5
5
  SHA512:
6
- metadata.gz: b94d77ccaed5dbb9c8d3fe1742e01b622b7a81a5fa9a3666584240217ef8100c12f76d1043cee6e313506d1e082c31becc1a11af7f7bcfea1d7f9edcc6c44dd0
7
- data.tar.gz: a8682074bb85e6f5c1be92d3207933395311882f9c6fbebf446ea558b53e57b7f12a2ff5c2fc3a6142ce3adf6b6903ef740a8dc304a13ae728f72d2f261c956a
6
+ metadata.gz: 9a19a3b8bbf2069a47a85eee6ccba7e55e353cb3ef100c4df6045acdc3dffe947b0f9d09c5966d7b4c0bec9421051bd5da644678372a6692acd0edf94d4181ae
7
+ data.tar.gz: dadac4dcb4294b76b65e36d416157d3af6f1e4605376973cb0dd320ae446faaa1ffb34585a29e67d1540d8e2741bf3a448cdfa95b1d4d2a4a93fe8318abe9c6c
data/.github/CODEOWNERS CHANGED
@@ -1,3 +1,6 @@
1
+ # Every change needs a review from a maintainer before it can merge.
2
+ * @mensfeld
3
+
1
4
  /.github @mensfeld
2
5
  /.github/workflows/ @mensfeld
3
- /.github/actions/ @mensfeld
6
+ /.github/actions/ @mensfeld
@@ -14,9 +14,16 @@ permissions:
14
14
  contents: read
15
15
 
16
16
  jobs:
17
+ # Docs-only pull requests skip the jobs below; CI Success still runs and reports success,
18
+ # so the required check does not stay pending
19
+ changes:
20
+ uses: ./.github/workflows/ci_changes.yml
21
+
17
22
  tests:
18
23
  timeout-minutes: 30
19
24
  runs-on: ubuntu-latest
25
+ needs: changes
26
+ if: needs.changes.outputs.code == 'true'
20
27
  strategy:
21
28
  fail-fast: false
22
29
  matrix:
@@ -24,6 +31,7 @@ jobs:
24
31
  - '4.0'
25
32
  - '3.4'
26
33
  - '3.3'
34
+ - 'jruby-10.0'
27
35
  include:
28
36
  - ruby: '4.0'
29
37
  coverage: 'true'
@@ -36,7 +44,7 @@ jobs:
36
44
  run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"
37
45
 
38
46
  - name: Set up Ruby
39
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
47
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
40
48
  with:
41
49
  ruby-version: ${{matrix.ruby}}
42
50
  bundler: 'latest'
@@ -59,6 +67,8 @@ jobs:
59
67
  rubocop:
60
68
  timeout-minutes: 5
61
69
  runs-on: ubuntu-latest
70
+ needs: changes
71
+ if: needs.changes.outputs.code == 'true'
62
72
  env:
63
73
  BUNDLE_GEMFILE: Gemfile.lint
64
74
  steps:
@@ -66,9 +76,9 @@ jobs:
66
76
  with:
67
77
  fetch-depth: 0
68
78
  - name: Set up Ruby
69
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
79
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
70
80
  with:
71
- ruby-version: '4.0.9'
81
+ ruby-version: '4.0.10'
72
82
  bundler-cache: true
73
83
  - name: Run rubocop
74
84
  run: bundle exec rubocop
@@ -76,28 +86,32 @@ jobs:
76
86
  yard-lint:
77
87
  timeout-minutes: 5
78
88
  runs-on: ubuntu-latest
89
+ needs: changes
90
+ if: needs.changes.outputs.code == 'true'
79
91
  env:
80
92
  BUNDLE_GEMFILE: Gemfile.lint
81
93
  steps:
82
94
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
83
95
  - name: Set up Ruby
84
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
96
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
85
97
  with:
86
- ruby-version: '4.0.9'
98
+ ruby-version: '4.0.10'
87
99
  bundler-cache: true
88
100
  - run: bundle exec yard-lint lib/
89
101
 
90
102
  minitest-integration:
91
103
  timeout-minutes: 5
92
104
  runs-on: ubuntu-latest
105
+ needs: changes
106
+ if: needs.changes.outputs.code == 'true'
93
107
  steps:
94
108
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
95
109
  with:
96
110
  fetch-depth: 0
97
111
  - name: Set up Ruby
98
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
112
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
99
113
  with:
100
- ruby-version: '4.0.9'
114
+ ruby-version: '4.0.10'
101
115
  bundler: 'latest'
102
116
  - name: Install dependencies
103
117
  run: bundle install --jobs 4 --retry 3
@@ -107,23 +121,69 @@ jobs:
107
121
  rspec-integration:
108
122
  timeout-minutes: 5
109
123
  runs-on: ubuntu-latest
124
+ needs: changes
125
+ if: needs.changes.outputs.code == 'true'
110
126
  steps:
111
127
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
112
128
  with:
113
129
  fetch-depth: 0
114
130
  - name: Set up Ruby
115
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
131
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
116
132
  with:
117
- ruby-version: '4.0.9'
133
+ ruby-version: '4.0.10'
118
134
  bundler: 'latest'
119
135
  - name: Install dependencies
120
136
  run: bundle install --jobs 4 --retry 3
121
137
  - name: Run rspec integration tests
122
138
  run: bundle exec rspec test/integration/rspec --require ./test/integration/rspec_helper.rb
123
139
 
140
+ minitest-integration-pro:
141
+ timeout-minutes: 5
142
+ runs-on: ubuntu-latest
143
+ needs: changes
144
+ if: needs.changes.outputs.code == 'true'
145
+ steps:
146
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
147
+ with:
148
+ fetch-depth: 0
149
+ - name: Set up Ruby
150
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
151
+ with:
152
+ ruby-version: '4.0.10'
153
+ bundler: 'latest'
154
+ - name: Install dependencies
155
+ run: bundle install --jobs 4 --retry 3
156
+ - name: Run minitest Pro integration tests
157
+ env:
158
+ KARAFKA_PRO_LICENSE_TOKEN: ${{ secrets.KARAFKA_PRO_LICENSE_TOKEN }}
159
+ run: bundle exec ruby -Ilib -e "Dir['test/integration/minitest_pro/*_test.rb'].sort.each { |f| require File.expand_path(f) }"
160
+
161
+ rspec-integration-pro:
162
+ timeout-minutes: 5
163
+ runs-on: ubuntu-latest
164
+ needs: changes
165
+ if: needs.changes.outputs.code == 'true'
166
+ steps:
167
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
168
+ with:
169
+ fetch-depth: 0
170
+ - name: Set up Ruby
171
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
172
+ with:
173
+ ruby-version: '4.0.10'
174
+ bundler: 'latest'
175
+ - name: Install dependencies
176
+ run: bundle install --jobs 4 --retry 3
177
+ - name: Run rspec Pro integration tests
178
+ env:
179
+ KARAFKA_PRO_LICENSE_TOKEN: ${{ secrets.KARAFKA_PRO_LICENSE_TOKEN }}
180
+ run: bundle exec rspec test/integration/rspec_pro
181
+
124
182
  lostconf:
125
183
  timeout-minutes: 5
126
184
  runs-on: ubuntu-latest
185
+ needs: changes
186
+ if: needs.changes.outputs.code == 'true'
127
187
  steps:
128
188
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
129
189
  with:
@@ -143,17 +203,22 @@ jobs:
143
203
  runs-on: ubuntu-latest
144
204
  if: always()
145
205
  needs:
206
+ - changes
146
207
  - rubocop
147
208
  - tests
148
209
  - minitest-integration
149
210
  - rspec-integration
211
+ - minitest-integration-pro
212
+ - rspec-integration-pro
150
213
  - yard-lint
151
214
  - lostconf
152
215
  steps:
216
+ # Skipped jobs are fine only when the change is docs-only
153
217
  - name: Check all jobs passed
154
218
  if: |
219
+ needs.changes.result != 'success' ||
155
220
  contains(needs.*.result, 'failure') ||
156
221
  contains(needs.*.result, 'cancelled') ||
157
- contains(needs.*.result, 'skipped')
222
+ (needs.changes.outputs.code != 'false' && contains(needs.*.result, 'skipped'))
158
223
  run: exit 1
159
224
  - run: echo "All CI checks passed!"
@@ -0,0 +1,56 @@
1
+ # Decides whether a pull request touches anything but docs. Called as the `changes` job by each
2
+ # CI workflow that runs on pull requests: docs-only pull requests skip the heavy jobs, while the
3
+ # Success job of each workflow still runs and reports success, so the required checks do not
4
+ # stay pending. Scheduled and any other runs always test everything
5
+
6
+ name: CI Changes
7
+
8
+ on:
9
+ workflow_call:
10
+ outputs:
11
+ code:
12
+ description: "'false' when the heavy jobs can be skipped, 'true' otherwise"
13
+ value: ${{ jobs.detect.outputs.code }}
14
+
15
+ permissions:
16
+ contents: read
17
+
18
+ jobs:
19
+ detect:
20
+ timeout-minutes: 5
21
+ runs-on: ubuntu-latest
22
+ outputs:
23
+ code: ${{ steps.filter.outputs.code }}
24
+ steps:
25
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
26
+ with:
27
+ fetch-depth: 2
28
+
29
+ - name: Detect code changes
30
+ id: filter
31
+ env:
32
+ EVENT_NAME: ${{ github.event_name }}
33
+ run: |
34
+ # Only pull requests can skip; scheduled and any other runs always test everything
35
+ if [ "$EVENT_NAME" != "pull_request" ]; then
36
+ echo "code=true" >> "$GITHUB_OUTPUT"
37
+ exit 0
38
+ fi
39
+
40
+ # HEAD is the PR merge commit, so HEAD^1..HEAD is what the PR changes in master
41
+ changed="$(git diff --name-only --no-renames HEAD^1 HEAD)"
42
+
43
+ # Anything not on the docs list counts as code (an empty diff too)
44
+ code=false
45
+ while IFS= read -r file; do
46
+ case "$file" in
47
+ lib/*|spec/*|test/*|ext/*|bin/*) code=true ;;
48
+ *.md|LICENSE*|MIT-LICENSE|.github/ISSUE_TEMPLATE/*|.github/FUNDING.yml) ;;
49
+ *.png|*.jpg|*.jpeg|*.svg|*.gif) ;;
50
+ *) code=true ;;
51
+ esac
52
+ done <<< "$changed"
53
+
54
+ echo "Changed files:"
55
+ echo "$changed"
56
+ echo "code=$code" >> "$GITHUB_OUTPUT"
@@ -24,12 +24,13 @@ jobs:
24
24
  fetch-depth: 0
25
25
 
26
26
  - name: Set up Ruby
27
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
27
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
28
28
  with:
29
29
  bundler-cache: false
30
30
 
31
- - name: Bundle install
32
- run: |
33
- bundle install --jobs 4 --retry 3
31
+ - name: Build the gem
32
+ run: gem build *.gemspec
34
33
 
35
- - uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
34
+ # Unlike rubygems/release-gem, this action does not push when signing fails
35
+ - name: Sign and push the gem (with Sigstore attestation)
36
+ uses: karafka/misc/.github/actions/gem-push@0fae3bd661f8f240f5b12039eb844ad52940a4fb # master
@@ -0,0 +1,49 @@
1
+ name: Sigstore Check
2
+
3
+ # Builds the gem, signs it the way the push workflows do and verifies the attestation, without
4
+ # pushing anything, so a broken signer shows up before a release
5
+
6
+ on:
7
+ schedule:
8
+ - cron: '0 3 * * *'
9
+ pull_request:
10
+ branches: [ master ]
11
+ paths:
12
+ - '.github/workflows/sigstore_check.yml'
13
+ - '.github/workflows/push*.yml'
14
+ workflow_dispatch:
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ concurrency:
20
+ group: ${{ github.workflow }}-${{ github.ref }}
21
+ cancel-in-progress: true
22
+
23
+ jobs:
24
+ sign:
25
+ # Signing needs the OIDC token, which forks do not get
26
+ if: >-
27
+ github.repository_owner == 'karafka' &&
28
+ (github.event_name != 'pull_request' ||
29
+ github.event.pull_request.head.repo.full_name == github.repository)
30
+ runs-on: ubuntu-latest
31
+ timeout-minutes: 15
32
+
33
+ permissions:
34
+ contents: read
35
+ id-token: write
36
+
37
+ steps:
38
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
39
+
40
+ - name: Set up Ruby
41
+ uses: ruby/setup-ruby@f9c4492e86def3e644ab32a0ef185eda5b0c20d0 # v1.342.0
42
+ with:
43
+ bundler-cache: false
44
+
45
+ - name: Build the gem
46
+ run: gem build *.gemspec
47
+
48
+ - name: Sign and verify the gem
49
+ uses: karafka/misc/.github/actions/sigstore-check@0fae3bd661f8f240f5b12039eb844ad52940a4fb # master
data/.gitignore CHANGED
@@ -17,6 +17,9 @@
17
17
  # sftp configuration file
18
18
  sftp-config.json
19
19
 
20
+ ### AI stuff
21
+ .claude
22
+ .codex
20
23
 
21
24
  ### Ruby template
22
25
  *.gem
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 4.0.9
1
+ 4.0.10
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Karafka Testing Changelog
2
2
 
3
+ ## 2.6.5 (2026-11-25)
4
+ - [Feature] Record marks with their offset metadata. Assert them with `karafka.marks` (#214).
5
+ - [Feature] Set the Pro offset metadata that consumers read with `karafka.offset_metadata = "..."`.
6
+ - [Maintenance] Publish with a Sigstore attestation again. 2.6.3 and 2.6.4 were published without one: signing failed in the release workflow (the signer does not work with json 3.x, the default of Ruby 4.0) and the attestation patch then pushed the gems unsigned.
7
+ - [Maintenance] Sign the gem with `sigstore-cli` and push it with `gem push --attestation` (the `gem-push` action of `karafka/misc`), so a failed signing stops the release instead of publishing the gem without an attestation.
8
+ - [Fix] Make Pro `#offset_metadata` work in specs. It no longer fails with `NoMethodError: undefined method '[]' for false`.
9
+ - [Fix] Raise `Karafka::Testing::Errors::TopicNotDefinedError` from `karafka.produce` when no topic can be resolved. It no longer fails with a confusing `WaterDrop::Errors::MessageInvalidError` when no `topic:` is given and no consumer is defined (#227).
10
+
3
11
  ## 2.6.4 (2026-11-04)
4
12
  - [Feature] Record consumer pauses and resumes. Assert them with `karafka.pauses` and `karafka.resumes` (#214).
5
13
  - [Maintenance] Declare `rake` in the `Gemfile`. It came only through `karafka-rdkafka`, which will stop depending on it for precompiled platform gems.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- karafka-testing (2.6.4)
4
+ karafka-testing (2.6.5)
5
5
  karafka (>= 2.6.0.rc2, < 2.7.0)
6
6
  waterdrop (>= 2.10.0)
7
7
 
@@ -35,9 +35,9 @@ GEM
35
35
  rake (> 12)
36
36
  logger (1.7.0)
37
37
  mini_portile2 (2.8.9)
38
- minitest (6.0.6)
38
+ minitest (6.1.0)
39
39
  drb (~> 2.0)
40
- prism (~> 1.5)
40
+ prism (>= 1.5, < 3)
41
41
  mocha (3.1.0)
42
42
  ruby2_keywords (>= 0.0.5)
43
43
  prism (1.9.0)
data/Rakefile CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "bundler/gem_tasks"
5
+ require "minitest/test_task"
5
6
 
6
- require "rake/testtask"
7
-
8
- Rake::TestTask.new(:test) do |t|
7
+ Minitest::TestTask.create(:test) do |t|
9
8
  t.libs << "test"
10
- t.test_files = FileList["test/test_helper.rb", "test/**/*_test.rb"]
11
- .exclude("test/integration/**/*")
9
+ t.libs << "lib"
10
+ t.test_globs = ["test/lib/**/*_test.rb"]
11
+ # Load test_helper before minitest/autorun so SimpleCov's at_exit hook is
12
+ # registered first and thus runs last (LIFO), collecting coverage AFTER tests
13
+ t.test_prelude = 'require "test_helper"; require "minitest/autorun"'
12
14
  end
13
15
 
14
16
  task default: :test
@@ -26,6 +26,12 @@ module Karafka
26
26
  # configured. Prior to initialization process, most of config values are nils, etc and
27
27
  # mocks will not work.
28
28
  KarafkaNotInitializedError = Class.new(BaseError)
29
+
30
+ # Raised when `karafka.produce` cannot resolve any target topic because no `topic:` was
31
+ # given, no consumer is defined and no consumer has been built with `consumer_for`.
32
+ # Without it we would pass a nil topic to WaterDrop and fail with a confusing
33
+ # `WaterDrop::Errors::MessageInvalidError`.
34
+ TopicNotDefinedError = Class.new(BaseError)
29
35
  end
30
36
  end
31
37
  end
@@ -5,6 +5,7 @@ require "karafka/testing/errors"
5
5
  require "karafka/testing/helpers"
6
6
  require "karafka/testing/spec_consumer_client"
7
7
  require "karafka/testing/spec_consumer_resume"
8
+ require "karafka/testing/pro/injector"
8
9
  require "karafka/testing/spec_producer_client"
9
10
  require "karafka/testing/minitest/proxy"
10
11
 
@@ -202,6 +203,8 @@ module Karafka
202
203
  # @param metadata [Hash] any metadata we want to dispatch alongside the payload.
203
204
  # Supports an `offset` key to set a custom offset for the message (otherwise
204
205
  # offsets auto-increment from 0).
206
+ # @raise [Karafka::Testing::Errors::TopicNotDefinedError] raised when the target topic
207
+ # cannot be resolved from the message metadata, the consumer or the defined consumers
205
208
  def _karafka_produce(payload, metadata = {})
206
209
  # Extract offset before passing to WaterDrop since it is not a valid
207
210
  # WaterDrop message attribute (Kafka assigns offsets, not producers). Kept per thread, as
@@ -216,6 +219,16 @@ module Karafka
216
219
  last_consumer = @_karafka_consumer_mappings&.values&.last
217
220
  last_consumer&.topic&.name
218
221
  end
222
+
223
+ # Without a topic, WaterDrop would fail with a confusing `MessageInvalidError`
224
+ unless topic_name
225
+ raise(
226
+ Errors::TopicNotDefinedError,
227
+ "Cannot resolve the target topic. Pass `topic:` to `produce` or use " \
228
+ "`@karafka.consumer_for(:topic_name)` to define a consumer first."
229
+ )
230
+ end
231
+
219
232
  Karafka.producer.produce_sync(
220
233
  {
221
234
  topic: topic_name,
@@ -247,6 +260,18 @@ module Karafka
247
260
  @_karafka_consumer_client.resumes
248
261
  end
249
262
 
263
+ # @return [Array<Hash>] messages marked as consumed by the consumers
264
+ def _karafka_marks
265
+ @_karafka_consumer_client.marks
266
+ end
267
+
268
+ # Sets the raw offset metadata the consumers read as committed
269
+ #
270
+ # @param metadata [String, nil] raw offset metadata
271
+ def _karafka_offset_metadata=(metadata)
272
+ @_karafka_consumer_client.offset_metadata = metadata
273
+ end
274
+
250
275
  # Produces message to a specific consumer instance
251
276
  # Use when testing multiple consumers for the same topic
252
277
  #
@@ -359,6 +384,7 @@ module Karafka
359
384
  expansions = processing_cfg.consumer_groups.expansions_selector.find(topic)
360
385
  expansions.each { |expansion| @consumer.singleton_class.include(expansion) }
361
386
  @consumer.singleton_class.prepend(Testing::SpecConsumerResume)
387
+ Testing::Pro::Injector.inject(@consumer, topic)
362
388
  @_karafka_consumer_mappings[topic.id] = @consumer
363
389
  @consumer
364
390
  end
@@ -50,6 +50,19 @@ module Karafka
50
50
  def resumes
51
51
  @minitest_example._karafka_resumes
52
52
  end
53
+
54
+ # @return [Array<Hash>] messages marked as consumed by the consumers, each with the
55
+ # `topic`, `partition`, `offset` and offset `metadata`
56
+ def marks
57
+ @minitest_example._karafka_marks
58
+ end
59
+
60
+ # Sets the raw offset metadata the consumers read as committed (Pro)
61
+ #
62
+ # @param metadata [String, nil] raw offset metadata, deserialized as the topic defines
63
+ def offset_metadata=(metadata)
64
+ @minitest_example._karafka_offset_metadata = metadata
65
+ end
53
66
  end
54
67
  end
55
68
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "karafka/testing/pro/spec_offset_metadata"
4
+
5
+ module Karafka
6
+ module Testing
7
+ # Spec replacements for Pro consumer features that need a running Kafka client
8
+ module Pro
9
+ # Prepends the Pro spec extensions to consumers built in specs
10
+ module Injector
11
+ # Pro spec extensions with the topic predicate that enables each of them
12
+ EXTENSIONS = {
13
+ SpecOffsetMetadata => :offset_metadata?
14
+ }.freeze
15
+
16
+ private_constant :EXTENSIONS
17
+
18
+ class << self
19
+ # Prepends the extensions of the features the topic uses when Pro is loaded
20
+ #
21
+ # @param consumer [Karafka::BaseConsumer] consumer built for specs
22
+ # @param topic [Karafka::Routing::Topic] topic of the consumer
23
+ def inject(consumer, topic)
24
+ return unless Karafka.pro?
25
+
26
+ EXTENSIONS.each do |extension, enabled|
27
+ next unless topic.public_send(enabled)
28
+
29
+ consumer.singleton_class.prepend(extension)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Karafka
4
+ module Testing
5
+ module Pro
6
+ # Reads the offset metadata from the spec client. In a real run the Pro offset metadata
7
+ # fetcher reads it from Kafka through a registered client, which does not exist in specs.
8
+ module SpecOffsetMetadata
9
+ # @param cache [Boolean] ignored, as the spec client holds the metadata in memory
10
+ # @return [Object, false] deserialized offset metadata or false if the partition was
11
+ # revoked
12
+ def offset_metadata(cache: true)
13
+ return false if revoked?
14
+
15
+ topic.offset_metadata.deserializer.call(client.offset_metadata)
16
+ end
17
+
18
+ alias_method :committed_offset_metadata, :offset_metadata
19
+ end
20
+ end
21
+ end
22
+ end
@@ -6,6 +6,7 @@ require "karafka/testing/errors"
6
6
  require "karafka/testing/helpers"
7
7
  require "karafka/testing/spec_consumer_client"
8
8
  require "karafka/testing/spec_consumer_resume"
9
+ require "karafka/testing/pro/injector"
9
10
  require "karafka/testing/spec_producer_client"
10
11
  require "karafka/testing/rspec/proxy"
11
12
 
@@ -197,6 +198,8 @@ module Karafka
197
198
  # @param metadata [Hash] any metadata we want to dispatch alongside the payload.
198
199
  # Supports an `offset` key to set a custom offset for the message (otherwise
199
200
  # offsets auto-increment from 0).
201
+ # @raise [Karafka::Testing::Errors::TopicNotDefinedError] raised when the target topic
202
+ # cannot be resolved from the message metadata, the consumer or the defined consumers
200
203
  def _karafka_produce(payload, metadata = {})
201
204
  # Extract offset before passing to WaterDrop since it is not a valid
202
205
  # WaterDrop message attribute (Kafka assigns offsets, not producers). Kept per thread, as
@@ -213,6 +216,16 @@ module Karafka
213
216
  last_consumer = @_karafka_consumer_mappings&.values&.last
214
217
  last_consumer&.topic&.name
215
218
  end
219
+
220
+ # Without a topic, WaterDrop would fail with a confusing `MessageInvalidError`
221
+ unless topic_name
222
+ raise(
223
+ Errors::TopicNotDefinedError,
224
+ "Cannot resolve the target topic. Pass `topic:` to `produce` or use " \
225
+ "`karafka.consumer_for(:topic_name)` to define a consumer first."
226
+ )
227
+ end
228
+
216
229
  Karafka.producer.produce_sync(
217
230
  {
218
231
  topic: topic_name,
@@ -238,6 +251,18 @@ module Karafka
238
251
  _karafka_consumer_client.resumes
239
252
  end
240
253
 
254
+ # @return [Array<Hash>] messages marked as consumed by the consumers
255
+ def _karafka_marks
256
+ _karafka_consumer_client.marks
257
+ end
258
+
259
+ # Sets the raw offset metadata the consumers read as committed
260
+ #
261
+ # @param metadata [String, nil] raw offset metadata
262
+ def _karafka_offset_metadata=(metadata)
263
+ _karafka_consumer_client.offset_metadata = metadata
264
+ end
265
+
241
266
  # Produces message to a specific consumer instance
242
267
  # Use when testing multiple consumers for the same topic
243
268
  #
@@ -379,6 +404,7 @@ module Karafka
379
404
  expansions = processing_cfg.consumer_groups.expansions_selector.find(topic)
380
405
  expansions.each { |expansion| consumer.singleton_class.include(expansion) }
381
406
  consumer.singleton_class.prepend(Testing::SpecConsumerResume)
407
+ Testing::Pro::Injector.inject(consumer, topic)
382
408
 
383
409
  @_karafka_consumer_mappings[topic.id] = consumer
384
410
  consumer
@@ -50,6 +50,19 @@ module Karafka
50
50
  def resumes
51
51
  @rspec_example._karafka_resumes
52
52
  end
53
+
54
+ # @return [Array<Hash>] messages marked as consumed by the consumers, each with the
55
+ # `topic`, `partition`, `offset` and offset `metadata`
56
+ def marks
57
+ @rspec_example._karafka_marks
58
+ end
59
+
60
+ # Sets the raw offset metadata the consumers read as committed (Pro)
61
+ #
62
+ # @param metadata [String, nil] raw offset metadata, deserialized as the topic defines
63
+ def offset_metadata=(metadata)
64
+ @rspec_example._karafka_offset_metadata = metadata
65
+ end
53
66
  end
54
67
  end
55
68
  end
@@ -14,15 +14,22 @@ module Karafka
14
14
  # `partition`
15
15
  attr_reader :resumes
16
16
 
17
- # Builds a client with no pauses or resumes recorded
17
+ # @return [Array<Hash>] messages marked as consumed on this client, each with the `topic`,
18
+ # `partition`, `offset` and offset `metadata`
19
+ attr_reader :marks
20
+
21
+ # @return [String, nil] raw offset metadata the consumers read as committed
22
+ attr_accessor :offset_metadata
23
+
24
+ # Builds a client with no pauses, resumes or marks recorded and no offset metadata
18
25
  def initialize
19
26
  @pauses = []
20
27
  @resumes = []
28
+ @marks = []
29
+ @offset_metadata = nil
21
30
  end
22
31
 
23
32
  %i[
24
- mark_as_consumed
25
- mark_as_consumed!
26
33
  commit_offsets
27
34
  commit_offsets!
28
35
  seek
@@ -31,25 +38,42 @@ module Karafka
31
38
  define_method(caught_delegator) { |*| true }
32
39
  end
33
40
 
41
+ # Records the mark instead of storing the offset
42
+ #
43
+ # @param message [Karafka::Messages::Message] message marked as consumed
44
+ # @param metadata [String, nil] offset metadata or nil if none
45
+ # @return [true]
46
+ def mark_as_consumed(message, metadata = nil)
47
+ @marks << {
48
+ topic: message.topic,
49
+ partition: message.partition,
50
+ offset: message.offset,
51
+ metadata: metadata
52
+ }
53
+ true
54
+ end
55
+
56
+ alias_method :mark_as_consumed!, :mark_as_consumed
57
+
34
58
  # Records the pause instead of pausing the partition
35
59
  #
36
- # @param topic [String] topic name
60
+ # @param topic_name [String] topic name
37
61
  # @param partition [Integer] partition
38
62
  # @param offset [Integer, nil] offset to continue from or nil to keep the current position
39
63
  # @param timeout [Integer] pause time in milliseconds
40
64
  # @return [true]
41
- def pause(topic, partition, offset = nil, timeout = 0)
42
- @pauses << { topic: topic, partition: partition, offset: offset, timeout: timeout }
65
+ def pause(topic_name, partition, offset = nil, timeout = 0)
66
+ @pauses << { topic: topic_name, partition: partition, offset: offset, timeout: timeout }
43
67
  true
44
68
  end
45
69
 
46
70
  # Records the resume instead of resuming the partition
47
71
  #
48
- # @param topic [String] topic name
72
+ # @param topic_name [String] topic name
49
73
  # @param partition [Integer] partition
50
74
  # @return [true]
51
- def resume(topic, partition)
52
- @resumes << { topic: topic, partition: partition }
75
+ def resume(topic_name, partition)
76
+ @resumes << { topic: topic_name, partition: partition }
53
77
  true
54
78
  end
55
79
 
@@ -13,6 +13,7 @@ module Karafka
13
13
  super(nil)
14
14
  @example = example
15
15
  @mutex = Mutex.new
16
+ @transaction_marks = []
16
17
  end
17
18
 
18
19
  # "Produces" message to Kafka. That is, it acknowledges it locally, adds it to the internal
@@ -31,19 +32,57 @@ module Karafka
31
32
  @mutex.synchronize { super }
32
33
  end
33
34
 
34
- # Finishes given level of transaction
35
+ # Keeps the offsets marked in the transaction until it is committed
36
+ #
37
+ # @param consumer [#marks] client of the consumer that marked the offsets
38
+ # @param tpl [Rdkafka::Consumer::TopicPartitionList] next offsets to process
39
+ # @param _timeout [Integer] timeout in milliseconds
40
+ def send_offsets_to_transaction(consumer, tpl, _timeout)
41
+ @mutex.synchronize do
42
+ tpl.to_h.each do |topic_name, partitions|
43
+ partitions.each do |partition|
44
+ @transaction_marks << [
45
+ consumer,
46
+ {
47
+ topic: topic_name,
48
+ partition: partition.partition,
49
+ offset: partition.offset - 1,
50
+ metadata: partition.metadata
51
+ }
52
+ ]
53
+ end
54
+ end
55
+ end
56
+
57
+ nil
58
+ end
59
+
60
+ # Finishes given level of transaction and records its marks on the consumer clients
35
61
  def commit_transaction
36
- @mutex.synchronize { super }
62
+ @mutex.synchronize do
63
+ super
64
+
65
+ @transaction_marks.each { |consumer, mark| consumer.marks << mark }
66
+ @transaction_marks.clear
67
+ end
37
68
  end
38
69
 
39
70
  # Aborts the transaction
40
71
  def abort_transaction
41
- @mutex.synchronize { super }
72
+ @mutex.synchronize do
73
+ super
74
+
75
+ @transaction_marks.clear
76
+ end
42
77
  end
43
78
 
44
79
  # Clears internal buffer. Used in between specs so messages do not leak out
45
80
  def reset
46
- @mutex.synchronize { super }
81
+ @mutex.synchronize do
82
+ super
83
+
84
+ @transaction_marks.clear
85
+ end
47
86
  end
48
87
  end
49
88
  end
@@ -3,6 +3,6 @@
3
3
  module Karafka
4
4
  module Testing
5
5
  # Current version of gem. It should match Karafka framework version
6
- VERSION = "2.6.4"
6
+ VERSION = "2.6.5"
7
7
  end
8
8
  end
data/renovate.json CHANGED
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "bundler": {
3
- "managerFilePatterns": ["/(^|/)Gemfile\\.lint$/"]
3
+ "managerFilePatterns": [
4
+ "/(^|/)Gemfile\\.lint$/"
5
+ ]
4
6
  },
5
7
  "$schema": "https://docs.renovatebot.com/renovate-schema.json",
6
8
  "extends": [
@@ -44,5 +46,11 @@
44
46
  "schedule": [
45
47
  "before 4am on the first day of the month"
46
48
  ]
49
+ },
50
+ "osvVulnerabilityAlerts": true,
51
+ "vulnerabilityAlerts": {
52
+ "labels": [
53
+ "security"
54
+ ]
47
55
  }
48
56
  }
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.6.4
4
+ version: 2.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Mensfeld
@@ -55,7 +55,9 @@ files:
55
55
  - ".github/ISSUE_TEMPLATE/bug_report.md"
56
56
  - ".github/ISSUE_TEMPLATE/feature_request.md"
57
57
  - ".github/workflows/ci.yml"
58
+ - ".github/workflows/ci_changes.yml"
58
59
  - ".github/workflows/push.yml"
60
+ - ".github/workflows/sigstore_check.yml"
59
61
  - ".github/workflows/trigger-wiki-refresh.yml"
60
62
  - ".github/workflows/verify-action-pins.yml"
61
63
  - ".gitignore"
@@ -63,7 +65,6 @@ files:
63
65
  - ".ruby-gemset"
64
66
  - ".ruby-version"
65
67
  - ".yard-lint.yml"
66
- - 2.0-Upgrade.md
67
68
  - CHANGELOG.md
68
69
  - Gemfile
69
70
  - Gemfile.lint
@@ -79,6 +80,8 @@ files:
79
80
  - lib/karafka/testing/helpers.rb
80
81
  - lib/karafka/testing/minitest/helpers.rb
81
82
  - lib/karafka/testing/minitest/proxy.rb
83
+ - lib/karafka/testing/pro/injector.rb
84
+ - lib/karafka/testing/pro/spec_offset_metadata.rb
82
85
  - lib/karafka/testing/rspec/helpers.rb
83
86
  - lib/karafka/testing/rspec/proxy.rb
84
87
  - lib/karafka/testing/spec_consumer_client.rb
data/2.0-Upgrade.md DELETED
@@ -1,60 +0,0 @@
1
- # Welcome to Karafka-Testing 2.0!
2
-
3
- Karafka-Testing 2.0 some breaking changes in the way consumer builder and message publishing is done.
4
-
5
- ## Upgrade
6
-
7
- Please upgrade your application to `Karafka 2.0` first.
8
-
9
- - Replace `#karafka_consumer_for` in your specs with `#karafka.consumer_for`
10
- - Replace `#publish_for_karafka` in your specs with `#karafka.produce`
11
-
12
- And that's all!
13
-
14
- Below you can find same example written for Karafka `2.0` and `1.4`.
15
-
16
- ### Karafka 2.0
17
-
18
- ```ruby
19
- RSpec.describe CountersConsumer do
20
- subject(:consumer) { karafka.consumer_for(:counters) }
21
-
22
- let(:nr1_value) { rand }
23
- let(:nr2_value) { rand }
24
- let(:sum) { nr1_value + nr2_value }
25
-
26
- before do
27
- karafka.produce({ 'number' => nr1_value }.to_json)
28
- karafka.produce({ 'number' => nr2_value }.to_json, partition: 2)
29
- allow(Karafka.logger).to receive(:info)
30
- end
31
-
32
- it 'expects to log a proper message' do
33
- expect(Karafka.logger).to receive(:info).with("Sum of 2 elements equals to: #{sum}")
34
- consumer.consume
35
- end
36
- end
37
- ```
38
-
39
- ### Karafka 1.4
40
-
41
- ```ruby
42
- RSpec.describe InlineBatchConsumer do
43
- subject(:consumer) { karafka_consumer_for(:counters) }
44
-
45
- let(:nr1_value) { rand }
46
- let(:nr2_value) { rand }
47
- let(:sum) { nr1_value + nr2_value }
48
-
49
- before do
50
- publish_for_karafka({ 'number' => nr1_value }.to_json)
51
- publish_for_karafka({ 'number' => nr2_value }.to_json, partition: 2)
52
- allow(Karafka.logger).to receive(:info)
53
- end
54
-
55
- it 'expects to log a proper message' do
56
- expect(Karafka.logger).to receive(:info).with("Sum of 2 elements equals to: #{sum}")
57
- consumer.consume
58
- end
59
- end
60
- ```