karafka-testing 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +4 -4
- data/README.md +2 -107
- data/lib/karafka/testing/rspec/helpers.rb +1 -0
- data/lib/karafka/testing/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 560aa633f1c2710d530871e4194d0cb631540c2844a97207b05bfe7297dba13a
|
4
|
+
data.tar.gz: 6fb1dbb71bbb69f427356ee78b9ddc9fdeee83a9105564d7d9798ba6da3c6a70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d0f3c2504f22eb9d3d36ba620b5e4f439c97d19b5607931a32a8bbbdf014a1cc772e0436d889e7536006c46b53239b248c44bfc230a0b69ae6d5c91b2e623ce
|
7
|
+
data.tar.gz: 67f249bfd323f1c5f27bc97f78e33701a5cf6d42715c11e0b146857bf90ef7f00f307e4d1cb9e186947390c9fdff2bf98453fcc9483192a4a491a026d420e2f2
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
karafka-testing (2.0.
|
4
|
+
karafka-testing (2.0.4)
|
5
5
|
karafka (>= 2.0, < 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,13 +9,13 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
concurrent-ruby (1.1.10)
|
11
11
|
ffi (1.15.5)
|
12
|
-
karafka (2.0.
|
12
|
+
karafka (2.0.13)
|
13
13
|
karafka-core (>= 2.0.2, < 3.0.0)
|
14
14
|
rdkafka (>= 0.12)
|
15
15
|
thor (>= 0.20)
|
16
16
|
waterdrop (>= 2.4.1, < 3.0.0)
|
17
17
|
zeitwerk (~> 2.3)
|
18
|
-
karafka-core (2.0.
|
18
|
+
karafka-core (2.0.3)
|
19
19
|
concurrent-ruby (>= 1.1)
|
20
20
|
mini_portile2 (2.8.0)
|
21
21
|
rake (13.0.6)
|
@@ -28,7 +28,7 @@ GEM
|
|
28
28
|
karafka-core (>= 2.0.2, < 3.0.0)
|
29
29
|
rdkafka (>= 0.10)
|
30
30
|
zeitwerk (~> 2.3)
|
31
|
-
zeitwerk (2.6.
|
31
|
+
zeitwerk (2.6.1)
|
32
32
|
|
33
33
|
PLATFORMS
|
34
34
|
x86_64-linux
|
data/README.md
CHANGED
@@ -6,114 +6,9 @@
|
|
6
6
|
|
7
7
|
Karafka-Testing is a library that provides RSpec helpers, to make testing of Karafka consumers and producer much easier.
|
8
8
|
|
9
|
-
## Installation
|
9
|
+
## Installation and usage
|
10
10
|
|
11
|
-
|
12
|
-
```ruby
|
13
|
-
group :test do
|
14
|
-
gem 'karafka-testing'
|
15
|
-
gem 'rspec'
|
16
|
-
end
|
17
|
-
```
|
18
|
-
|
19
|
-
and then in your `spec_helper.rb` file:
|
20
|
-
|
21
|
-
```ruby
|
22
|
-
require 'karafka/testing/rspec/helpers'
|
23
|
-
|
24
|
-
RSpec.configure do |config|
|
25
|
-
config.include Karafka::Testing::RSpec::Helpers
|
26
|
-
end
|
27
|
-
```
|
28
|
-
|
29
|
-
## Usage
|
30
|
-
|
31
|
-
Once included in your RSpec setup, this library will provide you with a special `#karafka` object that contains three methods that you can use within your specs:
|
32
|
-
|
33
|
-
- `#consumer_for` - creates a consumer instance for the desired topic. It **needs** to be set as the spec subject.
|
34
|
-
- `#produce` - "sends" message to the consumer instance.
|
35
|
-
- `#produced_messages` - contains all the messages "sent" to Kafka during spec execution.
|
36
|
-
|
37
|
-
**Note:** Messages sent using the `#produce` method and directly from `Karafka.producer` won't be sent to Kafka. They will be buffered and accessible in a per-spec buffer in case you want to test messages production.
|
38
|
-
|
39
|
-
Messages that target the topic built using the `karafka#consumer_for` method will additionally be delivered to the consumer you want to test.
|
40
|
-
|
41
|
-
### Testing messages consumption (consumers)
|
42
|
-
|
43
|
-
```ruby
|
44
|
-
RSpec.describe InlineBatchConsumer do
|
45
|
-
# This will create a consumer instance with all the settings defined for the given topic
|
46
|
-
subject(:consumer) { karafka.consumer_for('inline_batch_data') }
|
47
|
-
|
48
|
-
let(:nr1_value) { rand }
|
49
|
-
let(:nr2_value) { rand }
|
50
|
-
let(:sum) { nr1_value + nr2_value }
|
51
|
-
|
52
|
-
before do
|
53
|
-
# Sends first message to Karafka consumer
|
54
|
-
karafka.produce({ 'number' => nr1_value }.to_json)
|
55
|
-
|
56
|
-
# Sends second message to Karafka consumer
|
57
|
-
karafka.produce({ 'number' => nr2_value }.to_json, partition: 2)
|
58
|
-
|
59
|
-
allow(Karafka.logger).to receive(:info)
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'expects to log a proper message' do
|
63
|
-
expect(Karafka.logger).to receive(:info).with("Sum of 2 elements equals to: #{sum}")
|
64
|
-
consumer.consume
|
65
|
-
end
|
66
|
-
end
|
67
|
-
```
|
68
|
-
|
69
|
-
If your consumers use `producer` to dispatch messages, you can check its operations as well:
|
70
|
-
|
71
|
-
```ruby
|
72
|
-
RSpec.describe InlineBatchConsumer do
|
73
|
-
subject(:consumer) { karafka.consumer_for(:inline_batch_data) }
|
74
|
-
|
75
|
-
before { karafka.produce({ 'number' => 1 }.to_json) }
|
76
|
-
|
77
|
-
it 'expects to dispatch async message to messages topic with value bigger by 1' do
|
78
|
-
consumer.consume
|
79
|
-
|
80
|
-
expect(karafka.produced_messages.last.payload).to eq({ number: 2 }.to_json)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
```
|
84
|
-
|
85
|
-
### Testing messages production (producer)
|
86
|
-
|
87
|
-
When running RSpec, Karafka will not dispatch messages to Kafka using `Karafka.producer` but will buffer them internally.
|
88
|
-
|
89
|
-
This means you can check your application flow, making sure your logic acts as expected:
|
90
|
-
|
91
|
-
```ruby
|
92
|
-
# Example class in which there is a message production
|
93
|
-
class UsersBuilder
|
94
|
-
def create(user_details)
|
95
|
-
user = ::User.create!(user_details)
|
96
|
-
|
97
|
-
Karafka.producer.produce_sync(
|
98
|
-
topic: 'users_changes',
|
99
|
-
payload: { user_id: user.id, type: 'user.created' },
|
100
|
-
key: user.id.to_s
|
101
|
-
)
|
102
|
-
|
103
|
-
user
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
RSpec.describe InlineBatchConsumer do
|
108
|
-
let(:created_user) { UsersBuilder.new.create(user_details) }
|
109
|
-
|
110
|
-
before { created_user }
|
111
|
-
|
112
|
-
it { expect(karafka.produced_messages.size).to eq(1) }
|
113
|
-
it { expect(karafka.produced_messages.first[:topic]).to eq('user.created') }
|
114
|
-
it { expect(karafka.produced_messages.first[:key]).to eq(created_user.id.to_s) }
|
115
|
-
end
|
116
|
-
```
|
11
|
+
`karafka-testing` docs are part of Karafka framework docs. You can find the testing section [here](https://karafka.io/docs/Testing/).
|
117
12
|
|
118
13
|
## Note on contributions
|
119
14
|
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: karafka-testing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maciej Mensfeld
|
@@ -35,7 +35,7 @@ cert_chain:
|
|
35
35
|
Qf04B9ceLUaC4fPVEz10FyobjaFoY4i32xRto3XnrzeAgfEe4swLq8bQsR3w/EF3
|
36
36
|
MGU0FeSV2Yj7Xc2x/7BzLK8xQn5l7Yy75iPF+KP3vVmDHnNl
|
37
37
|
-----END CERTIFICATE-----
|
38
|
-
date: 2022-
|
38
|
+
date: 2022-10-14 00:00:00.000000000 Z
|
39
39
|
dependencies:
|
40
40
|
- !ruby/object:Gem::Dependency
|
41
41
|
name: karafka
|
metadata.gz.sig
CHANGED
Binary file
|