rdkafka 0.13.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +388 -111
  3. data/{LICENSE → MIT-LICENSE} +2 -1
  4. data/README.md +90 -43
  5. data/Rakefile +21 -23
  6. data/bin/verify_kafka_warnings +44 -0
  7. data/dist/librdkafka-2.15.1.tar.gz +0 -0
  8. data/dist/patches/rdkafka_global_init.patch +15 -0
  9. data/docker-compose-ssl.yml +39 -0
  10. data/docker-compose.yml +22 -17
  11. data/ext/Rakefile +97 -30
  12. data/lib/rdkafka/abstract_handle.rb +113 -34
  13. data/lib/rdkafka/admin/acl_binding_result.rb +51 -0
  14. data/lib/rdkafka/admin/alter_consumer_group_offsets_handle.rb +31 -0
  15. data/lib/rdkafka/admin/alter_consumer_group_offsets_report.rb +50 -0
  16. data/lib/rdkafka/admin/config_binding_result.rb +30 -0
  17. data/lib/rdkafka/admin/config_resource_binding_result.rb +19 -0
  18. data/lib/rdkafka/admin/create_acl_handle.rb +32 -0
  19. data/lib/rdkafka/admin/create_acl_report.rb +26 -0
  20. data/lib/rdkafka/admin/create_partitions_handle.rb +28 -0
  21. data/lib/rdkafka/admin/create_partitions_report.rb +7 -0
  22. data/lib/rdkafka/admin/create_topic_handle.rb +8 -7
  23. data/lib/rdkafka/admin/create_topic_report.rb +4 -1
  24. data/lib/rdkafka/admin/delete_acl_handle.rb +31 -0
  25. data/lib/rdkafka/admin/delete_acl_report.rb +25 -0
  26. data/lib/rdkafka/admin/delete_consumer_group_offsets_handle.rb +31 -0
  27. data/lib/rdkafka/admin/delete_consumer_group_offsets_report.rb +50 -0
  28. data/lib/rdkafka/admin/delete_groups_handle.rb +31 -0
  29. data/lib/rdkafka/admin/delete_groups_report.rb +27 -0
  30. data/lib/rdkafka/admin/delete_records_handle.rb +31 -0
  31. data/lib/rdkafka/admin/delete_records_report.rb +25 -0
  32. data/lib/rdkafka/admin/delete_topic_handle.rb +8 -7
  33. data/lib/rdkafka/admin/delete_topic_report.rb +4 -1
  34. data/lib/rdkafka/admin/describe_acl_handle.rb +31 -0
  35. data/lib/rdkafka/admin/describe_acl_report.rb +27 -0
  36. data/lib/rdkafka/admin/describe_cluster_handle.rb +31 -0
  37. data/lib/rdkafka/admin/describe_cluster_report.rb +48 -0
  38. data/lib/rdkafka/admin/describe_configs_handle.rb +30 -0
  39. data/lib/rdkafka/admin/describe_configs_report.rb +56 -0
  40. data/lib/rdkafka/admin/describe_consumer_groups_handle.rb +31 -0
  41. data/lib/rdkafka/admin/describe_consumer_groups_report.rb +151 -0
  42. data/lib/rdkafka/admin/describe_result_parsing.rb +47 -0
  43. data/lib/rdkafka/admin/describe_topics_handle.rb +31 -0
  44. data/lib/rdkafka/admin/describe_topics_report.rb +118 -0
  45. data/lib/rdkafka/admin/incremental_alter_configs_handle.rb +30 -0
  46. data/lib/rdkafka/admin/incremental_alter_configs_report.rb +56 -0
  47. data/lib/rdkafka/admin/list_consumer_groups_handle.rb +31 -0
  48. data/lib/rdkafka/admin/list_consumer_groups_report.rb +83 -0
  49. data/lib/rdkafka/admin/list_offsets_handle.rb +31 -0
  50. data/lib/rdkafka/admin/list_offsets_report.rb +54 -0
  51. data/lib/rdkafka/admin.rb +1381 -38
  52. data/lib/rdkafka/bindings/admin/acls.rb +85 -0
  53. data/lib/rdkafka/bindings/admin/configs.rb +37 -0
  54. data/lib/rdkafka/bindings/admin/list_offsets.rb +26 -0
  55. data/lib/rdkafka/bindings/admin.rb +197 -0
  56. data/lib/rdkafka/bindings/callbacks.rb +100 -0
  57. data/lib/rdkafka/bindings/configuration.rb +50 -0
  58. data/lib/rdkafka/bindings/consumer.rb +83 -0
  59. data/lib/rdkafka/bindings/errors.rb +15 -0
  60. data/lib/rdkafka/bindings/handle.rb +16 -0
  61. data/lib/rdkafka/bindings/messages.rb +28 -0
  62. data/lib/rdkafka/bindings/metadata.rb +14 -0
  63. data/lib/rdkafka/bindings/polling.rb +17 -0
  64. data/lib/rdkafka/bindings/producer.rb +77 -0
  65. data/lib/rdkafka/bindings/structs.rb +62 -0
  66. data/lib/rdkafka/bindings.rb +69 -294
  67. data/lib/rdkafka/callbacks/alter_consumer_group_offsets_handler.rb +65 -0
  68. data/lib/rdkafka/callbacks/base_handler.rb +62 -0
  69. data/lib/rdkafka/callbacks/create_acl_handler.rb +37 -0
  70. data/lib/rdkafka/callbacks/create_partitions_handler.rb +37 -0
  71. data/lib/rdkafka/callbacks/create_topic_handler.rb +37 -0
  72. data/lib/rdkafka/callbacks/delete_acl_handler.rb +42 -0
  73. data/lib/rdkafka/callbacks/delete_consumer_group_offsets_handler.rb +65 -0
  74. data/lib/rdkafka/callbacks/delete_groups_handler.rb +37 -0
  75. data/lib/rdkafka/callbacks/delete_records_handler.rb +38 -0
  76. data/lib/rdkafka/callbacks/delete_topic_handler.rb +37 -0
  77. data/lib/rdkafka/callbacks/describe_acl_handler.rb +35 -0
  78. data/lib/rdkafka/callbacks/describe_cluster_handler.rb +36 -0
  79. data/lib/rdkafka/callbacks/describe_configs_handler.rb +42 -0
  80. data/lib/rdkafka/callbacks/describe_consumer_groups_handler.rb +36 -0
  81. data/lib/rdkafka/callbacks/describe_topics_handler.rb +36 -0
  82. data/lib/rdkafka/callbacks/incremental_alter_configs_handler.rb +42 -0
  83. data/lib/rdkafka/callbacks/list_consumer_groups_handler.rb +39 -0
  84. data/lib/rdkafka/callbacks/list_offsets_handler.rb +42 -0
  85. data/lib/rdkafka/callbacks.rb +273 -52
  86. data/lib/rdkafka/clients.rb +81 -0
  87. data/lib/rdkafka/config.rb +333 -91
  88. data/lib/rdkafka/consumer/allocator.rb +38 -0
  89. data/lib/rdkafka/consumer/headers.rb +39 -25
  90. data/lib/rdkafka/consumer/message.rb +18 -20
  91. data/lib/rdkafka/consumer/partition.rb +15 -4
  92. data/lib/rdkafka/consumer/topic_partition_list.rb +82 -48
  93. data/lib/rdkafka/consumer.rb +765 -192
  94. data/lib/rdkafka/defaults.rb +123 -0
  95. data/lib/rdkafka/error.rb +40 -14
  96. data/lib/rdkafka/helpers/list_offsets.rb +127 -0
  97. data/lib/rdkafka/helpers/metadata.rb +29 -0
  98. data/lib/rdkafka/helpers/oauth.rb +95 -0
  99. data/lib/rdkafka/helpers/time.rb +19 -0
  100. data/lib/rdkafka/metadata.rb +138 -26
  101. data/lib/rdkafka/native_kafka.rb +142 -22
  102. data/lib/rdkafka/producer/delivery_handle.rb +27 -7
  103. data/lib/rdkafka/producer/delivery_report.rb +71 -4
  104. data/lib/rdkafka/producer/partitions_count_cache.rb +212 -0
  105. data/lib/rdkafka/producer.rb +422 -81
  106. data/lib/rdkafka/version.rb +6 -3
  107. data/lib/rdkafka.rb +73 -1
  108. data/package-lock.json +331 -0
  109. data/package.json +9 -0
  110. data/rdkafka.gemspec +78 -25
  111. data/renovate.json +111 -0
  112. metadata +114 -134
  113. data/.gitignore +0 -8
  114. data/.rspec +0 -1
  115. data/.semaphore/semaphore.yml +0 -27
  116. data/.yardopts +0 -2
  117. data/Gemfile +0 -5
  118. data/Guardfile +0 -19
  119. data/ext/README.md +0 -18
  120. data/spec/rdkafka/abstract_handle_spec.rb +0 -115
  121. data/spec/rdkafka/admin/create_topic_handle_spec.rb +0 -54
  122. data/spec/rdkafka/admin/create_topic_report_spec.rb +0 -18
  123. data/spec/rdkafka/admin/delete_topic_handle_spec.rb +0 -54
  124. data/spec/rdkafka/admin/delete_topic_report_spec.rb +0 -18
  125. data/spec/rdkafka/admin_spec.rb +0 -204
  126. data/spec/rdkafka/bindings_spec.rb +0 -136
  127. data/spec/rdkafka/callbacks_spec.rb +0 -22
  128. data/spec/rdkafka/config_spec.rb +0 -197
  129. data/spec/rdkafka/consumer/headers_spec.rb +0 -62
  130. data/spec/rdkafka/consumer/message_spec.rb +0 -141
  131. data/spec/rdkafka/consumer/partition_spec.rb +0 -59
  132. data/spec/rdkafka/consumer/topic_partition_list_spec.rb +0 -225
  133. data/spec/rdkafka/consumer_spec.rb +0 -1106
  134. data/spec/rdkafka/error_spec.rb +0 -91
  135. data/spec/rdkafka/metadata_spec.rb +0 -80
  136. data/spec/rdkafka/native_kafka_spec.rb +0 -124
  137. data/spec/rdkafka/producer/delivery_handle_spec.rb +0 -47
  138. data/spec/rdkafka/producer/delivery_report_spec.rb +0 -23
  139. data/spec/rdkafka/producer_spec.rb +0 -557
  140. data/spec/spec_helper.rb +0 -155
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe Rdkafka::Admin::DeleteTopicHandle do
6
- let(:response) { 0 }
7
-
8
- subject do
9
- Rdkafka::Admin::DeleteTopicHandle.new.tap do |handle|
10
- handle[:pending] = pending_handle
11
- handle[:response] = response
12
- handle[:error_string] = FFI::Pointer::NULL
13
- handle[:result_name] = FFI::MemoryPointer.from_string("my-test-topic")
14
- end
15
- end
16
-
17
- describe "#wait" do
18
- let(:pending_handle) { true }
19
-
20
- it "should wait until the timeout and then raise an error" do
21
- expect {
22
- subject.wait(max_wait_timeout: 0.1)
23
- }.to raise_error Rdkafka::Admin::DeleteTopicHandle::WaitTimeoutError, /delete topic/
24
- end
25
-
26
- context "when not pending anymore and no error" do
27
- let(:pending_handle) { false }
28
-
29
- it "should return a delete topic report" do
30
- report = subject.wait
31
-
32
- expect(report.error_string).to eq(nil)
33
- expect(report.result_name).to eq("my-test-topic")
34
- end
35
-
36
- it "should wait without a timeout" do
37
- report = subject.wait(max_wait_timeout: nil)
38
-
39
- expect(report.error_string).to eq(nil)
40
- expect(report.result_name).to eq("my-test-topic")
41
- end
42
- end
43
- end
44
-
45
- describe "#raise_error" do
46
- let(:pending_handle) { false }
47
-
48
- it "should raise the appropriate error" do
49
- expect {
50
- subject.raise_error
51
- }.to raise_exception(Rdkafka::RdkafkaError, /Success \(no_error\)/)
52
- end
53
- end
54
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe Rdkafka::Admin::DeleteTopicReport do
6
- subject { Rdkafka::Admin::DeleteTopicReport.new(
7
- FFI::MemoryPointer.from_string("error string"),
8
- FFI::MemoryPointer.from_string("result name")
9
- )}
10
-
11
- it "should get the error string" do
12
- expect(subject.error_string).to eq("error string")
13
- end
14
-
15
- it "should get the result name" do
16
- expect(subject.result_name).to eq("result name")
17
- end
18
- end
@@ -1,204 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require "ostruct"
5
-
6
- describe Rdkafka::Admin do
7
- let(:config) { rdkafka_config }
8
- let(:admin) { config.admin }
9
-
10
- after do
11
- # Registry should always end up being empty
12
- expect(Rdkafka::Admin::CreateTopicHandle::REGISTRY).to be_empty
13
- admin.close
14
- end
15
-
16
- let(:topic_name) { "test-topic-#{Random.new.rand(0..1_000_000)}" }
17
- let(:topic_partition_count) { 3 }
18
- let(:topic_replication_factor) { 1 }
19
- let(:topic_config) { {"cleanup.policy" => "compact", "min.cleanable.dirty.ratio" => 0.8} }
20
- let(:invalid_topic_config) { {"cleeeeenup.policee" => "campact"} }
21
-
22
- describe "#create_topic" do
23
- describe "called with invalid input" do
24
- describe "with an invalid topic name" do
25
- # https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/internals/Topic.java#L29
26
- # public static final String LEGAL_CHARS = "[a-zA-Z0-9._-]";
27
- let(:topic_name) { "[!@#]" }
28
-
29
- it "raises an exception" do
30
- create_topic_handle = admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
31
- expect {
32
- create_topic_handle.wait(max_wait_timeout: 15.0)
33
- }.to raise_exception { |ex|
34
- expect(ex).to be_a(Rdkafka::RdkafkaError)
35
- expect(ex.message).to match(/Broker: Invalid topic \(topic_exception\)/)
36
- expect(ex.broker_message).to match(/Topic name.*is illegal, it contains a character other than ASCII alphanumerics/)
37
- }
38
- end
39
- end
40
-
41
- describe "with the name of a topic that already exists" do
42
- let(:topic_name) { "empty_test_topic" } # created in spec_helper.rb
43
-
44
- it "raises an exception" do
45
- create_topic_handle = admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
46
- expect {
47
- create_topic_handle.wait(max_wait_timeout: 15.0)
48
- }.to raise_exception { |ex|
49
- expect(ex).to be_a(Rdkafka::RdkafkaError)
50
- expect(ex.message).to match(/Broker: Topic already exists \(topic_already_exists\)/)
51
- expect(ex.broker_message).to match(/Topic 'empty_test_topic' already exists/)
52
- }
53
- end
54
- end
55
-
56
- describe "with an invalid partition count" do
57
- let(:topic_partition_count) { -999 }
58
-
59
- it "raises an exception" do
60
- expect {
61
- admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
62
- }.to raise_error Rdkafka::Config::ConfigError, /num_partitions out of expected range/
63
- end
64
- end
65
-
66
- describe "with an invalid replication factor" do
67
- let(:topic_replication_factor) { -2 }
68
-
69
- it "raises an exception" do
70
- expect {
71
- admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
72
- }.to raise_error Rdkafka::Config::ConfigError, /replication_factor out of expected range/
73
- end
74
- end
75
-
76
- describe "with an invalid topic configuration" do
77
- it "doesn't create the topic" do
78
- create_topic_handle = admin.create_topic(topic_name, topic_partition_count, topic_replication_factor, invalid_topic_config)
79
- expect {
80
- create_topic_handle.wait(max_wait_timeout: 15.0)
81
- }.to raise_error Rdkafka::RdkafkaError, /Broker: Configuration is invalid \(invalid_config\)/
82
- end
83
- end
84
- end
85
-
86
- context "edge case" do
87
- context "where we are unable to get the background queue" do
88
- before do
89
- allow(Rdkafka::Bindings).to receive(:rd_kafka_queue_get_background).and_return(FFI::Pointer::NULL)
90
- end
91
-
92
- it "raises an exception" do
93
- expect {
94
- admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
95
- }.to raise_error Rdkafka::Config::ConfigError, /rd_kafka_queue_get_background was NULL/
96
- end
97
- end
98
-
99
- context "where rd_kafka_CreateTopics raises an exception" do
100
- before do
101
- allow(Rdkafka::Bindings).to receive(:rd_kafka_CreateTopics).and_raise(RuntimeError.new("oops"))
102
- end
103
-
104
- it "raises an exception" do
105
- expect {
106
- admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
107
- }.to raise_error RuntimeError, /oops/
108
- end
109
- end
110
- end
111
-
112
- it "creates a topic" do
113
- create_topic_handle = admin.create_topic(topic_name, topic_partition_count, topic_replication_factor, topic_config)
114
- create_topic_report = create_topic_handle.wait(max_wait_timeout: 15.0)
115
- expect(create_topic_report.error_string).to be_nil
116
- expect(create_topic_report.result_name).to eq(topic_name)
117
- end
118
- end
119
-
120
- describe "#delete_topic" do
121
- describe "called with invalid input" do
122
- # https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/internals/Topic.java#L29
123
- # public static final String LEGAL_CHARS = "[a-zA-Z0-9._-]";
124
- describe "with an invalid topic name" do
125
- let(:topic_name) { "[!@#]" }
126
-
127
- it "raises an exception" do
128
- delete_topic_handle = admin.delete_topic(topic_name)
129
- expect {
130
- delete_topic_handle.wait(max_wait_timeout: 15.0)
131
- }.to raise_exception { |ex|
132
- expect(ex).to be_a(Rdkafka::RdkafkaError)
133
- expect(ex.message).to match(/Broker: Unknown topic or partition \(unknown_topic_or_part\)/)
134
- expect(ex.broker_message).to match(/Broker: Unknown topic or partition/)
135
- }
136
- end
137
- end
138
-
139
- describe "with the name of a topic that does not exist" do
140
- it "raises an exception" do
141
- delete_topic_handle = admin.delete_topic(topic_name)
142
- expect {
143
- delete_topic_handle.wait(max_wait_timeout: 15.0)
144
- }.to raise_exception { |ex|
145
- expect(ex).to be_a(Rdkafka::RdkafkaError)
146
- expect(ex.message).to match(/Broker: Unknown topic or partition \(unknown_topic_or_part\)/)
147
- expect(ex.broker_message).to match(/Broker: Unknown topic or partition/)
148
- }
149
- end
150
- end
151
- end
152
-
153
- context "edge case" do
154
- context "where we are unable to get the background queue" do
155
- before do
156
- allow(Rdkafka::Bindings).to receive(:rd_kafka_queue_get_background).and_return(FFI::Pointer::NULL)
157
- end
158
-
159
- it "raises an exception" do
160
- expect {
161
- admin.delete_topic(topic_name)
162
- }.to raise_error Rdkafka::Config::ConfigError, /rd_kafka_queue_get_background was NULL/
163
- end
164
- end
165
-
166
- context "where rd_kafka_DeleteTopics raises an exception" do
167
- before do
168
- allow(Rdkafka::Bindings).to receive(:rd_kafka_DeleteTopics).and_raise(RuntimeError.new("oops"))
169
- end
170
-
171
- it "raises an exception" do
172
- expect {
173
- admin.delete_topic(topic_name)
174
- }.to raise_error RuntimeError, /oops/
175
- end
176
- end
177
- end
178
-
179
- it "deletes a topic that was newly created" do
180
- create_topic_handle = admin.create_topic(topic_name, topic_partition_count, topic_replication_factor)
181
- create_topic_report = create_topic_handle.wait(max_wait_timeout: 15.0)
182
- expect(create_topic_report.error_string).to be_nil
183
- expect(create_topic_report.result_name).to eq(topic_name)
184
-
185
- # Retry topic deletion a few times. On CI Kafka seems to not
186
- # always be ready for it immediately
187
- delete_topic_report = nil
188
- 10.times do |i|
189
- begin
190
- delete_topic_handle = admin.delete_topic(topic_name)
191
- delete_topic_report = delete_topic_handle.wait(max_wait_timeout: 15.0)
192
- break
193
- rescue Rdkafka::RdkafkaError => ex
194
- if i > 3
195
- raise ex
196
- end
197
- end
198
- end
199
-
200
- expect(delete_topic_report.error_string).to be_nil
201
- expect(delete_topic_report.result_name).to eq(topic_name)
202
- end
203
- end
204
- end
@@ -1,136 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require 'zlib'
5
-
6
- describe Rdkafka::Bindings do
7
- it "should load librdkafka" do
8
- expect(Rdkafka::Bindings.ffi_libraries.map(&:name).first).to include "librdkafka"
9
- end
10
-
11
- describe ".lib_extension" do
12
- it "should know the lib extension for darwin" do
13
- stub_const('RbConfig::CONFIG', 'host_os' =>'darwin')
14
- expect(Rdkafka::Bindings.lib_extension).to eq "dylib"
15
- end
16
-
17
- it "should know the lib extension for linux" do
18
- stub_const('RbConfig::CONFIG', 'host_os' =>'linux')
19
- expect(Rdkafka::Bindings.lib_extension).to eq "so"
20
- end
21
- end
22
-
23
- it "should successfully call librdkafka" do
24
- expect {
25
- Rdkafka::Bindings.rd_kafka_conf_new
26
- }.not_to raise_error
27
- end
28
-
29
- describe "log callback" do
30
- let(:log_queue) { Rdkafka::Config.log_queue }
31
- before do
32
- allow(log_queue).to receive(:<<)
33
- end
34
-
35
- it "should log fatal messages" do
36
- Rdkafka::Bindings::LogCallback.call(nil, 0, nil, "log line")
37
- expect(log_queue).to have_received(:<<).with([Logger::FATAL, "rdkafka: log line"])
38
- end
39
-
40
- it "should log error messages" do
41
- Rdkafka::Bindings::LogCallback.call(nil, 3, nil, "log line")
42
- expect(log_queue).to have_received(:<<).with([Logger::ERROR, "rdkafka: log line"])
43
- end
44
-
45
- it "should log warning messages" do
46
- Rdkafka::Bindings::LogCallback.call(nil, 4, nil, "log line")
47
- expect(log_queue).to have_received(:<<).with([Logger::WARN, "rdkafka: log line"])
48
- end
49
-
50
- it "should log info messages" do
51
- Rdkafka::Bindings::LogCallback.call(nil, 5, nil, "log line")
52
- expect(log_queue).to have_received(:<<).with([Logger::INFO, "rdkafka: log line"])
53
- end
54
-
55
- it "should log debug messages" do
56
- Rdkafka::Bindings::LogCallback.call(nil, 7, nil, "log line")
57
- expect(log_queue).to have_received(:<<).with([Logger::DEBUG, "rdkafka: log line"])
58
- end
59
-
60
- it "should log unknown messages" do
61
- Rdkafka::Bindings::LogCallback.call(nil, 100, nil, "log line")
62
- expect(log_queue).to have_received(:<<).with([Logger::UNKNOWN, "rdkafka: log line"])
63
- end
64
- end
65
-
66
- describe "partitioner" do
67
- let(:partition_key) { ('a'..'z').to_a.shuffle.take(15).join('') }
68
- let(:partition_count) { rand(50) + 1 }
69
-
70
- it "should return the same partition for a similar string and the same partition count" do
71
- result_1 = Rdkafka::Bindings.partitioner(partition_key, partition_count)
72
- result_2 = Rdkafka::Bindings.partitioner(partition_key, partition_count)
73
- expect(result_1).to eq(result_2)
74
- end
75
-
76
- it "should match the old partitioner" do
77
- result_1 = Rdkafka::Bindings.partitioner(partition_key, partition_count)
78
- result_2 = (Zlib.crc32(partition_key) % partition_count)
79
- expect(result_1).to eq(result_2)
80
- end
81
-
82
- it "should return the partition calculated by the specified partitioner" do
83
- result_1 = Rdkafka::Bindings.partitioner(partition_key, partition_count, "murmur2")
84
- ptr = FFI::MemoryPointer.from_string(partition_key)
85
- result_2 = Rdkafka::Bindings.rd_kafka_msg_partitioner_murmur2(nil, ptr, partition_key.size, partition_count, nil, nil)
86
- expect(result_1).to eq(result_2)
87
- end
88
- end
89
-
90
- describe "stats callback" do
91
- context "without a stats callback" do
92
- it "should do nothing" do
93
- expect {
94
- Rdkafka::Bindings::StatsCallback.call(nil, "{}", 2, nil)
95
- }.not_to raise_error
96
- end
97
- end
98
-
99
- context "with a stats callback" do
100
- before do
101
- Rdkafka::Config.statistics_callback = lambda do |stats|
102
- $received_stats = stats
103
- end
104
- end
105
-
106
- it "should call the stats callback with a stats hash" do
107
- Rdkafka::Bindings::StatsCallback.call(nil, "{\"received\":1}", 13, nil)
108
- expect($received_stats).to eq({'received' => 1})
109
- end
110
- end
111
- end
112
-
113
- describe "error callback" do
114
- context "without an error callback" do
115
- it "should do nothing" do
116
- expect {
117
- Rdkafka::Bindings::ErrorCallback.call(nil, 1, "error", nil)
118
- }.not_to raise_error
119
- end
120
- end
121
-
122
- context "with an error callback" do
123
- before do
124
- Rdkafka::Config.error_callback = lambda do |error|
125
- $received_error = error
126
- end
127
- end
128
-
129
- it "should call the error callback with an Rdkafka::Error" do
130
- Rdkafka::Bindings::ErrorCallback.call(nil, 8, "Broker not available", nil)
131
- expect($received_error.code).to eq(:broker_not_available)
132
- expect($received_error.broker_message).to eq("Broker not available")
133
- end
134
- end
135
- end
136
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe Rdkafka::Callbacks do
6
-
7
- # The code in the call back functions is 100% covered by other specs. Due to
8
- # the large number of collaborators, and the fact that FFI does not play
9
- # nicely with doubles, it was very difficult to construct tests that were
10
- # not over-mocked.
11
-
12
- # For debugging purposes, if you suspect that you are running into trouble in
13
- # one of the callback functions, it may be helpful to surround the inner body
14
- # of the method with something like:
15
- #
16
- # begin
17
- # <method body>
18
- # rescue => ex; puts ex.inspect; puts ex.backtrace; end;
19
- #
20
- # This will output to STDOUT any exceptions that are being raised in the callback.
21
-
22
- end
@@ -1,197 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe Rdkafka::Config do
6
- context "logger" do
7
- it "should have a default logger" do
8
- expect(Rdkafka::Config.logger).to be_a Logger
9
- end
10
-
11
- it "should set the logger" do
12
- logger = Logger.new(STDOUT)
13
- expect(Rdkafka::Config.logger).not_to eq logger
14
- Rdkafka::Config.logger = logger
15
- expect(Rdkafka::Config.logger).to eq logger
16
- end
17
-
18
- it "should not accept a nil logger" do
19
- expect {
20
- Rdkafka::Config.logger = nil
21
- }.to raise_error(Rdkafka::Config::NoLoggerError)
22
- end
23
-
24
- it "supports logging queue" do
25
- log = StringIO.new
26
- Rdkafka::Config.logger = Logger.new(log)
27
-
28
- Rdkafka::Config.log_queue << [Logger::FATAL, "I love testing"]
29
- 20.times do
30
- break if log.string != ""
31
- sleep 0.05
32
- end
33
-
34
- expect(log.string).to include "FATAL -- : I love testing"
35
- end
36
- end
37
-
38
- context "statistics callback" do
39
- context "with a proc/lambda" do
40
- it "should set the callback" do
41
- expect {
42
- Rdkafka::Config.statistics_callback = lambda do |stats|
43
- puts stats
44
- end
45
- }.not_to raise_error
46
- expect(Rdkafka::Config.statistics_callback).to respond_to :call
47
- end
48
- end
49
-
50
- context "with a callable object" do
51
- it "should set the callback" do
52
- callback = Class.new do
53
- def call(stats); end
54
- end
55
- expect {
56
- Rdkafka::Config.statistics_callback = callback.new
57
- }.not_to raise_error
58
- expect(Rdkafka::Config.statistics_callback).to respond_to :call
59
- end
60
- end
61
-
62
- it "should not accept a callback that's not callable" do
63
- expect {
64
- Rdkafka::Config.statistics_callback = 'a string'
65
- }.to raise_error(TypeError)
66
- end
67
- end
68
-
69
- context "error callback" do
70
- context "with a proc/lambda" do
71
- it "should set the callback" do
72
- expect {
73
- Rdkafka::Config.error_callback = lambda do |error|
74
- puts error
75
- end
76
- }.not_to raise_error
77
- expect(Rdkafka::Config.error_callback).to respond_to :call
78
- end
79
- end
80
-
81
- context "with a callable object" do
82
- it "should set the callback" do
83
- callback = Class.new do
84
- def call(stats); end
85
- end
86
- expect {
87
- Rdkafka::Config.error_callback = callback.new
88
- }.not_to raise_error
89
- expect(Rdkafka::Config.error_callback).to respond_to :call
90
- end
91
- end
92
-
93
- it "should not accept a callback that's not callable" do
94
- expect {
95
- Rdkafka::Config.error_callback = 'a string'
96
- }.to raise_error(TypeError)
97
- end
98
- end
99
-
100
- context "configuration" do
101
- it "should store configuration" do
102
- config = Rdkafka::Config.new
103
- config[:"key"] = 'value'
104
- expect(config[:"key"]).to eq 'value'
105
- end
106
-
107
- it "should use default configuration" do
108
- config = Rdkafka::Config.new
109
- expect(config[:"api.version.request"]).to eq true
110
- end
111
-
112
- it "should create a consumer with valid config" do
113
- consumer = rdkafka_consumer_config.consumer
114
- expect(consumer).to be_a Rdkafka::Consumer
115
- consumer.close
116
- end
117
-
118
- it "should raise an error when creating a consumer with invalid config" do
119
- config = Rdkafka::Config.new('invalid.key' => 'value')
120
- expect {
121
- config.consumer
122
- }.to raise_error(Rdkafka::Config::ConfigError, "No such configuration property: \"invalid.key\"")
123
- end
124
-
125
- it "should raise an error when creating a consumer with a nil key in the config" do
126
- config = Rdkafka::Config.new(nil => 'value')
127
- expect {
128
- config.consumer
129
- }.to raise_error(Rdkafka::Config::ConfigError, "No such configuration property: \"\"")
130
- end
131
-
132
- it "should treat a nil value as blank" do
133
- config = Rdkafka::Config.new('security.protocol' => nil)
134
- expect {
135
- config.consumer
136
- config.producer
137
- }.to raise_error(Rdkafka::Config::ConfigError, "Configuration property \"security.protocol\" cannot be set to empty value")
138
- end
139
-
140
- it "should create a producer with valid config" do
141
- producer = rdkafka_consumer_config.producer
142
- expect(producer).to be_a Rdkafka::Producer
143
- producer.close
144
- end
145
-
146
- it "should raise an error when creating a producer with invalid config" do
147
- config = Rdkafka::Config.new('invalid.key' => 'value')
148
- expect {
149
- config.producer
150
- }.to raise_error(Rdkafka::Config::ConfigError, "No such configuration property: \"invalid.key\"")
151
- end
152
-
153
- it "allows string partitioner key" do
154
- expect(Rdkafka::Producer).to receive(:new).with(kind_of(Rdkafka::NativeKafka), "murmur2").and_call_original
155
- config = Rdkafka::Config.new("partitioner" => "murmur2")
156
- config.producer.close
157
- end
158
-
159
- it "allows symbol partitioner key" do
160
- expect(Rdkafka::Producer).to receive(:new).with(kind_of(Rdkafka::NativeKafka), "murmur2").and_call_original
161
- config = Rdkafka::Config.new(:partitioner => "murmur2")
162
- config.producer.close
163
- end
164
-
165
- it "should allow configuring zstd compression" do
166
- config = Rdkafka::Config.new('compression.codec' => 'zstd')
167
- begin
168
- producer = config.producer
169
- expect(producer).to be_a Rdkafka::Producer
170
- producer.close
171
- rescue Rdkafka::Config::ConfigError => ex
172
- pending "Zstd compression not supported on this machine"
173
- raise ex
174
- end
175
- end
176
-
177
- it "should raise an error when client creation fails for a consumer" do
178
- config = Rdkafka::Config.new(
179
- "security.protocol" => "SSL",
180
- "ssl.ca.location" => "/nonsense"
181
- )
182
- expect {
183
- config.consumer
184
- }.to raise_error(Rdkafka::Config::ClientCreationError, /ssl.ca.location failed(.*)/)
185
- end
186
-
187
- it "should raise an error when client creation fails for a producer" do
188
- config = Rdkafka::Config.new(
189
- "security.protocol" => "SSL",
190
- "ssl.ca.location" => "/nonsense"
191
- )
192
- expect {
193
- config.producer
194
- }.to raise_error(Rdkafka::Config::ClientCreationError, /ssl.ca.location failed(.*)/)
195
- end
196
- end
197
- end
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- describe Rdkafka::Consumer::Headers do
6
- let(:headers) do
7
- { # Note String keys!
8
- "version" => "2.1.3",
9
- "type" => "String"
10
- }
11
- end
12
- let(:native_message) { double('native message') }
13
- let(:headers_ptr) { double('headers pointer') }
14
-
15
- describe '.from_native' do
16
- before do
17
- expect(Rdkafka::Bindings).to receive(:rd_kafka_message_headers).with(native_message, anything) do |_, headers_ptrptr|
18
- expect(headers_ptrptr).to receive(:read_pointer).and_return(headers_ptr)
19
- Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
20
- end
21
-
22
- expect(Rdkafka::Bindings).to \
23
- receive(:rd_kafka_header_get_all)
24
- .with(headers_ptr, 0, anything, anything, anything) do |_, _, name_ptrptr, value_ptrptr, size_ptr|
25
- expect(name_ptrptr).to receive(:read_pointer).and_return(double("pointer 0", read_string_to_null: headers.keys[0]))
26
- expect(size_ptr).to receive(:[]).with(:value).and_return(headers.keys[0].size)
27
- expect(value_ptrptr).to receive(:read_pointer).and_return(double("value pointer 0", read_string: headers.values[0]))
28
- Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
29
- end
30
-
31
- expect(Rdkafka::Bindings).to \
32
- receive(:rd_kafka_header_get_all)
33
- .with(headers_ptr, 1, anything, anything, anything) do |_, _, name_ptrptr, value_ptrptr, size_ptr|
34
- expect(name_ptrptr).to receive(:read_pointer).and_return(double("pointer 1", read_string_to_null: headers.keys[1]))
35
- expect(size_ptr).to receive(:[]).with(:value).and_return(headers.keys[1].size)
36
- expect(value_ptrptr).to receive(:read_pointer).and_return(double("value pointer 1", read_string: headers.values[1]))
37
- Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR
38
- end
39
-
40
- expect(Rdkafka::Bindings).to \
41
- receive(:rd_kafka_header_get_all)
42
- .with(headers_ptr, 2, anything, anything, anything)
43
- .and_return(Rdkafka::Bindings::RD_KAFKA_RESP_ERR__NOENT)
44
- end
45
-
46
- subject { described_class.from_native(native_message) }
47
-
48
- it { is_expected.to eq(headers) }
49
- it { is_expected.to be_frozen }
50
-
51
- it 'allows String key' do
52
- expect(subject['version']).to eq("2.1.3")
53
- end
54
-
55
- it 'allows Symbol key, but warns' do
56
- expect(Kernel).to \
57
- receive(:warn).with("rdkafka deprecation warning: header access with Symbol key :version treated as a String. " \
58
- "Please change your code to use String keys to avoid this warning. Symbol keys will break in version 1.")
59
- expect(subject[:version]).to eq("2.1.3")
60
- end
61
- end
62
- end