karafka-testing 1.4.4 → 2.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.github/workflows/ci.yml +5 -3
- data/.ruby-version +1 -1
- data/2.0-Upgrade.md +60 -0
- data/CHANGELOG.md +4 -5
- data/Gemfile.lock +22 -34
- data/README.md +11 -8
- data/karafka-testing.gemspec +1 -1
- data/lib/karafka/testing/dummy_client.rb +0 -2
- data/lib/karafka/testing/rspec/helpers.rb +36 -24
- data/lib/karafka/testing/rspec/proxy.rb +25 -0
- data/lib/karafka/testing/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +9 -7
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99b31b45b503aded03f25274fc7819219b3707644cbfa57a6136426351b52ae1
|
4
|
+
data.tar.gz: 1321c4e974d9dcd25f9f38fb0b8237a503d3f4399cc08ea299820d6dd8ce2479
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeb543efd92f2c9c7cafb50bb389e8d0baf388ce086cbe8751b2a2362d1e666c96766f771ff3fe20d38fcc9d3f8b69563073d4709fc93d756c880cbf4c6646e1
|
7
|
+
data.tar.gz: c5eb1aaeeedeb7ab8c08e77998422b5a249069818b50abc842e9cf0d9599b59f07d88167e71c4d9791f62e979babb15b659816fb9249a8927e11e65909d40e1f
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/.github/workflows/ci.yml
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
name: ci
|
2
2
|
|
3
|
+
concurrency: ci-${{ github.ref }}
|
4
|
+
|
3
5
|
on:
|
4
6
|
pull_request:
|
5
7
|
push:
|
@@ -14,11 +16,11 @@ jobs:
|
|
14
16
|
fail-fast: false
|
15
17
|
matrix:
|
16
18
|
ruby:
|
19
|
+
- '3.1'
|
17
20
|
- '3.0'
|
18
21
|
- '2.7'
|
19
|
-
- '2.6'
|
20
22
|
include:
|
21
|
-
- ruby: '3.
|
23
|
+
- ruby: '3.1'
|
22
24
|
coverage: 'true'
|
23
25
|
steps:
|
24
26
|
- uses: actions/checkout@v2
|
@@ -46,7 +48,7 @@ jobs:
|
|
46
48
|
- name: Set up Ruby
|
47
49
|
uses: ruby/setup-ruby@v1
|
48
50
|
with:
|
49
|
-
ruby-version: 3.
|
51
|
+
ruby-version: 3.1
|
50
52
|
- name: Install latest bundler
|
51
53
|
run: gem install bundler --no-document
|
52
54
|
- name: Install Diffend plugin
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0
|
1
|
+
3.1.0
|
data/2.0-Upgrade.md
ADDED
@@ -0,0 +1,60 @@
|
|
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.publish`
|
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.publish({ 'number' => nr1_value }.to_json)
|
28
|
+
karafka.publish({ '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
|
+
```
|
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# Karafka Test gem changelog
|
2
2
|
|
3
|
-
##
|
4
|
-
-
|
5
|
-
|
6
|
-
|
7
|
-
- #71 - `mark_as_consumed!` is not stubbed, causing `Karafka::Errors::MissingClientError`
|
3
|
+
## 2.0.0.alpha1 (2022-01-30)
|
4
|
+
- Change the API to be more comprehensive
|
5
|
+
- Update to work with Karafka 2.0
|
6
|
+
- Support for Ruby 3.1
|
8
7
|
|
9
8
|
## 1.4.2 (2021-04-21)
|
10
9
|
- Restore MIT license
|
data/Gemfile.lock
CHANGED
@@ -1,19 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
karafka-testing (
|
5
|
-
karafka (~>
|
4
|
+
karafka-testing (2.0.0.alpha1)
|
5
|
+
karafka (~> 2.0.alpha1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
concurrent-ruby (1.1.9)
|
11
|
-
|
12
|
-
king_konf (~> 1.0)
|
13
|
-
ruby-kafka (~> 1.0)
|
14
|
-
digest-crc (0.6.4)
|
15
|
-
rake (>= 12.0.0, < 14.0.0)
|
16
|
-
dry-configurable (0.13.0)
|
11
|
+
dry-configurable (0.14.0)
|
17
12
|
concurrent-ruby (~> 1.0)
|
18
13
|
dry-core (~> 0.6)
|
19
14
|
dry-container (0.9.0)
|
@@ -25,7 +20,7 @@ GEM
|
|
25
20
|
concurrent-ruby (~> 1.0)
|
26
21
|
dry-core (~> 0.5, >= 0.5)
|
27
22
|
dry-inflector (0.2.1)
|
28
|
-
dry-initializer (3.
|
23
|
+
dry-initializer (3.1.1)
|
29
24
|
dry-logic (1.2.0)
|
30
25
|
concurrent-ruby (~> 1.0)
|
31
26
|
dry-core (~> 0.5, >= 0.5)
|
@@ -52,37 +47,30 @@ GEM
|
|
52
47
|
dry-core (~> 0.5, >= 0.5)
|
53
48
|
dry-initializer (~> 3.0)
|
54
49
|
dry-schema (~> 1.8, >= 1.8.0)
|
55
|
-
|
56
|
-
|
57
|
-
io-console (0.5.9)
|
58
|
-
irb (1.3.7)
|
59
|
-
reline (>= 0.2.7)
|
60
|
-
karafka (1.4.11)
|
50
|
+
ffi (1.15.5)
|
51
|
+
karafka (2.0.0.alpha1)
|
61
52
|
dry-configurable (~> 0.13)
|
62
|
-
dry-inflector (~> 0.2)
|
63
53
|
dry-monitor (~> 0.5)
|
64
54
|
dry-validation (~> 1.7)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
zeitwerk (~> 2.4)
|
71
|
-
king_konf (1.0.0)
|
55
|
+
rdkafka (>= 0.10)
|
56
|
+
thor (>= 0.20)
|
57
|
+
waterdrop (>= 2.1.0, < 3.0.0)
|
58
|
+
zeitwerk (~> 2.3)
|
59
|
+
mini_portile2 (2.7.1)
|
72
60
|
rake (13.0.6)
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
thor (1.1
|
78
|
-
waterdrop (1.
|
79
|
-
|
61
|
+
rdkafka (0.11.1)
|
62
|
+
ffi (~> 1.15)
|
63
|
+
mini_portile2 (~> 2.6)
|
64
|
+
rake (> 12)
|
65
|
+
thor (1.2.1)
|
66
|
+
waterdrop (2.1.0)
|
67
|
+
concurrent-ruby (>= 1.1)
|
80
68
|
dry-configurable (~> 0.13)
|
81
69
|
dry-monitor (~> 0.5)
|
82
70
|
dry-validation (~> 1.7)
|
83
|
-
|
84
|
-
zeitwerk (~> 2.
|
85
|
-
zeitwerk (2.5.
|
71
|
+
rdkafka (>= 0.10)
|
72
|
+
zeitwerk (~> 2.3)
|
73
|
+
zeitwerk (2.5.4)
|
86
74
|
|
87
75
|
PLATFORMS
|
88
76
|
x86_64-linux
|
@@ -91,4 +79,4 @@ DEPENDENCIES
|
|
91
79
|
karafka-testing!
|
92
80
|
|
93
81
|
BUNDLED WITH
|
94
|
-
2.
|
82
|
+
2.3.5
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Karafka Testing library
|
2
2
|
|
3
|
+
**Note**: Documentation presented below works with not yet released Karafka `2.0`.
|
4
|
+
|
5
|
+
Please refer to [this](https://github.com/karafka/testing/tree/1.4) branch and its documentation for details about usage with Karafka `1.4`.
|
6
|
+
|
3
7
|
[![Build Status](https://github.com/karafka/testing/workflows/ci/badge.svg)](https://github.com/karafka/testing/actions?query=workflow%3Aci)
|
4
8
|
[![Gem Version](https://badge.fury.io/rb/karafka-testing.svg)](http://badge.fury.io/rb/karafka-testing)
|
5
9
|
[![Join the chat at https://slack.karafka.io](https://raw.githubusercontent.com/karafka/misc/master/slack.svg)](https://slack.karafka.io)
|
@@ -28,18 +32,17 @@ end
|
|
28
32
|
|
29
33
|
## Usage
|
30
34
|
|
31
|
-
Once included into your RSpec setup, this library will provide you two methods that you can use with your specs:
|
32
|
-
|
33
|
-
- `#karafka_consumer_for` - this method will create a consumer instance for the desired topic. It **needs** to be set as the spec subject.
|
34
|
-
- `#publish_for_karafka` - this method will "send" message to the consumer instance.
|
35
|
+
Once included into your RSpec setup, this library will provide you with a special object `#karafka` that includes two methods that you can use with your specs:
|
35
36
|
|
37
|
+
- `#consumer_for` - creates a consumer instance for the desired topic. It **needs** to be set as the spec subject.
|
38
|
+
- `#publish` - "sends" message to the consumer instance.
|
36
39
|
|
37
|
-
**Note:** Messages sent using the `#
|
40
|
+
**Note:** Messages sent using the `#publish` method won't be sent to Kafka. They will be "virtually" delegated to the created consumer instance so your specs can run without Kafka setup.
|
38
41
|
|
39
42
|
```ruby
|
40
43
|
RSpec.describe InlineBatchConsumer do
|
41
44
|
# This will create a consumer instance with all the settings defined for the given topic
|
42
|
-
subject(:consumer) {
|
45
|
+
subject(:consumer) { karafka.consumer_for(:inline_batch_data) }
|
43
46
|
|
44
47
|
let(:nr1_value) { rand }
|
45
48
|
let(:nr2_value) { rand }
|
@@ -47,9 +50,9 @@ RSpec.describe InlineBatchConsumer do
|
|
47
50
|
|
48
51
|
before do
|
49
52
|
# Sends first message to Karafka consumer
|
50
|
-
|
53
|
+
karafka.publish({ 'number' => nr1_value }.to_json)
|
51
54
|
# Sends second message to Karafka consumer
|
52
|
-
|
55
|
+
karafka.publish({ 'number' => nr2_value }.to_json, partition: 2)
|
53
56
|
allow(Karafka.logger).to receive(:info)
|
54
57
|
end
|
55
58
|
|
data/karafka-testing.gemspec
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'karafka/testing/errors'
|
4
4
|
require 'karafka/testing/dummy_client'
|
5
|
+
require 'karafka/testing/rspec/proxy'
|
5
6
|
|
6
7
|
module Karafka
|
7
8
|
module Testing
|
@@ -16,16 +17,15 @@ module Karafka
|
|
16
17
|
def included(base)
|
17
18
|
# This is an internal buffer for keeping "to be sent" messages before
|
18
19
|
# we run the consume
|
19
|
-
base.let(:
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# in between them
|
24
|
-
base.after { _karafka_raw_data.clear }
|
20
|
+
base.let(:_karafka_messages) { [] }
|
21
|
+
base.let(:karafka) { Karafka::Testing::RSpec::Proxy.new(self) }
|
22
|
+
# Clear the messages buffer after each spec, so nothing leaks in between them
|
23
|
+
base.after { _karafka_messages.clear }
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
|
-
# Creates a consumer instance for given topic
|
27
|
+
# Creates a consumer instance for a given topic
|
28
|
+
#
|
29
29
|
# @param requested_topic [String, Symbol] name of the topic for which we want to
|
30
30
|
# create a consumer instance
|
31
31
|
# @return [Object] described_class instance
|
@@ -34,12 +34,12 @@ module Karafka
|
|
34
34
|
#
|
35
35
|
# @example Creates a MyConsumer consumer instance with settings for `my_requested_topic`
|
36
36
|
# RSpec.describe MyConsumer do
|
37
|
-
# subject(:consumer) {
|
37
|
+
# subject(:consumer) { karafka.consumer_for(:my_requested_topic) }
|
38
38
|
# end
|
39
39
|
def karafka_consumer_for(requested_topic)
|
40
40
|
selected_topic = nil
|
41
41
|
|
42
|
-
# @note Remove in 2.
|
42
|
+
# @note Remove in 2.1. This won't work without the global state
|
43
43
|
::Karafka::App.consumer_groups.each do |consumer_group|
|
44
44
|
consumer_group.topics.each do |topic|
|
45
45
|
selected_topic = topic if topic.name == requested_topic.to_s
|
@@ -48,47 +48,59 @@ module Karafka
|
|
48
48
|
|
49
49
|
raise Karafka::Testing::Errors::TopicNotFoundError, requested_topic unless selected_topic
|
50
50
|
|
51
|
-
described_class.new
|
51
|
+
consumer = described_class.new
|
52
|
+
consumer.topic = selected_topic
|
53
|
+
consumer.client = Karafka::Testing::DummyClient.new
|
54
|
+
consumer
|
52
55
|
end
|
53
56
|
|
54
|
-
# Adds a new Karafka
|
57
|
+
# Adds a new Karafka message instance with given payload and options into an internal
|
55
58
|
# buffer that will be used to simulate messages delivery to the consumer
|
56
59
|
#
|
57
|
-
# @param
|
60
|
+
# @param payload [String] anything you want to send
|
58
61
|
# @param opts [Hash] additional options with which you want to overwrite the
|
59
62
|
# message defaults (key, offset, etc)
|
60
63
|
#
|
61
64
|
# @example Send a json message to consumer
|
62
65
|
# before do
|
63
|
-
#
|
66
|
+
# karafka.publish({ 'hello' => 'world' }.to_json)
|
64
67
|
# end
|
65
68
|
#
|
66
69
|
# @example Send a json message to consumer and simulate, that it is partition 6
|
67
70
|
# before do
|
68
|
-
#
|
71
|
+
# karafka.publish({ 'hello' => 'world' }.to_json, 'partition' => 6)
|
69
72
|
# end
|
70
|
-
def
|
71
|
-
metadata = Karafka::
|
72
|
-
**
|
73
|
+
def karafka_publish(payload, opts = {})
|
74
|
+
metadata = Karafka::Messages::Metadata.new(
|
75
|
+
**karafka_message_metadata_defaults.merge(opts)
|
73
76
|
).freeze
|
74
77
|
|
75
|
-
|
76
|
-
|
78
|
+
# Add this message to previously published messages
|
79
|
+
_karafka_messages << Karafka::Messages::Message.new(payload, metadata)
|
80
|
+
|
81
|
+
# Update batch metadata
|
82
|
+
batch_metadata = Karafka::Messages::Builders::BatchMetadata.call(
|
83
|
+
_karafka_messages,
|
84
|
+
subject.topic,
|
85
|
+
Time.now
|
86
|
+
)
|
87
|
+
|
88
|
+
# Update consumer messages batch
|
89
|
+
subject.messages = Karafka::Messages::Messages.new(_karafka_messages, batch_metadata)
|
77
90
|
end
|
78
91
|
|
79
92
|
private
|
80
93
|
|
81
94
|
# @return [Hash] message default options
|
82
|
-
def
|
95
|
+
def karafka_message_metadata_defaults
|
83
96
|
{
|
84
97
|
deserializer: subject.topic.deserializer,
|
85
|
-
|
98
|
+
timestamp: Time.now,
|
86
99
|
headers: {},
|
87
|
-
is_control_record: false,
|
88
100
|
key: nil,
|
89
|
-
offset:
|
101
|
+
offset: _karafka_messages.size,
|
90
102
|
partition: 0,
|
91
|
-
|
103
|
+
received_at: Time.now,
|
92
104
|
topic: subject.topic.name
|
93
105
|
}
|
94
106
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Karafka
|
4
|
+
module Testing
|
5
|
+
module RSpec
|
6
|
+
# Proxy object for a nicer `karafka.` API within RSpec
|
7
|
+
class Proxy
|
8
|
+
# @param rspec_example [RSpec::ExampleGroups] rspec context
|
9
|
+
def initialize(rspec_example)
|
10
|
+
@rspec_example = rspec_example
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param args Anything that the `#karafka_consumer_for` accepts
|
14
|
+
def consumer_for(*args)
|
15
|
+
@rspec_example.karafka_consumer_for(*args)
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param args Anything that the `#karafka_publish` accepts
|
19
|
+
def publish(*args)
|
20
|
+
@rspec_example.karafka_publish(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
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:
|
4
|
+
version: 2.0.0.alpha1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maciej Mensfeld
|
@@ -34,7 +34,7 @@ cert_chain:
|
|
34
34
|
R2P11bWoCtr70BsccVrN8jEhzwXngMyI2gVt750Y+dbTu1KgRqZKp/ECe7ZzPzXj
|
35
35
|
pIy9vHxTANKYVyI4qj8OrFdEM5BQNu8oQpL0iQ==
|
36
36
|
-----END CERTIFICATE-----
|
37
|
-
date:
|
37
|
+
date: 2022-01-30 00:00:00.000000000 Z
|
38
38
|
dependencies:
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: karafka
|
@@ -42,14 +42,14 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - "~>"
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
45
|
+
version: 2.0.alpha1
|
46
46
|
type: :runtime
|
47
47
|
prerelease: false
|
48
48
|
version_requirements: !ruby/object:Gem::Requirement
|
49
49
|
requirements:
|
50
50
|
- - "~>"
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version:
|
52
|
+
version: 2.0.alpha1
|
53
53
|
description: Library which provides helpers for easier Karafka consumers tests
|
54
54
|
email:
|
55
55
|
- maciej@mensfeld.pl
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- ".rspec"
|
65
65
|
- ".ruby-gemset"
|
66
66
|
- ".ruby-version"
|
67
|
+
- 2.0-Upgrade.md
|
67
68
|
- CHANGELOG.md
|
68
69
|
- Gemfile
|
69
70
|
- Gemfile.lock
|
@@ -76,6 +77,7 @@ files:
|
|
76
77
|
- lib/karafka/testing/dummy_client.rb
|
77
78
|
- lib/karafka/testing/errors.rb
|
78
79
|
- lib/karafka/testing/rspec/helpers.rb
|
80
|
+
- lib/karafka/testing/rspec/proxy.rb
|
79
81
|
- lib/karafka/testing/version.rb
|
80
82
|
homepage: https://karafka.io
|
81
83
|
licenses:
|
@@ -93,11 +95,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
95
|
version: 2.6.0
|
94
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
97
|
requirements:
|
96
|
-
- - "
|
98
|
+
- - ">"
|
97
99
|
- !ruby/object:Gem::Version
|
98
|
-
version:
|
100
|
+
version: 1.3.1
|
99
101
|
requirements: []
|
100
|
-
rubygems_version: 3.
|
102
|
+
rubygems_version: 3.3.4
|
101
103
|
signing_key:
|
102
104
|
specification_version: 4
|
103
105
|
summary: Library which provides helpers for easier Karafka consumers tests
|
metadata.gz.sig
CHANGED
Binary file
|