karafka-testing 1.4.5 → 2.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85d3cffd259d39159ccaa81dc8431a39c503c043bbaa505daa88e6bc1f502e9d
4
- data.tar.gz: 14643e21014bffe57f574e8e4faec7e95c1c14156dcee06cf58dd5f10f4cf86c
3
+ metadata.gz: 99b31b45b503aded03f25274fc7819219b3707644cbfa57a6136426351b52ae1
4
+ data.tar.gz: 1321c4e974d9dcd25f9f38fb0b8237a503d3f4399cc08ea299820d6dd8ce2479
5
5
  SHA512:
6
- metadata.gz: 8d2cdad297155202934fb1c6504f01d0ad27712772e01a7fa93cf29acbd1b06e97bca22aa54bd8a27a613b3930fc4cafa4c6e6ae0c972fab2a81617f90a06298
7
- data.tar.gz: 11eef076f62f72d505e51e316a9730a0ef91f5bdf6bf767f6ba4738bcfec37842e6a4b37ff4296b6636dcb412543f487f6dcef75b0992a529d5fda8a04a9fc8d
6
+ metadata.gz: aeb543efd92f2c9c7cafb50bb389e8d0baf388ce086cbe8751b2a2362d1e666c96766f771ff3fe20d38fcc9d3f8b69563073d4709fc93d756c880cbf4c6646e1
7
+ data.tar.gz: c5eb1aaeeedeb7ab8c08e77998422b5a249069818b50abc842e9cf0d9599b59f07d88167e71c4d9791f62e979babb15b659816fb9249a8927e11e65909d40e1f
checksums.yaml.gz.sig CHANGED
Binary file
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,14 +1,9 @@
1
1
  # Karafka Test gem changelog
2
2
 
3
- ## 1.4.5 (2022-02-19)
3
+ ## 2.0.0.alpha1 (2022-01-30)
4
+ - Change the API to be more comprehensive
5
+ - Update to work with Karafka 2.0
4
6
  - Support for Ruby 3.1
5
- - Add `rubygems_mfa_required`
6
-
7
- ## 1.4.4 (2021-12-05)
8
- - Source code metadata url added to the gemspec
9
-
10
- ## 1.4.3 (2021-11-27)
11
- - #71 - `mark_as_consumed!` is not stubbed, causing `Karafka::Errors::MissingClientError`
12
7
 
13
8
  ## 1.4.2 (2021-04-21)
14
9
  - Restore MIT license
data/Gemfile.lock CHANGED
@@ -1,18 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- karafka-testing (1.4.5)
5
- karafka (~> 1.4.0)
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
- delivery_boy (1.1.0)
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
11
  dry-configurable (0.14.0)
17
12
  concurrent-ruby (~> 1.0)
18
13
  dry-core (~> 0.6)
@@ -33,7 +28,7 @@ GEM
33
28
  dry-configurable (~> 0.13, >= 0.13.0)
34
29
  dry-core (~> 0.5, >= 0.5)
35
30
  dry-events (~> 0.2)
36
- dry-schema (1.9.1)
31
+ dry-schema (1.8.0)
37
32
  concurrent-ruby (~> 1.0)
38
33
  dry-configurable (~> 0.13, >= 0.13.0)
39
34
  dry-core (~> 0.5, >= 0.5)
@@ -46,36 +41,35 @@ GEM
46
41
  dry-core (~> 0.5, >= 0.5)
47
42
  dry-inflector (~> 0.1, >= 0.1.2)
48
43
  dry-logic (~> 1.0, >= 1.0.2)
49
- dry-validation (1.8.0)
44
+ dry-validation (1.7.0)
50
45
  concurrent-ruby (~> 1.0)
51
46
  dry-container (~> 0.7, >= 0.7.1)
52
47
  dry-core (~> 0.5, >= 0.5)
53
48
  dry-initializer (~> 3.0)
54
- dry-schema (~> 1.9, >= 1.9.1)
55
- envlogic (1.1.4)
56
- dry-inflector (~> 0.1)
57
- karafka (1.4.13)
49
+ dry-schema (~> 1.8, >= 1.8.0)
50
+ ffi (1.15.5)
51
+ karafka (2.0.0.alpha1)
58
52
  dry-configurable (~> 0.13)
59
- dry-inflector (~> 0.2)
60
53
  dry-monitor (~> 0.5)
61
54
  dry-validation (~> 1.7)
62
- envlogic (~> 1.1)
63
- ruby-kafka (>= 1.3.0)
64
- thor (>= 1.1)
65
- waterdrop (~> 1.4)
66
- zeitwerk (~> 2.4)
67
- 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)
68
60
  rake (13.0.6)
69
- ruby-kafka (1.4.0)
70
- digest-crc
61
+ rdkafka (0.11.1)
62
+ ffi (~> 1.15)
63
+ mini_portile2 (~> 2.6)
64
+ rake (> 12)
71
65
  thor (1.2.1)
72
- waterdrop (1.4.4)
73
- delivery_boy (>= 0.2, < 2.x)
66
+ waterdrop (2.1.0)
67
+ concurrent-ruby (>= 1.1)
74
68
  dry-configurable (~> 0.13)
75
69
  dry-monitor (~> 0.5)
