fake-kafka 0.0.1.pre.beta4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c7c0173148a3e19aec4d000b07d1722b0ad251bd2f30966ee650c7fd66d8e7a
4
+ data.tar.gz: 6293413cf7b847f93c9aeb2ddf1786083f01454b44363094b4657b4f10bc4641
5
+ SHA512:
6
+ metadata.gz: 5a22c0ed2c542b7884933a6c229e76ee6305f3365731d2880cd86511e8c1b83f4caa0983acbadb00c57a197b24303433aa2e472c9008d85eab962206026ee37d
7
+ data.tar.gz: 1b49b8aedf524975fe08de1e2abecb10b7254c6d463f56d302fd439c2b856da6edc8d8070f39a81a101be2d1c7589036b29d8ea9a3b29debd29816600c066558
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fake-kafka.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fake-kafka (0.0.1.pre.beta1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ fake-kafka!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
@@ -0,0 +1,101 @@
1
+ # Fake::Kafka
2
+
3
+ It is a drop in replacement for ruby-kafka driver, it works as In-memory driver, useful for development and test environments
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fake-kafka', git: 'git@github.com:catawiki/fake-kafka.git', tag: '0.0.1-beta4'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fake-kafka
20
+
21
+ ## Usage
22
+
23
+ ### Testing
24
+
25
+ Add the following lines to replace your kafka driver with the in-memory one.
26
+
27
+ For example for catbus add the following on you `spec/rails_helper.rb`
28
+
29
+ ```ruby
30
+ require 'fake/kafka'
31
+ Catbus.kafka = Fake::Kafka.new
32
+ ```
33
+
34
+ #### Cleaning kafka memory (AKA 'databasecleaner')
35
+
36
+ ```ruby
37
+ kafka.reset!
38
+ ```
39
+
40
+ With catbus and rspec you can do something like this within your rails_helper.rb
41
+
42
+ ```ruby
43
+ RSpec.configure do |config|
44
+ ...
45
+ config.around(:each) do |example|
46
+ ...
47
+ Catbus.kafka.reset!
48
+ ...
49
+ end
50
+ end
51
+
52
+ ```
53
+
54
+ #### Testing a consumer
55
+
56
+ ```ruby
57
+ describe 'consume' do
58
+ let(:payload) do
59
+ {
60
+ payload: {
61
+ YOUR_CONTENT_HERE
62
+ },
63
+ event_name: EVENT_NAME
64
+ }
65
+ end
66
+ before do
67
+ Catbus.kafka.deliver_message payload, topic: 'test_YOUR_TOPIC'
68
+ end
69
+ it 'should consume message' do
70
+ expect(Resque).to receive(:enqueue).with(described_class, payload.to_json)
71
+ Catbus.consumer.send(:consume)
72
+ end
73
+ ```
74
+
75
+ #### Testing a producer
76
+ ```ruby
77
+ describe 'produce' do
78
+ let(:payload) do
79
+ {
80
+ payload: {
81
+ YOUR_CONTENT_HERE
82
+ },
83
+ event_name: EVENT_NAME
84
+ }
85
+ end
86
+ it 'should consume message' do
87
+ expect(Catbus.kafka).to receive(:deliver_message).with(payload.to_json, topic: 'test_YOUR_TOPIC', key: nil)
88
+ YOUR_PRODUCER.process(PARAMS)
89
+ end
90
+ ```
91
+
92
+
93
+ ## Development
94
+
95
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
96
+
97
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
98
+
99
+ ## Contributing
100
+
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/catawiki/fake-kafka.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fake/kafka"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fake/kafka/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fake-kafka"
8
+ spec.version = Fake::Kafka::VERSION
9
+ spec.authors = ["Sebastian Arcila Valenzuela"]
10
+ spec.email = ["sebastianarcila@gmail.com"]
11
+
12
+ spec.summary = %q{Fake Kafka Consumer and Producer.}
13
+ spec.description = %q{In memory 'kafka' instruments design for testing integrations with kafka.}
14
+ spec.homepage = "https://github.com/catawiki/fake-kafka"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,44 @@
1
+ require 'fake/kafka/version'
2
+ require 'fake/kafka/batch'
3
+ require 'fake/kafka/message'
4
+ require 'fake/kafka/consumer'
5
+ require 'fake/kafka/producer'
6
+
7
+ module Fake
8
+ class Kafka
9
+ attr_reader :messages, :paused_partitions
10
+
11
+ def initialize(*options)
12
+ @messages = []
13
+ @paused_partitions = {}
14
+ end
15
+
16
+ def paused?(topic, partition)
17
+ @paused_partitions[topic] ||= {}
18
+ !!@paused_partitions[topic][partition]
19
+ end
20
+
21
+ def deliver_message(value, topic:, key: nil)
22
+ @messages << Message.new(value, key, topic, 0, 0)
23
+ end
24
+
25
+ def messages_in(topic)
26
+ messages.select {|message| message.topic == topic }
27
+ end
28
+
29
+ def consumer(*options)
30
+ Consumer.new(self)
31
+ end
32
+
33
+ def producer(*)
34
+ Producer.new(self)
35
+ end
36
+
37
+ # Used to clean in-memory data
38
+ # Useful between test runs
39
+ def reset!
40
+ @messages = []
41
+ @paused_partitions = {}
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ class Fake::Kafka::Batch
2
+ attr_reader :topic, :partition, :messages, :highwater_mark_offset
3
+
4
+ def initialize(topic:, partition:, messages:, highwater_mark_offset:)
5
+ @topic = topic
6
+ @partition = partition
7
+ @messages = messages
8
+ @highwater_mark_offset = highwater_mark_offset
9
+ end
10
+ end
@@ -0,0 +1,43 @@
1
+ class Fake::Kafka::Consumer
2
+ def initialize(kafka)
3
+ @kafka = kafka
4
+ @topics = []
5
+ end
6
+
7
+ def subscribe(topic, **)
8
+ @topics << topic.to_sym
9
+ end
10
+
11
+ def each_message(*options, &block)
12
+ @kafka.messages.each do |message|
13
+ next unless @topics.include?(message.topic.to_sym)
14
+
15
+ begin
16
+ block.call(message)
17
+ rescue StandardError => e
18
+ raise Kafka::ProcessingError.new(message.topic, message.partition, message.offset)
19
+ end
20
+ end
21
+ end
22
+
23
+ def each_batch(*options, &block)
24
+ begin
25
+ batch = Fake::Kafka::Batch.new(
26
+ topic: @kafka.messages.first.topic,
27
+ partition: @kafka.messages.first.partition,
28
+ messages: @kafka.messages,
29
+ highwater_mark_offset: @kafka.messages.first.offset
30
+ )
31
+ block.call(batch)
32
+ rescue StandardError => e
33
+ raise Kafka::ProcessingError.new(batch.topic, batch.partition, batch.highwater_mark_offset)
34
+ end
35
+ end
36
+
37
+ def pause(topic, partition, timeout:, max_timeout: nil, exponential_backoff: false)
38
+ @kafka.paused_partitions[topic] ||= {}
39
+ @kafka.paused_partitions[topic][partition] = true
40
+ end
41
+
42
+ def stop; end
43
+ end
@@ -0,0 +1,14 @@
1
+ class Fake::Kafka::Message
2
+ attr_reader :key, :topic, :partition, :offset
3
+ def initialize(value, key, topic, partition, offset)
4
+ @value = value
5
+ @key = key
6
+ @topic = topic
7
+ @partition = partition
8
+ @offset = offset
9
+ end
10
+
11
+ def value
12
+ @value.to_json
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ class Fake::Kafka::Producer
2
+ def initialize(kafka)
3
+ @kafka = kafka
4
+ @buffer = []
5
+ end
6
+
7
+ def produce(value, **options)
8
+ @buffer << [value, options]
9
+ end
10
+
11
+ def deliver_messages
12
+ @buffer.each do |value, **options|
13
+ @kafka.deliver_message(value.to_s, **options)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Fake
2
+ class Kafka
3
+ VERSION = "0.0.1-beta4"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fake-kafka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre.beta4
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Arcila Valenzuela
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: In memory 'kafka' instruments design for testing integrations with kafka.
56
+ email:
57
+ - sebastianarcila@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - fake-kafka.gemspec
72
+ - lib/fake/kafka.rb
73
+ - lib/fake/kafka/batch.rb
74
+ - lib/fake/kafka/consumer.rb
75
+ - lib/fake/kafka/message.rb
76
+ - lib/fake/kafka/producer.rb
77
+ - lib/fake/kafka/version.rb
78
+ homepage: https://github.com/catawiki/fake-kafka
79
+ licenses: []
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">"
93
+ - !ruby/object:Gem::Version
94
+ version: 1.3.1
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Fake Kafka Consumer and Producer.
100
+ test_files: []