karafka-testing 2.6.2 → 2.6.4
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 +4 -4
- data/.github/workflows/ci.yml +10 -11
- data/.github/workflows/push.yml +2 -2
- data/.github/workflows/verify-action-pins.yml +8 -1
- data/.rubocop.yml +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +18 -2
- data/Gemfile +2 -1
- data/Gemfile.lock +10 -11
- data/README.md +4 -4
- data/karafka-testing.gemspec +1 -1
- data/lib/karafka/testing/helpers.rb +17 -3
- data/lib/karafka/testing/minitest/helpers.rb +117 -28
- data/lib/karafka/testing/minitest/proxy.rb +14 -0
- data/lib/karafka/testing/rspec/helpers.rb +107 -31
- data/lib/karafka/testing/rspec/proxy.rb +14 -0
- data/lib/karafka/testing/spec_consumer_client.rb +36 -0
- data/lib/karafka/testing/spec_consumer_resume.rb +22 -0
- data/lib/karafka/testing/spec_producer_client.rb +32 -6
- data/lib/karafka/testing/version.rb +1 -1
- data/lib/karafka/testing.rb +6 -0
- data/package-lock.json +9 -9
- data/renovate.json +3 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '08d462de8e80468e64eef6f71a2c17e47a4a1ca597dd9e290feaf0ccfc5f49d6'
|
|
4
|
+
data.tar.gz: d7971f8a44ed5f442de78467dce1bbd243596c5bc2bd05f2046b7778b3dfa2c7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b94d77ccaed5dbb9c8d3fe1742e01b622b7a81a5fa9a3666584240217ef8100c12f76d1043cee6e313506d1e082c31becc1a11af7f7bcfea1d7f9edcc6c44dd0
|
|
7
|
+
data.tar.gz: a8682074bb85e6f5c1be92d3207933395311882f9c6fbebf446ea558b53e57b7f12a2ff5c2fc3a6142ce3adf6b6903ef740a8dc304a13ae728f72d2f261c956a
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -24,7 +24,6 @@ jobs:
|
|
|
24
24
|
- '4.0'
|
|
25
25
|
- '3.4'
|
|
26
26
|
- '3.3'
|
|
27
|
-
- '3.2'
|
|
28
27
|
include:
|
|
29
28
|
- ruby: '4.0'
|
|
30
29
|
coverage: 'true'
|
|
@@ -37,7 +36,7 @@ jobs:
|
|
|
37
36
|
run: "[ -e $APT_DEPS ] || sudo apt-get install -y --no-install-recommends $APT_DEPS"
|
|
38
37
|
|
|
39
38
|
- name: Set up Ruby
|
|
40
|
-
uses: ruby/setup-ruby@
|
|
39
|
+
uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
41
40
|
with:
|
|
42
41
|
ruby-version: ${{matrix.ruby}}
|
|
43
42
|
bundler: 'latest'
|
|
@@ -67,9 +66,9 @@ jobs:
|
|
|
67
66
|
with:
|
|
68
67
|
fetch-depth: 0
|
|
69
68
|
- name: Set up Ruby
|
|
70
|
-
uses: ruby/setup-ruby@
|
|
69
|
+
uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
71
70
|
with:
|
|
72
|
-
ruby-version: '4.0.
|
|
71
|
+
ruby-version: '4.0.9'
|
|
73
72
|
bundler-cache: true
|
|
74
73
|
- name: Run rubocop
|
|
75
74
|
run: bundle exec rubocop
|
|
@@ -82,9 +81,9 @@ jobs:
|
|
|
82
81
|
steps:
|
|
83
82
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
84
83
|
- name: Set up Ruby
|
|
85
|
-
uses: ruby/setup-ruby@
|
|
84
|
+
uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
86
85
|
with:
|
|
87
|
-
ruby-version: '4.0.
|
|
86
|
+
ruby-version: '4.0.9'
|
|
88
87
|
bundler-cache: true
|
|
89
88
|
- run: bundle exec yard-lint lib/
|
|
90
89
|
|
|
@@ -96,9 +95,9 @@ jobs:
|
|
|
96
95
|
with:
|
|
97
96
|
fetch-depth: 0
|
|
98
97
|
- name: Set up Ruby
|
|
99
|
-
uses: ruby/setup-ruby@
|
|
98
|
+
uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
100
99
|
with:
|
|
101
|
-
ruby-version: '4.0.
|
|
100
|
+
ruby-version: '4.0.9'
|
|
102
101
|
bundler: 'latest'
|
|
103
102
|
- name: Install dependencies
|
|
104
103
|
run: bundle install --jobs 4 --retry 3
|
|
@@ -113,9 +112,9 @@ jobs:
|
|
|
113
112
|
with:
|
|
114
113
|
fetch-depth: 0
|
|
115
114
|
- name: Set up Ruby
|
|
116
|
-
uses: ruby/setup-ruby@
|
|
115
|
+
uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
117
116
|
with:
|
|
118
|
-
ruby-version: '4.0.
|
|
117
|
+
ruby-version: '4.0.9'
|
|
119
118
|
bundler: 'latest'
|
|
120
119
|
- name: Install dependencies
|
|
121
120
|
run: bundle install --jobs 4 --retry 3
|
|
@@ -130,7 +129,7 @@ jobs:
|
|
|
130
129
|
with:
|
|
131
130
|
fetch-depth: 0
|
|
132
131
|
- name: Set up Node.js
|
|
133
|
-
uses: actions/setup-node@
|
|
132
|
+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
|
|
134
133
|
with:
|
|
135
134
|
node-version: '20'
|
|
136
135
|
cache: 'npm'
|
data/.github/workflows/push.yml
CHANGED
|
@@ -24,7 +24,7 @@ jobs:
|
|
|
24
24
|
fetch-depth: 0
|
|
25
25
|
|
|
26
26
|
- name: Set up Ruby
|
|
27
|
-
uses: ruby/setup-ruby@
|
|
27
|
+
uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
|
|
28
28
|
with:
|
|
29
29
|
bundler-cache: false
|
|
30
30
|
|
|
@@ -32,4 +32,4 @@ jobs:
|
|
|
32
32
|
run: |
|
|
33
33
|
bundle install --jobs 4 --retry 3
|
|
34
34
|
|
|
35
|
-
- uses: rubygems/release-gem@
|
|
35
|
+
- uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
|
|
@@ -10,7 +10,14 @@ jobs:
|
|
|
10
10
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
11
11
|
- name: Check SHA pins
|
|
12
12
|
run: |
|
|
13
|
-
|
|
13
|
+
# Every remote action must be pinned to a full 40-character commit SHA. Local actions
|
|
14
|
+
# (./) and docker:// images are not checked.
|
|
15
|
+
unpinned=$(grep -E -rn --include="*.yml" --include="*.yaml" \
|
|
16
|
+
"^[[:space:]]*(-[[:space:]]+)?uses:[[:space:]]*['\"]?[^.'\"[:space:]]" .github/workflows/ \
|
|
17
|
+
| grep -Ev "uses:[[:space:]]*['\"]?docker://" \
|
|
18
|
+
| grep -Ev "@[0-9a-f]{40}['\"]?([[:space:]]|$)" || true)
|
|
19
|
+
if [ -n "$unpinned" ]; then
|
|
20
|
+
echo "$unpinned"
|
|
14
21
|
echo "::error::Actions should use SHA pins, not tags or branch names"
|
|
15
22
|
exit 1
|
|
16
23
|
fi
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
4.0.
|
|
1
|
+
4.0.9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
# Karafka Testing Changelog
|
|
2
2
|
|
|
3
|
+
## 2.6.4 (2026-11-04)
|
|
4
|
+
- [Feature] Record consumer pauses and resumes. Assert them with `karafka.pauses` and `karafka.resumes` (#214).
|
|
5
|
+
- [Maintenance] Declare `rake` in the `Gemfile`. It came only through `karafka-rdkafka`, which will stop depending on it for precompiled platform gems.
|
|
6
|
+
- [Fix] Give each consumer in one test a batch of only its own messages. Several consumers no longer share one batch, and offsets count per consumer.
|
|
7
|
+
- [Fix] Stub `pause` and `resume` on the spec consumer client. Consumers that call `#pause` no longer fail with `NoMethodError`.
|
|
8
|
+
- [Fix] Make `Minitest::Spec.include(Karafka::Testing::Minitest::Helpers)` work. Specs no longer fail with `ArgumentError: wrong number of arguments`.
|
|
9
|
+
- [Fix] Make producing in a test safe from many threads. On JRuby, messages produced from 2+ threads at once were lost or got duplicate offsets.
|
|
10
|
+
|
|
11
|
+
## 2.6.3 (2026-10-25)
|
|
12
|
+
- **[EOL]** Drop Ruby 3.2 support; require Ruby 3.3+.
|
|
13
|
+
- [Maintenance] Align internal naming and YARD docs with the Karafka ecosystem conventions.
|
|
14
|
+
- [Maintenance] Drop `ostruct` from the test dependencies.
|
|
15
|
+
- [Fix] Make the Minitest helpers work without Mocha. They no longer fail with `undefined method 'stubs'` when Mocha is not loaded, and still use Mocha when it is.
|
|
16
|
+
- [Fix] Set `metadata.message` on test messages and give `headers: nil` empty `raw_headers`, as a real run does.
|
|
17
|
+
- [Fix] Make `produce(partition: N)` set N as the batch and consumer partition, not only the message one. With mixed partitions the first message decides.
|
|
18
|
+
|
|
3
19
|
## 2.6.2 (2026-08-13)
|
|
4
|
-
- [Fix] Read the
|
|
20
|
+
- [Fix] Read the `strategy_selector` and `expansions_selector` settings from their new `consumer_groups` location. Requires Karafka `>= 2.6.0`.
|
|
5
21
|
|
|
6
22
|
## 2.6.1 (2026-08-05)
|
|
7
23
|
- [Fix] Fix the dependency constraints.
|
|
@@ -18,8 +34,8 @@
|
|
|
18
34
|
- [Fix] Fix consumer mapping collision when multiple consumer groups listen to the same topic.
|
|
19
35
|
|
|
20
36
|
## 2.5.4 (2025-10-03)
|
|
21
|
-
- [Fix] Topic present in second dynamically built subscription group not available for dispatch (#291).
|
|
22
37
|
- **[EOL]** Remove Ruby `3.1` according to the EOL schedule.
|
|
38
|
+
- [Fix] Topic present in second dynamically built subscription group not available for dispatch (#291).
|
|
23
39
|
|
|
24
40
|
## 2.5.3 (2025-08-12)
|
|
25
41
|
- [Fix] Allow testing consumers with Pro expansions (i.e. Piping).
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
karafka-testing (2.6.
|
|
4
|
+
karafka-testing (2.6.4)
|
|
5
5
|
karafka (>= 2.6.0.rc2, < 2.7.0)
|
|
6
6
|
waterdrop (>= 2.10.0)
|
|
7
7
|
|
|
@@ -12,22 +12,22 @@ GEM
|
|
|
12
12
|
drb (2.2.3)
|
|
13
13
|
ffi (1.17.4)
|
|
14
14
|
ffi (1.17.4-x86_64-linux-gnu)
|
|
15
|
-
json (
|
|
16
|
-
karafka (2.6.
|
|
15
|
+
json (3.1.0)
|
|
16
|
+
karafka (2.6.1)
|
|
17
17
|
karafka-core (>= 2.6.2, < 2.7.0)
|
|
18
18
|
karafka-rdkafka (>= 0.28.0)
|
|
19
19
|
waterdrop (>= 2.10.2, < 3.0.0)
|
|
20
20
|
zeitwerk (~> 2.3)
|
|
21
|
-
karafka-core (2.6.
|
|
21
|
+
karafka-core (2.6.6)
|
|
22
22
|
karafka-rdkafka (>= 0.20.0)
|
|
23
23
|
logger (>= 1.6.0)
|
|
24
|
-
karafka-rdkafka (0.
|
|
24
|
+
karafka-rdkafka (0.31.0)
|
|
25
25
|
ffi (~> 1.17.1)
|
|
26
26
|
json (> 2.0)
|
|
27
27
|
logger
|
|
28
28
|
mini_portile2 (~> 2.6)
|
|
29
29
|
rake (> 12)
|
|
30
|
-
karafka-rdkafka (0.
|
|
30
|
+
karafka-rdkafka (0.31.0-x86_64-linux-gnu)
|
|
31
31
|
ffi (~> 1.17.1)
|
|
32
32
|
json (> 2.0)
|
|
33
33
|
logger
|
|
@@ -40,7 +40,6 @@ GEM
|
|
|
40
40
|
prism (~> 1.5)
|
|
41
41
|
mocha (3.1.0)
|
|
42
42
|
ruby2_keywords (>= 0.0.5)
|
|
43
|
-
ostruct (0.6.3)
|
|
44
43
|
prism (1.9.0)
|
|
45
44
|
rake (13.4.2)
|
|
46
45
|
rspec (3.13.2)
|
|
@@ -57,11 +56,11 @@ GEM
|
|
|
57
56
|
rspec-support (~> 3.13.0)
|
|
58
57
|
rspec-support (3.13.7)
|
|
59
58
|
ruby2_keywords (0.0.5)
|
|
60
|
-
simplecov (1.
|
|
59
|
+
simplecov (1.3.2)
|
|
61
60
|
warning (1.6.0)
|
|
62
|
-
waterdrop (2.10.
|
|
61
|
+
waterdrop (2.10.9)
|
|
63
62
|
karafka-core (>= 2.5.12, < 3.0.0)
|
|
64
|
-
karafka-rdkafka (>= 0.
|
|
63
|
+
karafka-rdkafka (>= 0.31.0)
|
|
65
64
|
zeitwerk (~> 2.3)
|
|
66
65
|
zeitwerk (2.8.3)
|
|
67
66
|
|
|
@@ -73,7 +72,7 @@ DEPENDENCIES
|
|
|
73
72
|
karafka-testing!
|
|
74
73
|
minitest
|
|
75
74
|
mocha
|
|
76
|
-
|
|
75
|
+
rake
|
|
77
76
|
rspec
|
|
78
77
|
simplecov
|
|
79
78
|
warning
|
data/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# Karafka Testing library
|
|
2
2
|
|
|
3
|
-
[](https://github.com/karafka/karafka-testing/actions
|
|
4
|
-
[](
|
|
3
|
+
[](https://github.com/karafka/karafka-testing/actions/workflows/ci.yml)
|
|
4
|
+
[](https://badge.fury.io/rb/karafka-testing)
|
|
5
5
|
[](https://slack.karafka.io)
|
|
6
6
|
|
|
7
|
-
Karafka-Testing is a library that provides RSpec helpers
|
|
7
|
+
Karafka-Testing is a library that provides RSpec and Minitest helpers that make testing of Karafka consumers and producers much easier.
|
|
8
8
|
|
|
9
9
|
## Installation and usage
|
|
10
10
|
|
|
@@ -14,6 +14,6 @@ Karafka-Testing is a library that provides RSpec helpers, to make testing of Kar
|
|
|
14
14
|
|
|
15
15
|
First, thank you for considering contributing to the Karafka ecosystem! It's people like you that make the open source community such a great community!
|
|
16
16
|
|
|
17
|
-
Each pull request must pass all the
|
|
17
|
+
Each pull request must pass all the tests, including integration tests, and meet our quality requirements.
|
|
18
18
|
|
|
19
19
|
Fork it, update and wait for the Github Actions results.
|
data/karafka-testing.gemspec
CHANGED
|
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
|
22
22
|
spec.add_dependency "karafka", ">= 2.6.0.rc2", "< 2.7.0"
|
|
23
23
|
spec.add_dependency "waterdrop", ">= 2.10.0"
|
|
24
24
|
|
|
25
|
-
spec.required_ruby_version = ">= 3.
|
|
25
|
+
spec.required_ruby_version = ">= 3.3.0"
|
|
26
26
|
|
|
27
27
|
spec.metadata = {
|
|
28
28
|
"homepage_uri" => "https://karafka.io",
|
|
@@ -8,19 +8,19 @@ module Karafka
|
|
|
8
8
|
# Finds all the routing topics matching requested topic within all topics or within
|
|
9
9
|
# provided consumer group based on name
|
|
10
10
|
#
|
|
11
|
-
# @param
|
|
11
|
+
# @param topic_name [String] requested topic name
|
|
12
12
|
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
|
13
13
|
# @return [Array<Karafka::Routing::Topic>] all matching topics
|
|
14
14
|
#
|
|
15
15
|
# @note Since we run the lookup on subscription groups, the search will automatically
|
|
16
16
|
# expand with matching patterns
|
|
17
|
-
def karafka_consumer_find_candidate_topics(
|
|
17
|
+
def karafka_consumer_find_candidate_topics(topic_name, requested_consumer_group)
|
|
18
18
|
subscription_groups = karafka_consumer_find_subscription_groups(requested_consumer_group)
|
|
19
19
|
|
|
20
20
|
candidate_topics = []
|
|
21
21
|
|
|
22
22
|
subscription_groups.each do |group|
|
|
23
|
-
topic = group.topics.find(
|
|
23
|
+
topic = group.topics.find(topic_name.to_s)
|
|
24
24
|
|
|
25
25
|
next unless topic
|
|
26
26
|
|
|
@@ -36,6 +36,20 @@ module Karafka
|
|
|
36
36
|
candidate_topics.uniq { |topic| topic.subscription_group.name }
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
# Builds a coordinator for given topic partition, as a consumer gets in a real run
|
|
40
|
+
#
|
|
41
|
+
# @param topic [Karafka::Routing::Topic] topic of the consumer
|
|
42
|
+
# @param partition [Integer] partition the consumer works on
|
|
43
|
+
# @return [Karafka::Processing::ConsumerGroups::Coordinator] coordinator for the partition
|
|
44
|
+
def karafka_build_coordinator(topic, partition)
|
|
45
|
+
coordinator = Karafka::Processing::CoordinatorsBuffer
|
|
46
|
+
.new(Karafka::Routing::Topics.new([topic]))
|
|
47
|
+
.find_or_create(topic.name, partition)
|
|
48
|
+
|
|
49
|
+
coordinator.seek_offset = 0
|
|
50
|
+
coordinator
|
|
51
|
+
end
|
|
52
|
+
|
|
39
53
|
# Finds subscription groups from the requested consumer group or selects all if no
|
|
40
54
|
# consumer group specified
|
|
41
55
|
# @param requested_consumer_group [String] requested consumer group or nil to look in all
|
|
@@ -4,6 +4,7 @@ require "karafka/testing"
|
|
|
4
4
|
require "karafka/testing/errors"
|
|
5
5
|
require "karafka/testing/helpers"
|
|
6
6
|
require "karafka/testing/spec_consumer_client"
|
|
7
|
+
require "karafka/testing/spec_consumer_resume"
|
|
7
8
|
require "karafka/testing/spec_producer_client"
|
|
8
9
|
require "karafka/testing/minitest/proxy"
|
|
9
10
|
|
|
@@ -39,12 +40,24 @@ module Karafka
|
|
|
39
40
|
|
|
40
41
|
@_karafka_consumer_mappings = {}
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
# Not a Mocha stub even when Mocha is loaded: Mocha records every call in an
|
|
44
|
+
# unsynchronized array, which breaks when code under test produces from many threads
|
|
45
|
+
producer_client = @_karafka_producer_client
|
|
46
|
+
Karafka.producer.define_singleton_method(:client) { producer_client }
|
|
47
|
+
@_karafka_producer_client_stubbed = true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
teardown_flow = lambda do
|
|
51
|
+
next unless @_karafka_producer_client_stubbed
|
|
52
|
+
|
|
53
|
+
Karafka.producer.singleton_class.remove_method(:client)
|
|
54
|
+
@_karafka_producer_client_stubbed = false
|
|
43
55
|
end
|
|
44
56
|
|
|
45
57
|
if base.to_s == "Minitest::Spec"
|
|
58
|
+
# `before` passes the test to its block, which the lambda does not take
|
|
46
59
|
base.class_eval do
|
|
47
|
-
before(&eval_flow)
|
|
60
|
+
before { instance_exec(&eval_flow) }
|
|
48
61
|
end
|
|
49
62
|
elsif base.respond_to?(:setup) && base.method(:setup).arity != 0
|
|
50
63
|
base.class_eval do
|
|
@@ -60,29 +73,44 @@ module Karafka
|
|
|
60
73
|
|
|
61
74
|
base.prepend(setup_mod)
|
|
62
75
|
end
|
|
76
|
+
|
|
77
|
+
# `after_teardown` runs even when a test overrides `teardown` without calling `super`
|
|
78
|
+
teardown_mod = Module.new do
|
|
79
|
+
define_method(:after_teardown) do
|
|
80
|
+
super()
|
|
81
|
+
ensure
|
|
82
|
+
instance_exec(&teardown_flow)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
base.prepend(teardown_mod)
|
|
63
87
|
end
|
|
64
88
|
end
|
|
65
89
|
|
|
66
90
|
# Creates a consumer instance for a given topic
|
|
67
91
|
#
|
|
68
|
-
# @param
|
|
69
|
-
#
|
|
92
|
+
# @param topic_name [String, Symbol] name of the topic for which we want to create a
|
|
93
|
+
# consumer instance
|
|
70
94
|
# @param requested_consumer_group [String, Symbol, nil] optional name of the consumer group
|
|
71
95
|
# if we have multiple consumer groups listening on the same topic
|
|
72
96
|
# @return [Object] Karafka consumer instance
|
|
73
97
|
# @raise [Karafka::Testing::Errors::TopicNotFoundError] raised when we're unable to find
|
|
74
98
|
# topic that was requested
|
|
99
|
+
# @raise [Karafka::Testing::Errors::TopicInManyConsumerGroupsError] raised when the topic is
|
|
100
|
+
# in many consumer groups and no consumer group was requested
|
|
101
|
+
# @raise [Karafka::Testing::Errors::ConsumerGroupNotFoundError] raised when the requested
|
|
102
|
+
# consumer group does not exist
|
|
75
103
|
#
|
|
76
104
|
# @example Creates a consumer instance with settings for `my_requested_topic`
|
|
77
105
|
# consumer = @karafka.consumer_for(:my_requested_topic)
|
|
78
|
-
def _karafka_consumer_for(
|
|
106
|
+
def _karafka_consumer_for(topic_name, requested_consumer_group = nil)
|
|
79
107
|
selected_topics = Testing::Helpers.karafka_consumer_find_candidate_topics(
|
|
80
|
-
|
|
108
|
+
topic_name.to_s,
|
|
81
109
|
requested_consumer_group.to_s
|
|
82
110
|
)
|
|
83
111
|
|
|
84
|
-
raise Errors::TopicInManyConsumerGroupsError,
|
|
85
|
-
raise Errors::TopicNotFoundError,
|
|
112
|
+
raise Errors::TopicInManyConsumerGroupsError, topic_name if selected_topics.size > 1
|
|
113
|
+
raise Errors::TopicNotFoundError, topic_name if selected_topics.empty?
|
|
86
114
|
|
|
87
115
|
_karafka_build_consumer_for(selected_topics.first)
|
|
88
116
|
end
|
|
@@ -96,7 +124,7 @@ module Karafka
|
|
|
96
124
|
# @karafka.produce({ 'hello' => 'world' }.to_json)
|
|
97
125
|
#
|
|
98
126
|
# @example Send a json message to consumer and simulate, that it is partition 6
|
|
99
|
-
# @karafka.produce({ 'hello' => 'world' }.to_json,
|
|
127
|
+
# @karafka.produce({ 'hello' => 'world' }.to_json, partition: 6)
|
|
100
128
|
def _karafka_add_message_to_consumer_if_needed(message)
|
|
101
129
|
consumer_obj = if message[:consumer_group]
|
|
102
130
|
_karafka_find_consumer_for_message(message)
|
|
@@ -118,8 +146,10 @@ module Karafka
|
|
|
118
146
|
return if message[:consumer_group] &&
|
|
119
147
|
message[:consumer_group].to_s != consumer_obj.topic.consumer_group.name
|
|
120
148
|
|
|
149
|
+
batch = _karafka_consumer_batch(consumer_obj)
|
|
150
|
+
|
|
121
151
|
# Build message metadata and copy any metadata that would come from the message
|
|
122
|
-
metadata = _karafka_message_metadata_defaults(consumer_obj)
|
|
152
|
+
metadata = _karafka_message_metadata_defaults(consumer_obj, batch)
|
|
123
153
|
|
|
124
154
|
metadata.keys.each do |key|
|
|
125
155
|
message_key = METADATA_DISPATCH_MAPPINGS.fetch(key, key)
|
|
@@ -129,22 +159,40 @@ module Karafka
|
|
|
129
159
|
metadata[key] = message.fetch(message_key)
|
|
130
160
|
end
|
|
131
161
|
|
|
132
|
-
|
|
133
|
-
|
|
162
|
+
metadata[:raw_headers] ||= {}
|
|
163
|
+
|
|
164
|
+
metadata = Karafka::Messages::Metadata.new(metadata)
|
|
165
|
+
karafka_message = Karafka::Messages::Message.new(
|
|
134
166
|
message[:payload],
|
|
135
|
-
|
|
167
|
+
metadata
|
|
136
168
|
)
|
|
169
|
+
metadata[:message] = karafka_message
|
|
170
|
+
|
|
171
|
+
# Add this message to previously produced messages
|
|
172
|
+
@_karafka_consumer_messages << karafka_message
|
|
173
|
+
batch << karafka_message unless batch.equal?(@_karafka_consumer_messages)
|
|
174
|
+
# A real batch never mixes partitions, so the first message of this consumer decides
|
|
175
|
+
# the partition
|
|
176
|
+
partition = (@_karafka_batch_partitions ||= {})[consumer_obj] ||= karafka_message.partition
|
|
177
|
+
|
|
178
|
+
if consumer_obj.partition != partition
|
|
179
|
+
consumer_obj.coordinator = Testing::Helpers.karafka_build_coordinator(
|
|
180
|
+
consumer_obj.topic,
|
|
181
|
+
partition
|
|
182
|
+
)
|
|
183
|
+
end
|
|
184
|
+
|
|
137
185
|
# Update batch metadata
|
|
138
186
|
batch_metadata = Karafka::Messages::Builders::BatchMetadata.call(
|
|
139
|
-
|
|
187
|
+
batch,
|
|
140
188
|
consumer_obj.topic,
|
|
141
|
-
|
|
189
|
+
partition,
|
|
142
190
|
Time.now
|
|
143
191
|
)
|
|
144
192
|
|
|
145
193
|
# Update consumer messages batch
|
|
146
194
|
consumer_obj.messages = Karafka::Messages::Messages.new(
|
|
147
|
-
|
|
195
|
+
batch,
|
|
148
196
|
batch_metadata
|
|
149
197
|
)
|
|
150
198
|
end
|
|
@@ -156,10 +204,11 @@ module Karafka
|
|
|
156
204
|
# offsets auto-increment from 0).
|
|
157
205
|
def _karafka_produce(payload, metadata = {})
|
|
158
206
|
# Extract offset before passing to WaterDrop since it is not a valid
|
|
159
|
-
# WaterDrop message attribute (Kafka assigns offsets, not producers)
|
|
160
|
-
|
|
207
|
+
# WaterDrop message attribute (Kafka assigns offsets, not producers). Kept per thread, as
|
|
208
|
+
# code under test may produce from many threads at once
|
|
209
|
+
Thread.current[:_karafka_next_offset] = metadata.delete(:offset)
|
|
161
210
|
|
|
162
|
-
|
|
211
|
+
topic_name = if metadata[:topic]
|
|
163
212
|
metadata[:topic]
|
|
164
213
|
elsif defined?(@consumer)
|
|
165
214
|
@consumer.topic.name
|
|
@@ -169,12 +218,12 @@ module Karafka
|
|
|
169
218
|
end
|
|
170
219
|
Karafka.producer.produce_sync(
|
|
171
220
|
{
|
|
172
|
-
topic:
|
|
221
|
+
topic: topic_name,
|
|
173
222
|
payload: payload
|
|
174
223
|
}.merge(metadata)
|
|
175
224
|
)
|
|
176
225
|
ensure
|
|
177
|
-
|
|
226
|
+
Thread.current[:_karafka_next_offset] = nil
|
|
178
227
|
end
|
|
179
228
|
|
|
180
229
|
# @return [Array<Hash>] messages that were produced
|
|
@@ -188,6 +237,16 @@ module Karafka
|
|
|
188
237
|
@_karafka_consumer_messages
|
|
189
238
|
end
|
|
190
239
|
|
|
240
|
+
# @return [Array<Hash>] pauses requested by the consumers
|
|
241
|
+
def _karafka_pauses
|
|
242
|
+
@_karafka_consumer_client.pauses
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# @return [Array<Hash>] resumes of paused consumers
|
|
246
|
+
def _karafka_resumes
|
|
247
|
+
@_karafka_consumer_client.resumes
|
|
248
|
+
end
|
|
249
|
+
|
|
191
250
|
# Produces message to a specific consumer instance
|
|
192
251
|
# Use when testing multiple consumers for the same topic
|
|
193
252
|
#
|
|
@@ -212,6 +271,38 @@ module Karafka
|
|
|
212
271
|
|
|
213
272
|
private
|
|
214
273
|
|
|
274
|
+
# Messages of the batch of a given consumer. The first consumer that gets a message uses
|
|
275
|
+
# the shared buffer, so altering `consumer_messages` alters its batch. Once a second
|
|
276
|
+
# consumer gets one, each consumer gets its own batch, as a real batch holds one topic.
|
|
277
|
+
#
|
|
278
|
+
# @param consumer_obj [Karafka::BaseConsumer] consumer reference
|
|
279
|
+
# @return [Array<Karafka::Messages::Message>] messages of this consumer's batch
|
|
280
|
+
def _karafka_consumer_batch(consumer_obj)
|
|
281
|
+
batches = (@_karafka_consumer_batches ||= {})
|
|
282
|
+
|
|
283
|
+
batches.fetch(consumer_obj) do
|
|
284
|
+
if batches.empty?
|
|
285
|
+
batches[consumer_obj] = @_karafka_consumer_messages
|
|
286
|
+
else
|
|
287
|
+
first_consumer, first_batch = batches.first
|
|
288
|
+
|
|
289
|
+
if first_batch.equal?(@_karafka_consumer_messages)
|
|
290
|
+
own_batch = first_batch.dup
|
|
291
|
+
batches[first_consumer] = own_batch
|
|
292
|
+
|
|
293
|
+
if first_consumer.messages
|
|
294
|
+
first_consumer.messages = Karafka::Messages::Messages.new(
|
|
295
|
+
own_batch,
|
|
296
|
+
first_consumer.messages.metadata
|
|
297
|
+
)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
batches[consumer_obj] = []
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
215
306
|
# Finds a consumer for the given message with backward-compatible fallback
|
|
216
307
|
# @param message [Hash] the message being routed
|
|
217
308
|
# @return [Object, nil] the consumer instance or nil
|
|
@@ -235,14 +326,15 @@ module Karafka
|
|
|
235
326
|
end
|
|
236
327
|
|
|
237
328
|
# @param consumer_obj [Karafka::BaseConsumer] consumer reference
|
|
329
|
+
# @param batch [Array<Karafka::Messages::Message>] messages already given to this consumer
|
|
238
330
|
# @return [Hash] message default options
|
|
239
|
-
def _karafka_message_metadata_defaults(consumer_obj)
|
|
331
|
+
def _karafka_message_metadata_defaults(consumer_obj, batch)
|
|
240
332
|
{
|
|
241
333
|
deserializers: consumer_obj.topic.deserializers,
|
|
242
334
|
timestamp: Time.now,
|
|
243
335
|
raw_headers: {},
|
|
244
336
|
raw_key: nil,
|
|
245
|
-
offset:
|
|
337
|
+
offset: Thread.current[:_karafka_next_offset] || batch.size,
|
|
246
338
|
partition: 0,
|
|
247
339
|
received_at: Time.now,
|
|
248
340
|
topic: consumer_obj.topic.name
|
|
@@ -254,9 +346,6 @@ module Karafka
|
|
|
254
346
|
# @param topic [Karafka::Routing::Topic] topic for which we want to build the consumer
|
|
255
347
|
# @return [Object] karafka consumer
|
|
256
348
|
def _karafka_build_consumer_for(topic)
|
|
257
|
-
coordinators = Karafka::Processing::CoordinatorsBuffer.new(
|
|
258
|
-
Karafka::Routing::Topics.new([topic])
|
|
259
|
-
)
|
|
260
349
|
processing_cfg = Karafka::App.config.internal.processing
|
|
261
350
|
@consumer = topic.consumer.new
|
|
262
351
|
@consumer.producer = Karafka::App.producer
|
|
@@ -264,12 +353,12 @@ module Karafka
|
|
|
264
353
|
strategy = processing_cfg.consumer_groups.strategy_selector.find(topic)
|
|
265
354
|
@consumer.singleton_class.include(strategy)
|
|
266
355
|
@consumer.client = @_karafka_consumer_client
|
|
267
|
-
@consumer.coordinator =
|
|
268
|
-
@consumer.coordinator.seek_offset = 0
|
|
356
|
+
@consumer.coordinator = Testing::Helpers.karafka_build_coordinator(topic, 0)
|
|
269
357
|
# Indicate usage as for tests no direct enqueuing happens
|
|
270
358
|
@consumer.instance_variable_set(:@used, true)
|
|
271
359
|
expansions = processing_cfg.consumer_groups.expansions_selector.find(topic)
|
|
272
360
|
expansions.each { |expansion| @consumer.singleton_class.include(expansion) }
|
|
361
|
+
@consumer.singleton_class.prepend(Testing::SpecConsumerResume)
|
|
273
362
|
@_karafka_consumer_mappings[topic.id] = @consumer
|
|
274
363
|
@consumer
|
|
275
364
|
end
|
|
@@ -12,16 +12,19 @@ module Karafka
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
# Forwards all arguments to `#_karafka_consumer_for`
|
|
15
|
+
# @return [Karafka::BaseConsumer] consumer instance
|
|
15
16
|
def consumer_for(*)
|
|
16
17
|
@minitest_example._karafka_consumer_for(*)
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
# Forwards all arguments to `#_karafka_produce`
|
|
21
|
+
# @return [Rdkafka::Producer::DeliveryReport] delivery report
|
|
20
22
|
def produce(*)
|
|
21
23
|
@minitest_example._karafka_produce(*)
|
|
22
24
|
end
|
|
23
25
|
|
|
24
26
|
# Forwards all arguments to `#_karafka_produce_to`
|
|
27
|
+
# @return [Rdkafka::Producer::DeliveryReport] delivery report
|
|
25
28
|
def produce_to(*)
|
|
26
29
|
@minitest_example._karafka_produce_to(*)
|
|
27
30
|
end
|
|
@@ -36,6 +39,17 @@ module Karafka
|
|
|
36
39
|
def consumer_messages
|
|
37
40
|
@minitest_example._karafka_consumer_messages
|
|
38
41
|
end
|
|
42
|
+
|
|
43
|
+
# @return [Array<Hash>] pauses requested by the consumers, each with the `topic`,
|
|
44
|
+
# `partition`, `offset` and `timeout`
|
|
45
|
+
def pauses
|
|
46
|
+
@minitest_example._karafka_pauses
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @return [Array<Hash>] resumes of paused consumers, each with the `topic` and `partition`
|
|
50
|
+
def resumes
|
|
51
|
+
@minitest_example._karafka_resumes
|
|
52
|
+
end
|
|
39
53
|
end
|
|
40
54
|
end
|
|
41
55
|
end
|
|
@@ -5,6 +5,7 @@ require "karafka/testing"
|
|
|
5
5
|
require "karafka/testing/errors"
|
|
6
6
|
require "karafka/testing/helpers"
|
|
7
7
|
require "karafka/testing/spec_consumer_client"
|
|
8
|
+
require "karafka/testing/spec_consumer_resume"
|
|
8
9
|
require "karafka/testing/spec_producer_client"
|
|
9
10
|
require "karafka/testing/rspec/proxy"
|
|
10
11
|
|
|
@@ -55,37 +56,52 @@ module Karafka
|
|
|
55
56
|
# that patches are available because some users have Mocha as part of their
|
|
56
57
|
# supply chain, but do not use it when running Karafka specs. In such cases, without
|
|
57
58
|
# such check `karafka-testing` would falsely assume, that Mocha is in use.
|
|
59
|
+
# With Mocha we define the method instead of stubbing it: Mocha records every call in
|
|
60
|
+
# an unsynchronized array, which breaks when code under test produces from many threads
|
|
58
61
|
if Object.const_defined?("Mocha", false) && Karafka.producer.respond_to?(:stubs)
|
|
59
|
-
|
|
62
|
+
producer_client = _karafka_producer_client
|
|
63
|
+
Karafka.producer.define_singleton_method(:client) { producer_client }
|
|
64
|
+
@_karafka_producer_client_stubbed = true
|
|
60
65
|
else
|
|
61
66
|
allow(Karafka.producer).to receive(:client).and_return(_karafka_producer_client)
|
|
62
67
|
end
|
|
63
68
|
end
|
|
69
|
+
|
|
70
|
+
base.after do
|
|
71
|
+
next unless @_karafka_producer_client_stubbed
|
|
72
|
+
|
|
73
|
+
Karafka.producer.singleton_class.remove_method(:client)
|
|
74
|
+
@_karafka_producer_client_stubbed = false
|
|
75
|
+
end
|
|
64
76
|
end
|
|
65
77
|
end
|
|
66
78
|
|
|
67
79
|
# Creates a consumer instance for a given topic
|
|
68
80
|
#
|
|
69
|
-
# @param
|
|
70
|
-
#
|
|
81
|
+
# @param topic_name [String, Symbol] name of the topic for which we want to create a
|
|
82
|
+
# consumer instance
|
|
71
83
|
# @param requested_consumer_group [String, Symbol, nil] optional name of the consumer group
|
|
72
84
|
# if we have multiple consumer groups listening on the same topic
|
|
73
85
|
# @return [Object] Karafka consumer instance
|
|
74
86
|
# @raise [Karafka::Testing::Errors::TopicNotFoundError] raised when we're unable to find
|
|
75
87
|
# topic that was requested
|
|
88
|
+
# @raise [Karafka::Testing::Errors::TopicInManyConsumerGroupsError] raised when the topic is
|
|
89
|
+
# in many consumer groups and no consumer group was requested
|
|
90
|
+
# @raise [Karafka::Testing::Errors::ConsumerGroupNotFoundError] raised when the requested
|
|
91
|
+
# consumer group does not exist
|
|
76
92
|
#
|
|
77
93
|
# @example Creates a MyConsumer consumer instance with settings for `my_requested_topic`
|
|
78
94
|
# RSpec.describe MyConsumer do
|
|
79
95
|
# subject(:consumer) { karafka.consumer_for(:my_requested_topic) }
|
|
80
96
|
# end
|
|
81
|
-
def _karafka_consumer_for(
|
|
97
|
+
def _karafka_consumer_for(topic_name, requested_consumer_group = nil)
|
|
82
98
|
selected_topics = Testing::Helpers.karafka_consumer_find_candidate_topics(
|
|
83
|
-
|
|
99
|
+
topic_name.to_s,
|
|
84
100
|
requested_consumer_group.to_s
|
|
85
101
|
)
|
|
86
102
|
|
|
87
|
-
raise Errors::TopicInManyConsumerGroupsError,
|
|
88
|
-
raise Errors::TopicNotFoundError,
|
|
103
|
+
raise Errors::TopicInManyConsumerGroupsError, topic_name if selected_topics.size > 1
|
|
104
|
+
raise Errors::TopicNotFoundError, topic_name if selected_topics.empty?
|
|
89
105
|
|
|
90
106
|
_karafka_build_consumer_for(selected_topics.first)
|
|
91
107
|
end
|
|
@@ -101,12 +117,12 @@ module Karafka
|
|
|
101
117
|
#
|
|
102
118
|
# @example Send a json message to consumer and simulate, that it is partition 6
|
|
103
119
|
# before do
|
|
104
|
-
# karafka.produce({ 'hello' => 'world' }.to_json,
|
|
120
|
+
# karafka.produce({ 'hello' => 'world' }.to_json, partition: 6)
|
|
105
121
|
# end
|
|
106
122
|
def _karafka_add_message_to_consumer_if_needed(message)
|
|
107
123
|
consumer_obj = if defined?(consumer)
|
|
108
124
|
consumer
|
|
109
|
-
elsif
|
|
125
|
+
elsif _karafka_described_class_consumer?
|
|
110
126
|
_karafka_resolve_subject_consumer || _karafka_find_consumer_for_message(message)
|
|
111
127
|
else
|
|
112
128
|
_karafka_find_consumer_for_message(message)
|
|
@@ -124,8 +140,10 @@ module Karafka
|
|
|
124
140
|
return if message[:consumer_group] &&
|
|
125
141
|
message[:consumer_group].to_s != consumer_obj.topic.consumer_group.name
|
|
126
142
|
|
|
143
|
+
batch = _karafka_consumer_batch(consumer_obj)
|
|
144
|
+
|
|
127
145
|
# Build message metadata and copy any metadata that would come from the message
|
|
128
|
-
metadata = _karafka_message_metadata_defaults(consumer_obj)
|
|
146
|
+
metadata = _karafka_message_metadata_defaults(consumer_obj, batch)
|
|
129
147
|
|
|
130
148
|
metadata.keys.each do |key|
|
|
131
149
|
message_key = METADATA_DISPATCH_MAPPINGS.fetch(key, key)
|
|
@@ -135,23 +153,41 @@ module Karafka
|
|
|
135
153
|
metadata[key] = message.fetch(message_key)
|
|
136
154
|
end
|
|
137
155
|
|
|
138
|
-
|
|
139
|
-
|
|
156
|
+
metadata[:raw_headers] ||= {}
|
|
157
|
+
|
|
158
|
+
metadata = Karafka::Messages::Metadata.new(metadata)
|
|
159
|
+
karafka_message = Karafka::Messages::Message.new(
|
|
140
160
|
message[:payload],
|
|
141
|
-
|
|
161
|
+
metadata
|
|
142
162
|
)
|
|
163
|
+
metadata[:message] = karafka_message
|
|
164
|
+
|
|
165
|
+
# Add this message to previously produced messages
|
|
166
|
+
_karafka_consumer_messages << karafka_message
|
|
167
|
+
batch << karafka_message unless batch.equal?(_karafka_consumer_messages)
|
|
168
|
+
|
|
169
|
+
# A real batch never mixes partitions, so the first message of this consumer decides
|
|
170
|
+
# the partition
|
|
171
|
+
partition = (@_karafka_batch_partitions ||= {})[consumer_obj] ||= karafka_message.partition
|
|
172
|
+
|
|
173
|
+
if consumer_obj.partition != partition
|
|
174
|
+
consumer_obj.coordinator = Testing::Helpers.karafka_build_coordinator(
|
|
175
|
+
consumer_obj.topic,
|
|
176
|
+
partition
|
|
177
|
+
)
|
|
178
|
+
end
|
|
143
179
|
|
|
144
180
|
# Update batch metadata
|
|
145
181
|
batch_metadata = Karafka::Messages::Builders::BatchMetadata.call(
|
|
146
|
-
|
|
182
|
+
batch,
|
|
147
183
|
consumer_obj.topic,
|
|
148
|
-
|
|
184
|
+
partition,
|
|
149
185
|
Time.now
|
|
150
186
|
)
|
|
151
187
|
|
|
152
188
|
# Update consumer messages batch
|
|
153
189
|
consumer_obj.messages = Karafka::Messages::Messages.new(
|
|
154
|
-
|
|
190
|
+
batch,
|
|
155
191
|
batch_metadata
|
|
156
192
|
)
|
|
157
193
|
end
|
|
@@ -163,14 +199,15 @@ module Karafka
|
|
|
163
199
|
# offsets auto-increment from 0).
|
|
164
200
|
def _karafka_produce(payload, metadata = {})
|
|
165
201
|
# Extract offset before passing to WaterDrop since it is not a valid
|
|
166
|
-
# WaterDrop message attribute (Kafka assigns offsets, not producers)
|
|
167
|
-
|
|
202
|
+
# WaterDrop message attribute (Kafka assigns offsets, not producers). Kept per thread, as
|
|
203
|
+
# code under test may produce from many threads at once
|
|
204
|
+
Thread.current[:_karafka_next_offset] = metadata.delete(:offset)
|
|
168
205
|
|
|
169
|
-
|
|
206
|
+
topic_name = if metadata[:topic]
|
|
170
207
|
metadata[:topic]
|
|
171
208
|
elsif defined?(consumer)
|
|
172
209
|
consumer.topic.name
|
|
173
|
-
elsif
|
|
210
|
+
elsif _karafka_described_class_consumer? && (sub = _karafka_resolve_subject_consumer)
|
|
174
211
|
sub.topic.name
|
|
175
212
|
else
|
|
176
213
|
last_consumer = @_karafka_consumer_mappings&.values&.last
|
|
@@ -178,12 +215,12 @@ module Karafka
|
|
|
178
215
|
end
|
|
179
216
|
Karafka.producer.produce_sync(
|
|
180
217
|
{
|
|
181
|
-
topic:
|
|
218
|
+
topic: topic_name,
|
|
182
219
|
payload: payload
|
|
183
220
|
}.merge(metadata)
|
|
184
221
|
)
|
|
185
222
|
ensure
|
|
186
|
-
|
|
223
|
+
Thread.current[:_karafka_next_offset] = nil
|
|
187
224
|
end
|
|
188
225
|
|
|
189
226
|
# @return [Array<Hash>] messages that were produced
|
|
@@ -191,6 +228,16 @@ module Karafka
|
|
|
191
228
|
_karafka_producer_client.messages
|
|
192
229
|
end
|
|
193
230
|
|
|
231
|
+
# @return [Array<Hash>] pauses requested by the consumers
|
|
232
|
+
def _karafka_pauses
|
|
233
|
+
_karafka_consumer_client.pauses
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# @return [Array<Hash>] resumes of paused consumers
|
|
237
|
+
def _karafka_resumes
|
|
238
|
+
_karafka_consumer_client.resumes
|
|
239
|
+
end
|
|
240
|
+
|
|
194
241
|
# Produces message to a specific consumer instance
|
|
195
242
|
# Use when testing multiple consumers for the same topic
|
|
196
243
|
#
|
|
@@ -218,11 +265,43 @@ module Karafka
|
|
|
218
265
|
|
|
219
266
|
private
|
|
220
267
|
|
|
268
|
+
# Messages of the batch of a given consumer. The first consumer that gets a message uses
|
|
269
|
+
# the shared buffer, so altering `consumer_messages` alters its batch. Once a second
|
|
270
|
+
# consumer gets one, each consumer gets its own batch, as a real batch holds one topic.
|
|
271
|
+
#
|
|
272
|
+
# @param consumer_obj [Karafka::BaseConsumer] consumer reference
|
|
273
|
+
# @return [Array<Karafka::Messages::Message>] messages of this consumer's batch
|
|
274
|
+
def _karafka_consumer_batch(consumer_obj)
|
|
275
|
+
batches = (@_karafka_consumer_batches ||= {})
|
|
276
|
+
|
|
277
|
+
batches.fetch(consumer_obj) do
|
|
278
|
+
if batches.empty?
|
|
279
|
+
batches[consumer_obj] = _karafka_consumer_messages
|
|
280
|
+
else
|
|
281
|
+
first_consumer, first_batch = batches.first
|
|
282
|
+
|
|
283
|
+
if first_batch.equal?(_karafka_consumer_messages)
|
|
284
|
+
own_batch = first_batch.dup
|
|
285
|
+
batches[first_consumer] = own_batch
|
|
286
|
+
|
|
287
|
+
if first_consumer.messages
|
|
288
|
+
first_consumer.messages = Karafka::Messages::Messages.new(
|
|
289
|
+
own_batch,
|
|
290
|
+
first_consumer.messages.metadata
|
|
291
|
+
)
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
batches[consumer_obj] = []
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
221
300
|
# Checks if the RSpec described_class is a Karafka consumer class.
|
|
222
301
|
# When true, the implicit RSpec subject can be used as a consumer fallback.
|
|
223
302
|
#
|
|
224
303
|
# @return [Boolean] true if described_class inherits from Karafka::BaseConsumer
|
|
225
|
-
def
|
|
304
|
+
def _karafka_described_class_consumer?
|
|
226
305
|
respond_to?(:described_class) &&
|
|
227
306
|
described_class.is_a?(Class) &&
|
|
228
307
|
described_class < Karafka::BaseConsumer
|
|
@@ -267,14 +346,15 @@ module Karafka
|
|
|
267
346
|
end
|
|
268
347
|
|
|
269
348
|
# @param consumer_obj [Karafka::BaseConsumer] consumer reference
|
|
349
|
+
# @param batch [Array<Karafka::Messages::Message>] messages already given to this consumer
|
|
270
350
|
# @return [Hash] message default options
|
|
271
|
-
def _karafka_message_metadata_defaults(consumer_obj)
|
|
351
|
+
def _karafka_message_metadata_defaults(consumer_obj, batch)
|
|
272
352
|
{
|
|
273
353
|
deserializers: consumer_obj.topic.deserializers,
|
|
274
354
|
timestamp: Time.now,
|
|
275
355
|
raw_headers: {},
|
|
276
356
|
raw_key: nil,
|
|
277
|
-
offset:
|
|
357
|
+
offset: Thread.current[:_karafka_next_offset] || batch.size,
|
|
278
358
|
partition: 0,
|
|
279
359
|
received_at: Time.now,
|
|
280
360
|
topic: consumer_obj.topic.name
|
|
@@ -286,10 +366,6 @@ module Karafka
|
|
|
286
366
|
# @param topic [Karafka::Routing::Topic] topic for which we want to build the consumer
|
|
287
367
|
# @return [Object] karafka consumer
|
|
288
368
|
def _karafka_build_consumer_for(topic)
|
|
289
|
-
coordinators = Karafka::Processing::CoordinatorsBuffer.new(
|
|
290
|
-
Karafka::Routing::Topics.new([topic])
|
|
291
|
-
)
|
|
292
|
-
|
|
293
369
|
processing_cfg = Karafka::App.config.internal.processing
|
|
294
370
|
consumer = topic.consumer.new
|
|
295
371
|
consumer.producer = Karafka::App.producer
|
|
@@ -297,12 +373,12 @@ module Karafka
|
|
|
297
373
|
strategy = processing_cfg.consumer_groups.strategy_selector.find(topic)
|
|
298
374
|
consumer.singleton_class.include(strategy)
|
|
299
375
|
consumer.client = _karafka_consumer_client
|
|
300
|
-
consumer.coordinator =
|
|
301
|
-
consumer.coordinator.seek_offset = 0
|
|
376
|
+
consumer.coordinator = Testing::Helpers.karafka_build_coordinator(topic, 0)
|
|
302
377
|
# Indicate usage as for tests no direct enqueuing happens
|
|
303
378
|
consumer.instance_variable_set(:@used, true)
|
|
304
379
|
expansions = processing_cfg.consumer_groups.expansions_selector.find(topic)
|
|
305
380
|
expansions.each { |expansion| consumer.singleton_class.include(expansion) }
|
|
381
|
+
consumer.singleton_class.prepend(Testing::SpecConsumerResume)
|
|
306
382
|
|
|
307
383
|
@_karafka_consumer_mappings[topic.id] = consumer
|
|
308
384
|
consumer
|
|
@@ -12,16 +12,19 @@ module Karafka
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
# Forwards all arguments to `#_karafka_consumer_for`
|
|
15
|
+
# @return [Karafka::BaseConsumer] consumer instance
|
|
15
16
|
def consumer_for(*)
|
|
16
17
|
@rspec_example._karafka_consumer_for(*)
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
# Forwards all arguments to `#_karafka_produce`
|
|
21
|
+
# @return [Rdkafka::Producer::DeliveryReport] delivery report
|
|
20
22
|
def produce(*)
|
|
21
23
|
@rspec_example._karafka_produce(*)
|
|
22
24
|
end
|
|
23
25
|
|
|
24
26
|
# Forwards all arguments to `#_karafka_produce_to`
|
|
27
|
+
# @return [Rdkafka::Producer::DeliveryReport] delivery report
|
|
25
28
|
def produce_to(*)
|
|
26
29
|
@rspec_example._karafka_produce_to(*)
|
|
27
30
|
end
|
|
@@ -36,6 +39,17 @@ module Karafka
|
|
|
36
39
|
def consumer_messages
|
|
37
40
|
@rspec_example._karafka_consumer_messages
|
|
38
41
|
end
|
|
42
|
+
|
|
43
|
+
# @return [Array<Hash>] pauses requested by the consumers, each with the `topic`,
|
|
44
|
+
# `partition`, `offset` and `timeout`
|
|
45
|
+
def pauses
|
|
46
|
+
@rspec_example._karafka_pauses
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @return [Array<Hash>] resumes of paused consumers, each with the `topic` and `partition`
|
|
50
|
+
def resumes
|
|
51
|
+
@rspec_example._karafka_resumes
|
|
52
|
+
end
|
|
39
53
|
end
|
|
40
54
|
end
|
|
41
55
|
end
|
|
@@ -6,6 +6,20 @@ module Karafka
|
|
|
6
6
|
# For specs we do not mark anything as consumed, nor do we really send heartbeats.
|
|
7
7
|
# Those things are tested in the framework itself
|
|
8
8
|
class SpecConsumerClient
|
|
9
|
+
# @return [Array<Hash>] pauses requested on this client, each with the `topic`,
|
|
10
|
+
# `partition`, `offset` and `timeout`
|
|
11
|
+
attr_reader :pauses
|
|
12
|
+
|
|
13
|
+
# @return [Array<Hash>] resumes requested on this client, each with the `topic` and
|
|
14
|
+
# `partition`
|
|
15
|
+
attr_reader :resumes
|
|
16
|
+
|
|
17
|
+
# Builds a client with no pauses or resumes recorded
|
|
18
|
+
def initialize
|
|
19
|
+
@pauses = []
|
|
20
|
+
@resumes = []
|
|
21
|
+
end
|
|
22
|
+
|
|
9
23
|
%i[
|
|
10
24
|
mark_as_consumed
|
|
11
25
|
mark_as_consumed!
|
|
@@ -17,6 +31,28 @@ module Karafka
|
|
|
17
31
|
define_method(caught_delegator) { |*| true }
|
|
18
32
|
end
|
|
19
33
|
|
|
34
|
+
# Records the pause instead of pausing the partition
|
|
35
|
+
#
|
|
36
|
+
# @param topic [String] topic name
|
|
37
|
+
# @param partition [Integer] partition
|
|
38
|
+
# @param offset [Integer, nil] offset to continue from or nil to keep the current position
|
|
39
|
+
# @param timeout [Integer] pause time in milliseconds
|
|
40
|
+
# @return [true]
|
|
41
|
+
def pause(topic, partition, offset = nil, timeout = 0)
|
|
42
|
+
@pauses << { topic: topic, partition: partition, offset: offset, timeout: timeout }
|
|
43
|
+
true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Records the resume instead of resuming the partition
|
|
47
|
+
#
|
|
48
|
+
# @param topic [String] topic name
|
|
49
|
+
# @param partition [Integer] partition
|
|
50
|
+
# @return [true]
|
|
51
|
+
def resume(topic, partition)
|
|
52
|
+
@resumes << { topic: topic, partition: partition }
|
|
53
|
+
true
|
|
54
|
+
end
|
|
55
|
+
|
|
20
56
|
# @return [Boolean] assignments are never lost for specs
|
|
21
57
|
def assignment_lost?
|
|
22
58
|
false
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Karafka
|
|
4
|
+
module Testing
|
|
5
|
+
# Resumes the partition on the client when a paused consumer resumes. In a real run the
|
|
6
|
+
# listener does it before the next poll, which never happens in specs.
|
|
7
|
+
module SpecConsumerResume
|
|
8
|
+
# Expires the pause and, if it was paused, resumes the partition as the listener does
|
|
9
|
+
def resume
|
|
10
|
+
pause_tracker = coordinator.pause_tracker
|
|
11
|
+
paused = pause_tracker.paused?
|
|
12
|
+
|
|
13
|
+
super
|
|
14
|
+
|
|
15
|
+
return unless paused
|
|
16
|
+
|
|
17
|
+
pause_tracker.resume
|
|
18
|
+
client.resume(topic.name, partition)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -3,21 +3,47 @@
|
|
|
3
3
|
module Karafka
|
|
4
4
|
module Testing
|
|
5
5
|
# Spec producer client used to buffer messages that we send out in specs
|
|
6
|
+
#
|
|
7
|
+
# @note Code under test may produce from many threads at once, so every operation that touches
|
|
8
|
+
# the buffers runs under one lock
|
|
6
9
|
class SpecProducerClient < ::WaterDrop::Clients::Buffered
|
|
7
|
-
# @param
|
|
8
|
-
# on it that are
|
|
9
|
-
def initialize(
|
|
10
|
+
# @param example [RSpec::Core::ExampleGroup, Minitest::Test] test example we need to hold to
|
|
11
|
+
# trigger actions on it that are test context aware
|
|
12
|
+
def initialize(example)
|
|
10
13
|
super(nil)
|
|
11
|
-
@
|
|
14
|
+
@example = example
|
|
15
|
+
@mutex = Mutex.new
|
|
12
16
|
end
|
|
13
17
|
|
|
14
18
|
# "Produces" message to Kafka. That is, it acknowledges it locally, adds it to the internal
|
|
15
19
|
# buffer and adds it (if needed) into the current consumer messages buffer
|
|
16
20
|
# @param message [Hash] `Karafka.producer.produce_sync` message hash
|
|
17
21
|
def produce(message)
|
|
18
|
-
@
|
|
22
|
+
@mutex.synchronize do
|
|
23
|
+
@example._karafka_add_message_to_consumer_if_needed(message)
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
super
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Starts the transaction on a given level
|
|
30
|
+
def begin_transaction
|
|
31
|
+
@mutex.synchronize { super }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Finishes given level of transaction
|
|
35
|
+
def commit_transaction
|
|
36
|
+
@mutex.synchronize { super }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Aborts the transaction
|
|
40
|
+
def abort_transaction
|
|
41
|
+
@mutex.synchronize { super }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Clears internal buffer. Used in between specs so messages do not leak out
|
|
45
|
+
def reset
|
|
46
|
+
@mutex.synchronize { super }
|
|
21
47
|
end
|
|
22
48
|
end
|
|
23
49
|
end
|
data/lib/karafka/testing.rb
CHANGED
|
@@ -8,6 +8,8 @@ module Karafka
|
|
|
8
8
|
#
|
|
9
9
|
# You should never use karafka-testing primitives when framework is not loaded because
|
|
10
10
|
# testing lib stubs certain pieces of Karafka that need to be initialized.
|
|
11
|
+
#
|
|
12
|
+
# @raise [Karafka::Testing::Errors::KarafkaNotLoadedError] raised when Karafka is not loaded
|
|
11
13
|
def ensure_karafka_loaded!
|
|
12
14
|
return if ::Karafka.const_defined?("App", false)
|
|
13
15
|
|
|
@@ -19,6 +21,10 @@ module Karafka
|
|
|
19
21
|
|
|
20
22
|
# If you do not initialize Karafka always within your specs, do not include/use this lib
|
|
21
23
|
# in places where Karafka would not be loaded.
|
|
24
|
+
#
|
|
25
|
+
# @raise [Karafka::Testing::Errors::KarafkaNotLoadedError] raised when Karafka is not loaded
|
|
26
|
+
# @raise [Karafka::Testing::Errors::KarafkaNotInitializedError] raised when Karafka is still
|
|
27
|
+
# initializing
|
|
22
28
|
def ensure_karafka_initialized!
|
|
23
29
|
ensure_karafka_loaded!
|
|
24
30
|
|
data/package-lock.json
CHANGED
|
@@ -103,9 +103,9 @@
|
|
|
103
103
|
}
|
|
104
104
|
},
|
|
105
105
|
"node_modules/fastq": {
|
|
106
|
-
"version": "1.20.
|
|
107
|
-
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.
|
|
108
|
-
"integrity": "sha512-
|
|
106
|
+
"version": "1.20.3",
|
|
107
|
+
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.3.tgz",
|
|
108
|
+
"integrity": "sha512-XKv5nnLs6nLF71NgiKJLIZFLkPyIEuOselLG7ujZnGrRfQK8HpvY+WqKhAJUAdLomwVHErVS4LfxFlPq0/FTAw==",
|
|
109
109
|
"dev": true,
|
|
110
110
|
"license": "ISC",
|
|
111
111
|
"dependencies": {
|
|
@@ -286,9 +286,9 @@
|
|
|
286
286
|
}
|
|
287
287
|
},
|
|
288
288
|
"node_modules/smol-toml": {
|
|
289
|
-
"version": "1.
|
|
290
|
-
"resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.
|
|
291
|
-
"integrity": "sha512-
|
|
289
|
+
"version": "1.9.1",
|
|
290
|
+
"resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.9.1.tgz",
|
|
291
|
+
"integrity": "sha512-j7WyPdf0CJoutMzMoPLF/bVXHtKZIT9Rb4eGeEbWw8X2t5oirjPgpL+OpaENZrILYaXqYwnVH+qgUwVbl+pUJQ==",
|
|
292
292
|
"dev": true,
|
|
293
293
|
"license": "BSD-3-Clause",
|
|
294
294
|
"engines": {
|
|
@@ -312,9 +312,9 @@
|
|
|
312
312
|
}
|
|
313
313
|
},
|
|
314
314
|
"node_modules/yaml": {
|
|
315
|
-
"version": "2.9.
|
|
316
|
-
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.
|
|
317
|
-
"integrity": "sha512-
|
|
315
|
+
"version": "2.9.1",
|
|
316
|
+
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.1.tgz",
|
|
317
|
+
"integrity": "sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw==",
|
|
318
318
|
"dev": true,
|
|
319
319
|
"license": "ISC",
|
|
320
320
|
"bin": {
|
data/renovate.json
CHANGED
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
|
+
version: 2.6.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Maciej Mensfeld
|
|
@@ -82,6 +82,7 @@ files:
|
|
|
82
82
|
- lib/karafka/testing/rspec/helpers.rb
|
|
83
83
|
- lib/karafka/testing/rspec/proxy.rb
|
|
84
84
|
- lib/karafka/testing/spec_consumer_client.rb
|
|
85
|
+
- lib/karafka/testing/spec_consumer_resume.rb
|
|
85
86
|
- lib/karafka/testing/spec_producer_client.rb
|
|
86
87
|
- lib/karafka/testing/version.rb
|
|
87
88
|
- package-lock.json
|
|
@@ -104,14 +105,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
104
105
|
requirements:
|
|
105
106
|
- - ">="
|
|
106
107
|
- !ruby/object:Gem::Version
|
|
107
|
-
version: 3.
|
|
108
|
+
version: 3.3.0
|
|
108
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
110
|
requirements:
|
|
110
111
|
- - ">="
|
|
111
112
|
- !ruby/object:Gem::Version
|
|
112
113
|
version: '0'
|
|
113
114
|
requirements: []
|
|
114
|
-
rubygems_version: 4.0.
|
|
115
|
+
rubygems_version: 4.0.20
|
|
115
116
|
specification_version: 4
|
|
116
117
|
summary: Library which provides helpers for easier Karafka consumers tests
|
|
117
118
|
test_files: []
|