76
70
  dry-validation (~> 1.7)
77
- ruby-kafka (>= 1.3.0)
78
- zeitwerk (~> 2.4)
71
+ rdkafka (>= 0.10)
72
+ zeitwerk (~> 2.3)
79
73
  zeitwerk (2.5.4)
80
74
 
81
75
  PLATFORMS
@@ -85,4 +79,4 @@ DEPENDENCIES
85
79
  karafka-testing!
86
80
 
87
81
  BUNDLED WITH
88
- 2.3.6
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 `#publish_for_karafka` 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.
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) { karafka_consumer_for(:inline_batch_data) }
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
- publish_for_karafka({ 'number' => nr1_value }.to_json)
53
+ karafka.publish({ 'number' => nr1_value }.to_json)
51
54
  # Sends second message to Karafka consumer
52
- publish_for_karafka({ 'number' => nr2_value }.to_json)
55
+ karafka.publish({ 'number' => nr2_value }.to_json, partition: 2)
53
56
  allow(Karafka.logger).to receive(:info)
54
57
  end
55
58
 
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.require_paths = %w[lib]
21
21
  spec.cert_chain = %w[certs/mensfeld.pem]
22
+ spec.metadata = { 'source_code_uri' => 'https://github.com/karafka/testing' }
22
23
 
23
24
  spec.required_ruby_version = '>= 2.6.0'
24
25
 
@@ -26,10 +27,5 @@ Gem::Specification.new do |spec|
26
27
  spec.signing_key = File.expand_path('~/.ssh/gem-private_key.pem')
27
28
  end
28
29
 
29
- spec.add_dependency 'karafka', '~> 1.4.0'
30
-
31
- spec.metadata = {
32
- 'source_code_uri' => 'https://github.com/karafka/karafka',
33
- 'rubygems_mfa_required' => 'true'
34
- }
30
+ spec.add_dependency 'karafka', '~> 2.0.alpha1'
35
31
  end
@@ -9,8 +9,6 @@ module Karafka
9
9
  %i[
10
10
  mark_as_consumed
11
11
  mark_as_consumed!
12
- trigger_heartbeat
13
- trigger_heartbeat!
14
12
  ].each do |caught_delegator|
15
13
  define_method(caught_delegator) { |*| }
16
14
  end
@@ -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(:_karafka_raw_data) { [] }
20
- # Inject a dummy client that will intercept commands directed to the kafka client
21
- base.before { Persistence::Client.write(DummyClient.new) }
22
- # Clear the messages buffer after each spec, so nothing will leak
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) { karafka_consumer_for(:my_requested_topic) }
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.0. This won't work without the global state
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(selected_topic)
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 params instance with given payload and options into an internal
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 raw_payload [String] anything you want to send
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
- # publish_for_karafka({ 'hello' => 'world' }.to_json)
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
- # publish_for_karafka({ 'hello' => 'world' }.to_json, 'partition' => 6)
71
+ # karafka.publish({ 'hello' => 'world' }.to_json, 'partition' => 6)
69
72
  # end
70
- def publish_for_karafka(raw_payload, opts = {})
71
- metadata = Karafka::Params::Metadata.new(
72
- **metadata_defaults.merge(opts)
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
- _karafka_raw_data << Karafka::Params::Params.new(raw_payload, metadata)
76
- subject.params_batch = Karafka::Params::ParamsBatch.new(_karafka_raw_data)
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 metadata_defaults
95
+ def karafka_message_metadata_defaults
83
96
  {
84
97
  deserializer: subject.topic.deserializer,
85
- create_time: Time.now,
98
+ timestamp: Time.now,
86
99
  headers: {},
87
- is_control_record: false,
88
100
  key: nil,
89
- offset: 0,
101
+ offset: _karafka_messages.size,
90
102
  partition: 0,
91
- receive_time: Time.now,
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
@@ -4,6 +4,6 @@
4
4
  module Karafka
5
5
  module Testing
6
6
  # Current version of gem. It should match Karafka framework version
7
- VERSION = '1.4.5'
7
+ VERSION = '2.0.0.alpha1'
8
8
  end
9
9
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karafka-testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.5
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: 2022-02-19 00:00:00.000000000 Z
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: 1.4.0
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: 1.4.0
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,13 +77,13 @@ 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:
82
84
  - MIT
83
85
  metadata:
84
- source_code_uri: https://github.com/karafka/karafka
85
- rubygems_mfa_required: 'true'
86
+ source_code_uri: https://github.com/karafka/testing
86
87
  post_install_message:
87
88
  rdoc_options: []
88
89
  require_paths:
@@ -94,11 +95,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
95
  version: 2.6.0
95
96
  required_rubygems_version: !ruby/object:Gem::Requirement
96
97
  requirements:
97
- - - ">="
98
+ - - ">"
98
99
  - !ruby/object:Gem::Version
99
- version: '0'
100
+ version: 1.3.1
100
101
  requirements: []
101
- rubygems_version: 3.3.3
102
+ rubygems_version: 3.3.4
102
103
  signing_key:
103
104
  specification_version: 4
104
105
  summary: Library which provides helpers for easier Karafka consumers tests
metadata.gz.sig CHANGED
Binary file