kafka_command 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +179 -0
- data/.env +1 -0
- data/.env.test +1 -0
- data/.gitignore +41 -0
- data/.rspec +1 -0
- data/.rubocop.yml +12 -0
- data/.ruby-version +1 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +194 -0
- data/LICENSE +21 -0
- data/README.md +138 -0
- data/Rakefile +34 -0
- data/app/assets/config/manifest.js +3 -0
- data/app/assets/images/.keep +0 -0
- data/app/assets/images/kafka_command/cluster_view.png +0 -0
- data/app/assets/images/kafka_command/kafka.png +0 -0
- data/app/assets/images/kafka_command/topic_view.png +0 -0
- data/app/assets/javascripts/kafka_command/application.js +14 -0
- data/app/assets/stylesheets/kafka_command/application.css +27 -0
- data/app/assets/stylesheets/kafka_command/clusters.css +8 -0
- data/app/assets/stylesheets/kafka_command/topics.css +3 -0
- data/app/channels/application_cable/channel.rb +6 -0
- data/app/channels/application_cable/connection.rb +6 -0
- data/app/controllers/kafka_command/application_controller.rb +96 -0
- data/app/controllers/kafka_command/brokers_controller.rb +26 -0
- data/app/controllers/kafka_command/clusters_controller.rb +46 -0
- data/app/controllers/kafka_command/consumer_groups_controller.rb +44 -0
- data/app/controllers/kafka_command/topics_controller.rb +187 -0
- data/app/helpers/kafka_command/application_helper.rb +29 -0
- data/app/helpers/kafka_command/consumer_group_helper.rb +13 -0
- data/app/jobs/application_job.rb +6 -0
- data/app/mailers/application_mailer.rb +8 -0
- data/app/models/kafka_command/broker.rb +47 -0
- data/app/models/kafka_command/client.rb +102 -0
- data/app/models/kafka_command/cluster.rb +172 -0
- data/app/models/kafka_command/consumer_group.rb +142 -0
- data/app/models/kafka_command/consumer_group_partition.rb +23 -0
- data/app/models/kafka_command/group_member.rb +18 -0
- data/app/models/kafka_command/partition.rb +36 -0
- data/app/models/kafka_command/topic.rb +153 -0
- data/app/views/kafka_command/brokers/index.html.erb +38 -0
- data/app/views/kafka_command/clusters/_tabs.html.erb +9 -0
- data/app/views/kafka_command/clusters/index.html.erb +54 -0
- data/app/views/kafka_command/clusters/new.html.erb +115 -0
- data/app/views/kafka_command/configuration_error.html.erb +1 -0
- data/app/views/kafka_command/consumer_groups/index.html.erb +32 -0
- data/app/views/kafka_command/consumer_groups/show.html.erb +115 -0
- data/app/views/kafka_command/shared/_alert.html.erb +13 -0
- data/app/views/kafka_command/shared/_search_bar.html.erb +31 -0
- data/app/views/kafka_command/shared/_title.html.erb +6 -0
- data/app/views/kafka_command/topics/_form_fields.html.erb +49 -0
- data/app/views/kafka_command/topics/edit.html.erb +17 -0
- data/app/views/kafka_command/topics/index.html.erb +46 -0
- data/app/views/kafka_command/topics/new.html.erb +36 -0
- data/app/views/kafka_command/topics/show.html.erb +126 -0
- data/app/views/layouts/kafka_command/application.html.erb +50 -0
- data/bin/rails +16 -0
- data/config/initializers/kafka.rb +13 -0
- data/config/initializers/kafka_command.rb +11 -0
- data/config/routes.rb +11 -0
- data/docker-compose.yml +18 -0
- data/kafka_command.gemspec +27 -0
- data/lib/assets/.keep +0 -0
- data/lib/core_extensions/kafka/broker/attr_readers.rb +11 -0
- data/lib/core_extensions/kafka/broker_pool/attr_readers.rb +11 -0
- data/lib/core_extensions/kafka/client/attr_readers.rb +11 -0
- data/lib/core_extensions/kafka/cluster/attr_readers.rb +11 -0
- data/lib/core_extensions/kafka/protocol/metadata_response/partition_metadata/attr_readers.rb +15 -0
- data/lib/kafka_command/configuration.rb +150 -0
- data/lib/kafka_command/engine.rb +11 -0
- data/lib/kafka_command/errors.rb +6 -0
- data/lib/kafka_command/version.rb +5 -0
- data/lib/kafka_command.rb +13 -0
- data/lib/tasks/.keep +0 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +4 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +36 -0
- data/spec/dummy/bin/update +31 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config/application.rb +19 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +61 -0
- data/spec/dummy/config/environments/production.rb +94 -0
- data/spec/dummy/config/environments/test.rb +46 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +14 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +25 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/kafka_command.yml +18 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +34 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/config/ssl/test_ca_cert +1 -0
- data/spec/dummy/config/ssl/test_client_cert +1 -0
- data/spec/dummy/config/ssl/test_client_cert_key +1 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/db/schema.rb +42 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +0 -0
- data/spec/dummy/log/hey.log +0 -0
- data/spec/dummy/log/test.log +2227 -0
- data/spec/dummy/package.json +5 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/examples.txt +165 -0
- data/spec/fast_helper.rb +20 -0
- data/spec/fixtures/files/kafka_command_sasl.yml +10 -0
- data/spec/fixtures/files/kafka_command_ssl.yml +10 -0
- data/spec/fixtures/files/kafka_command_ssl_file_paths.yml +11 -0
- data/spec/fixtures/files/kafka_command_staging.yml +8 -0
- data/spec/lib/kafka_command/configuration_spec.rb +311 -0
- data/spec/models/kafka_command/broker_spec.rb +83 -0
- data/spec/models/kafka_command/client_spec.rb +306 -0
- data/spec/models/kafka_command/cluster_spec.rb +163 -0
- data/spec/models/kafka_command/consumer_group_partition_spec.rb +43 -0
- data/spec/models/kafka_command/consumer_group_spec.rb +236 -0
- data/spec/models/kafka_command/partition_spec.rb +95 -0
- data/spec/models/kafka_command/topic_spec.rb +311 -0
- data/spec/rails_helper.rb +63 -0
- data/spec/requests/json/brokers_spec.rb +50 -0
- data/spec/requests/json/clusters_spec.rb +58 -0
- data/spec/requests/json/consumer_groups_spec.rb +139 -0
- data/spec/requests/json/topics_spec.rb +274 -0
- data/spec/spec_helper.rb +109 -0
- data/spec/support/factory_bot.rb +5 -0
- data/spec/support/json_helper.rb +13 -0
- data/spec/support/kafka_helper.rb +93 -0
- metadata +326 -0
@@ -0,0 +1,163 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe KafkaCommand::Cluster do
|
4
|
+
let(:cluster) { described_class.all.first }
|
5
|
+
let(:topic_name) { SecureRandom.hex(12) }
|
6
|
+
|
7
|
+
describe '#new' do
|
8
|
+
let(:brokers) { ENV['SEED_BROKERS'].split(',') }
|
9
|
+
|
10
|
+
context 'plaintext' do
|
11
|
+
it 'creates a kafka client with brokers and client id' do
|
12
|
+
expect(KafkaCommand::Client).to receive(:new).with(
|
13
|
+
hash_including(
|
14
|
+
brokers: brokers,
|
15
|
+
client_id: 'test_cluster'
|
16
|
+
)
|
17
|
+
).at_least(:once)
|
18
|
+
|
19
|
+
cluster
|
20
|
+
|
21
|
+
expect(cluster.ssl?).to eq(false)
|
22
|
+
expect(cluster.sasl?).to eq(false)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'sasl config' do
|
27
|
+
before do
|
28
|
+
allow(KafkaCommand).to receive(:config).and_return(
|
29
|
+
KafkaCommand::Configuration.new(KafkaCommand::Configuration.parse_yaml('spec/fixtures/files/kafka_command_sasl.yml'))
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'creates a kafka client with brokers, client_id, and sasl params' do
|
34
|
+
expect(KafkaCommand::Client).to receive(:new).with(
|
35
|
+
brokers: brokers,
|
36
|
+
client_id: 'sasl_test_cluster',
|
37
|
+
sasl_scram_username: 'test',
|
38
|
+
sasl_scram_password: 'test',
|
39
|
+
sasl_scram_mechanism: 'sha256',
|
40
|
+
ssl_ca_cert: 'test_ca_cert'
|
41
|
+
)
|
42
|
+
|
43
|
+
cluster
|
44
|
+
|
45
|
+
expect(cluster.sasl?).to eq(true)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'ssl' do
|
50
|
+
before do
|
51
|
+
allow(KafkaCommand).to receive(:config).and_return(
|
52
|
+
KafkaCommand::Configuration.new(KafkaCommand::Configuration.parse_yaml('spec/fixtures/files/kafka_command_ssl.yml'))
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'creates a kafka client with brokers, client_id, and ssl params' do
|
57
|
+
expect(KafkaCommand::Client).to receive(:new).with(
|
58
|
+
brokers: brokers,
|
59
|
+
client_id: 'ssl_test_cluster',
|
60
|
+
ssl_client_cert: 'test_client_cert',
|
61
|
+
ssl_client_cert_key: 'test_client_cert_key',
|
62
|
+
ssl_ca_cert: 'test_ca_cert'
|
63
|
+
)
|
64
|
+
|
65
|
+
cluster
|
66
|
+
|
67
|
+
expect(cluster.ssl?).to eq(true)
|
68
|
+
expect(cluster.sasl?).to eq(false)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#create_topic' do
|
74
|
+
let(:kwargs) do
|
75
|
+
{ replication_factor: 1, num_partitions: 1 }
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'calls KafkaCommand::Client#create_topic' do
|
79
|
+
expect_any_instance_of(KafkaCommand::Client).to receive(:create_topic).with(topic_name, **kwargs)
|
80
|
+
cluster.create_topic(topic_name, **kwargs)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'creates a topic' do
|
84
|
+
topic_count = cluster.topics.count
|
85
|
+
topic = cluster.create_topic(topic_name, **kwargs)
|
86
|
+
expect(topic).to be_an_instance_of(KafkaCommand::Topic)
|
87
|
+
cluster.client.refresh_topics!
|
88
|
+
expect(cluster.topics.count).to eq(topic_count + 1)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#to_s' do
|
93
|
+
it 'returns the name' do
|
94
|
+
expect(cluster.to_s).to eq(cluster.name)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '.all' do
|
99
|
+
it "returns a list of #{described_class}" do
|
100
|
+
described_class.all.each do |c|
|
101
|
+
expect(c).to be_an_instance_of(described_class)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe '.find' do
|
107
|
+
context 'cluster exists' do
|
108
|
+
it 'finds the cluster' do
|
109
|
+
expect(described_class.find(cluster.name)).to eq(cluster)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'cluster does not exist' do
|
114
|
+
it 'returns nil' do
|
115
|
+
expect(described_class.find('doesnotexists')).to be_nil
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'forwarding' do
|
121
|
+
describe '#topics' do
|
122
|
+
before { create_topic(topic_name) }
|
123
|
+
|
124
|
+
it 'fowards the call to the KafkaCommand::Client' do
|
125
|
+
expect(cluster.client).to receive(:topics).once
|
126
|
+
cluster.topics
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'returns a list of topics' do
|
130
|
+
expect(cluster.topics.first).to be_an_instance_of(KafkaCommand::Topic)
|
131
|
+
expect(cluster.topics.map(&:name)).to include(topic_name)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe '#brokers' do
|
136
|
+
it 'fowards the call to the KafkaCommand::Client' do
|
137
|
+
expect(cluster.client).to receive(:brokers).once
|
138
|
+
cluster.brokers
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'returns a list of brokers' do
|
142
|
+
expect(cluster.brokers.first).to be_an_instance_of(KafkaCommand::Broker)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe '#groups' do
|
147
|
+
before { create_topic(topic_name) }
|
148
|
+
let(:group_id) { SecureRandom.hex(12) }
|
149
|
+
|
150
|
+
it 'fowards to the call to the KafkaCommand::Client' do
|
151
|
+
expect(cluster.client).to receive(:groups).once
|
152
|
+
cluster.groups
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'returns a list of groups' do
|
156
|
+
run_consumer_group(topic_name, group_id) do
|
157
|
+
expect(cluster.groups.first).to be_an_instance_of(KafkaCommand::ConsumerGroup)
|
158
|
+
expect(cluster.groups.map(&:group_id)).to include(group_id)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe KafkaCommand::ConsumerGroupPartition do
|
4
|
+
let(:group_id) { "group-#{SecureRandom.hex(12)}" }
|
5
|
+
let(:topic_name) { "test-#{SecureRandom.hex(12)}" }
|
6
|
+
let(:lag) { 1 }
|
7
|
+
let(:offset) { 200 }
|
8
|
+
let(:partition_id) { 1 }
|
9
|
+
|
10
|
+
let(:consumer_group_partition_) do
|
11
|
+
described_class.new(
|
12
|
+
group_id: group_id,
|
13
|
+
lag: lag,
|
14
|
+
offset: offset,
|
15
|
+
topic_name: topic_name,
|
16
|
+
partition_id: partition_id
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#new' do
|
21
|
+
it 'creates a new Kafka::ConsumerGroupPartition with attributes' do
|
22
|
+
expect(consumer_group_partition_.lag).to eq(lag)
|
23
|
+
expect(consumer_group_partition_.group_id).to eq(group_id)
|
24
|
+
expect(consumer_group_partition_.topic_name).to eq(topic_name)
|
25
|
+
expect(consumer_group_partition_.offset).to eq(offset)
|
26
|
+
expect(consumer_group_partition_.partition_id).to eq(partition_id)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#as_json' do
|
31
|
+
let(:expected_json) do
|
32
|
+
{
|
33
|
+
lag: lag,
|
34
|
+
offset: offset,
|
35
|
+
partition_id: partition_id
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns the expected payload' do
|
40
|
+
expect(consumer_group_partition_.as_json).to eq(expected_json)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,236 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe KafkaCommand::ConsumerGroup do
|
4
|
+
let(:group_id) { "test-group-#{SecureRandom.hex(24)}" }
|
5
|
+
let(:topic_name) { "test-topic-#{SecureRandom.hex(24)}" }
|
6
|
+
let(:num_partitions) { 5 }
|
7
|
+
let(:group) do
|
8
|
+
KafkaCommand::Cluster.all.first.groups.find { |g| g.group_id == group_id }
|
9
|
+
end
|
10
|
+
|
11
|
+
before { create_topic(topic_name, num_partitions: num_partitions) }
|
12
|
+
|
13
|
+
describe '#new' do
|
14
|
+
describe 'running' do
|
15
|
+
it 'initializes a stable Kafka::ConsumerGroup with group_id, members, and state' do
|
16
|
+
run_consumer_group(topic_name, group_id) do
|
17
|
+
expect(group.group_id).to eq(group_id)
|
18
|
+
expect(group.members).to_not be_empty
|
19
|
+
expect(group.members.sample).to be_an_instance_of(KafkaCommand::GroupMember)
|
20
|
+
expect(group.state).to eq('Stable')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'dormant' do
|
26
|
+
before { run_consumer_group(topic_name, group_id) }
|
27
|
+
|
28
|
+
it 'initializes an empty Kafka::ConsumerGroup with group_id, members, and state' do
|
29
|
+
expect(group.group_id).to eq(group_id)
|
30
|
+
expect(group.members).to be_empty
|
31
|
+
expect(group.state).to eq('Empty')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#refresh!' do
|
37
|
+
it 'refreshes the group metadata' do
|
38
|
+
run_consumer_group(topic_name, group_id) do
|
39
|
+
expect(group.state).to eq('Stable')
|
40
|
+
expect(group.members).to_not be_empty
|
41
|
+
end
|
42
|
+
|
43
|
+
group.refresh!
|
44
|
+
expect(group.members).to be_empty
|
45
|
+
expect(group.state).to eq('Empty')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#stable?' do
|
50
|
+
describe 'running' do
|
51
|
+
it 'returns true' do
|
52
|
+
run_consumer_group(topic_name, group_id) do
|
53
|
+
expect(group.stable?).to eq(true)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'dormant' do
|
59
|
+
before { run_consumer_group(topic_name, group_id) }
|
60
|
+
|
61
|
+
it 'returns false' do
|
62
|
+
expect(group.stable?).to eq(false)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#stable?' do
|
68
|
+
describe 'running' do
|
69
|
+
it 'returns true' do
|
70
|
+
run_consumer_group(topic_name, group_id) do
|
71
|
+
expect(group.stable?).to eq(true)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'dormant' do
|
77
|
+
before { run_consumer_group(topic_name, group_id) }
|
78
|
+
|
79
|
+
it 'returns false' do
|
80
|
+
expect(group.stable?).to eq(false)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#empty?' do
|
86
|
+
describe 'running' do
|
87
|
+
it 'returns false' do
|
88
|
+
run_consumer_group(topic_name, group_id) do
|
89
|
+
expect(group.empty?).to eq(false)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe 'dormant' do
|
95
|
+
before { run_consumer_group(topic_name, group_id) }
|
96
|
+
|
97
|
+
it 'returns true' do
|
98
|
+
expect(group.empty?).to eq(true)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe '#partitions_for' do
|
104
|
+
let(:num_partitions) { 3 }
|
105
|
+
let(:partitions) { group.partitions_for(topic_name) }
|
106
|
+
let(:total_lag) { partitions.map(&:lag).compact.reduce(:+) }
|
107
|
+
|
108
|
+
it 'returns ConsumerGroupPartitions' do
|
109
|
+
run_consumer_group(topic_name, group_id) do
|
110
|
+
expect(partitions.count).to eq(num_partitions)
|
111
|
+
expect(partitions.sample).to be_an_instance_of(KafkaCommand::ConsumerGroupPartition)
|
112
|
+
expect(partitions.sample.group_id).to eq(group_id)
|
113
|
+
expect(partitions.sample.topic_name).to eq(topic_name)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'some partitions no offsets' do
|
118
|
+
before { run_consumer_group(topic_name, group_id) }
|
119
|
+
|
120
|
+
it 'has "nil" lag' do
|
121
|
+
expect(partitions.map(&:lag)).to include(nil)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'lag' do
|
126
|
+
let(:partition_0) do
|
127
|
+
partitions.find { |p| p.partition_id == 0 }
|
128
|
+
end
|
129
|
+
|
130
|
+
let(:partition_1) do
|
131
|
+
partitions.find { |p| p.partition_id == 1 }
|
132
|
+
end
|
133
|
+
|
134
|
+
let(:partition_2) do
|
135
|
+
partitions.find { |p| p.partition_id == 2 }
|
136
|
+
end
|
137
|
+
|
138
|
+
before do
|
139
|
+
deliver_message('test', topic: topic_name, partition: 0)
|
140
|
+
deliver_message('test1', topic: topic_name, partition: 1)
|
141
|
+
deliver_message('test2', topic: topic_name, partition: 2)
|
142
|
+
run_consumer_group(topic_name, group_id, num_messages_to_consume: 3)
|
143
|
+
end
|
144
|
+
|
145
|
+
describe 'no lag' do
|
146
|
+
it 'has no lag' do
|
147
|
+
expect(partition_0.lag).to eq(0)
|
148
|
+
expect(partition_1.lag).to eq(0)
|
149
|
+
expect(partition_2.lag).to eq(0)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe 'some lag' do
|
154
|
+
before do
|
155
|
+
2.times { deliver_message('test', topic: topic_name, partition: 0) }
|
156
|
+
3.times { deliver_message('test1', topic: topic_name, partition: 1) }
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'has lag' do
|
160
|
+
expect(total_lag).to eq(5)
|
161
|
+
expect(partition_0.lag).to eq(2)
|
162
|
+
expect(partition_1.lag).to eq(3)
|
163
|
+
expect(partition_2.lag).to eq(0)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe '#consumed_topics' do
|
170
|
+
context 'running' do
|
171
|
+
it 'returns a list of topics' do
|
172
|
+
run_consumer_group(topic_name, group_id) do
|
173
|
+
expect(group.consumed_topics.map(&:name)).to include(topic_name)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context 'dormant' do
|
179
|
+
before { run_consumer_group(topic_name, group_id) }
|
180
|
+
|
181
|
+
it 'returns an empty list' do
|
182
|
+
expect(group.consumed_topics).to be_empty
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe '#as_json' do
|
188
|
+
let(:num_partitions) { 3 }
|
189
|
+
let(:partitions) { group.partitions_for(topic_name) }
|
190
|
+
|
191
|
+
|
192
|
+
before do
|
193
|
+
deliver_message('test', topic: topic_name, partition: 0)
|
194
|
+
deliver_message('test1', topic: topic_name, partition: 1)
|
195
|
+
deliver_message('test2', topic: topic_name, partition: 2)
|
196
|
+
run_consumer_group(topic_name, group_id, num_messages_to_consume: 3)
|
197
|
+
2.times { deliver_message('test', topic: topic_name, partition: 0) }
|
198
|
+
3.times { deliver_message('test1', topic: topic_name, partition: 1) }
|
199
|
+
end
|
200
|
+
|
201
|
+
context 'running' do
|
202
|
+
let(:expected_json) do
|
203
|
+
{
|
204
|
+
group_id: group_id,
|
205
|
+
topics: [
|
206
|
+
{
|
207
|
+
name: topic_name,
|
208
|
+
partitions: partitions.map(&:as_json)
|
209
|
+
}
|
210
|
+
],
|
211
|
+
state: 'Stable'
|
212
|
+
}
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'returns the expected payload' do
|
216
|
+
run_consumer_group(topic_name, group_id) do
|
217
|
+
expect(group.as_json).to eq(expected_json)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'dormant' do
|
223
|
+
let(:expected_json) do
|
224
|
+
{
|
225
|
+
group_id: group_id,
|
226
|
+
topics: [],
|
227
|
+
state: 'Empty'
|
228
|
+
}
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'returns the expected payload' do
|
232
|
+
expect(group.as_json).to eq(expected_json)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe KafkaCommand::Partition do
|
4
|
+
let(:topic_name) { "test-#{SecureRandom.hex(12)}" }
|
5
|
+
subject do
|
6
|
+
KafkaCommand::Cluster.all.first.topics.find { |t| t.name == topic_name }.partitions.sample
|
7
|
+
end
|
8
|
+
|
9
|
+
before { create_topic(topic_name) }
|
10
|
+
|
11
|
+
describe '#new' do
|
12
|
+
it 'contains a reference to a topic wrapper' do
|
13
|
+
expect(subject.topic).to be_an_instance_of(KafkaCommand::Topic)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#highwater_mark_offset' do
|
18
|
+
let(:num_messages) { 2 }
|
19
|
+
|
20
|
+
before do
|
21
|
+
num_messages.times { deliver_message('test', topic: topic_name) }
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'retrieves the latest offset' do
|
25
|
+
expect(subject.highwater_mark_offset).to eq(num_messages)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#as_json' do
|
30
|
+
let(:expected_json) do
|
31
|
+
{
|
32
|
+
isr: subject.isr,
|
33
|
+
leader: subject.leader,
|
34
|
+
id: subject.partition_id,
|
35
|
+
highwater_mark_offset: subject.highwater_mark_offset
|
36
|
+
}.to_json
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'equals the expected json' do
|
40
|
+
expect(subject.as_json.to_json).to eq(expected_json)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'forwarding' do
|
45
|
+
let(:partition_metadata) do
|
46
|
+
subject.instance_variable_get(:@partition_metadata)
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#isr' do
|
50
|
+
it 'forwards isr to the partition metadata' do
|
51
|
+
expect(partition_metadata).to receive(:isr)
|
52
|
+
subject.isr
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'returns the isr' do
|
56
|
+
expect(subject.isr).to be_an_instance_of(Array)
|
57
|
+
expect(subject.isr.first).to be_an_instance_of(Integer)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#leader' do
|
62
|
+
it 'forwards leader to the partition metadata' do
|
63
|
+
expect(partition_metadata).to receive(:leader)
|
64
|
+
subject.leader
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns the leader broker' do
|
68
|
+
expect(subject.leader).to be_an_instance_of(Integer)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#partition_id' do
|
73
|
+
it 'forwards leader to the partition metadata' do
|
74
|
+
expect(partition_metadata).to receive(:leader)
|
75
|
+
subject.leader
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'returns the partition id' do
|
79
|
+
expect(subject.partition_id).to be > -1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#replicas' do
|
84
|
+
it 'forwards replicas to the partition metadata' do
|
85
|
+
expect(partition_metadata).to receive(:replicas)
|
86
|
+
subject.replicas
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'returns the replica node ids' do
|
90
|
+
expect(subject.replicas).to be_an_instance_of(Array)
|
91
|
+
expect(subject.replicas.first).to be_an_instance_of(Integer)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|