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,557 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require "zlib"
5
-
6
- describe Rdkafka::Producer do
7
- let(:producer) { rdkafka_producer_config.producer }
8
- let(:consumer) { rdkafka_consumer_config.consumer }
9
-
10
- after do
11
- # Registry should always end up being empty
12
- registry = Rdkafka::Producer::DeliveryHandle::REGISTRY
13
- expect(registry).to be_empty, registry.inspect
14
- producer.close
15
- consumer.close
16
- end
17
-
18
- context "delivery callback" do
19
- context "with a proc/lambda" do
20
- it "should set the callback" do
21
- expect {
22
- producer.delivery_callback = lambda do |delivery_handle|
23
- puts delivery_handle
24
- end
25
- }.not_to raise_error
26
- expect(producer.delivery_callback).to respond_to :call
27
- end
28
-
29
- it "should call the callback when a message is delivered" do
30
- @callback_called = false
31
-
32
- producer.delivery_callback = lambda do |report|
33
- expect(report).not_to be_nil
34
- expect(report.partition).to eq 1
35
- expect(report.offset).to be >= 0
36
- expect(report.topic_name).to eq "produce_test_topic"
37
- @callback_called = true
38
- end
39
-
40
- # Produce a message
41
- handle = producer.produce(
42
- topic: "produce_test_topic",
43
- payload: "payload",
44
- key: "key"
45
- )
46
-
47
- # Wait for it to be delivered
48
- handle.wait(max_wait_timeout: 15)
49
-
50
- # Join the producer thread.
51
- producer.close
52
-
53
- # Callback should have been called
54
- expect(@callback_called).to be true
55
- end
56
-
57
- it "should provide handle" do
58
- @callback_handle = nil
59
-
60
- producer.delivery_callback = lambda { |_, handle| @callback_handle = handle }
61
-
62
- # Produce a message
63
- handle = producer.produce(
64
- topic: "produce_test_topic",
65
- payload: "payload",
66
- key: "key"
67
- )
68
-
69
- # Wait for it to be delivered
70
- handle.wait(max_wait_timeout: 15)
71
-
72
- # Join the producer thread.
73
- producer.close
74
-
75
- expect(handle).to be @callback_handle
76
- end
77
- end
78
-
79
- context "with a callable object" do
80
- it "should set the callback" do
81
- callback = Class.new do
82
- def call(stats); end
83
- end
84
- expect {
85
- producer.delivery_callback = callback.new
86
- }.not_to raise_error
87
- expect(producer.delivery_callback).to respond_to :call
88
- end
89
-
90
- it "should call the callback when a message is delivered" do
91
- called_report = []
92
- callback = Class.new do
93
- def initialize(called_report)
94
- @called_report = called_report
95
- end
96
-
97
- def call(report)
98
- @called_report << report
99
- end
100
- end
101
- producer.delivery_callback = callback.new(called_report)
102
-
103
- # Produce a message
104
- handle = producer.produce(
105
- topic: "produce_test_topic",
106
- payload: "payload",
107
- key: "key"
108
- )
109
-
110
- # Wait for it to be delivered
111
- handle.wait(max_wait_timeout: 15)
112
-
113
- # Join the producer thread.
114
- producer.close
115
-
116
- # Callback should have been called
117
- expect(called_report.first).not_to be_nil
118
- expect(called_report.first.partition).to eq 1
119
- expect(called_report.first.offset).to be >= 0
120
- expect(called_report.first.topic_name).to eq "produce_test_topic"
121
- end
122
-
123
- it "should provide handle" do
124
- callback_handles = []
125
- callback = Class.new do
126
- def initialize(callback_handles)
127
- @callback_handles = callback_handles
128
- end
129
-
130
- def call(_, handle)
131
- @callback_handles << handle
132
- end
133
- end
134
- producer.delivery_callback = callback.new(callback_handles)
135
-
136
- # Produce a message
137
- handle = producer.produce(
138
- topic: "produce_test_topic",
139
- payload: "payload",
140
- key: "key"
141
- )
142
-
143
- # Wait for it to be delivered
144
- handle.wait(max_wait_timeout: 15)
145
-
146
- # Join the producer thread.
147
- producer.close
148
-
149
- # Callback should have been called
150
- expect(handle).to be callback_handles.first
151
- end
152
- end
153
-
154
- it "should not accept a callback that's not callable" do
155
- expect {
156
- producer.delivery_callback = 'a string'
157
- }.to raise_error(TypeError)
158
- end
159
- end
160
-
161
- it "should require a topic" do
162
- expect {
163
- producer.produce(
164
- payload: "payload",
165
- key: "key"
166
- )
167
- }.to raise_error ArgumentError, /missing keyword: [\:]?topic/
168
- end
169
-
170
- it "should produce a message" do
171
- # Produce a message
172
- handle = producer.produce(
173
- topic: "produce_test_topic",
174
- payload: "payload",
175
- key: "key"
176
- )
177
-
178
- # Should be pending at first
179
- expect(handle.pending?).to be true
180
-
181
- # Check delivery handle and report
182
- report = handle.wait(max_wait_timeout: 5)
183
- expect(handle.pending?).to be false
184
- expect(report).not_to be_nil
185
- expect(report.partition).to eq 1
186
- expect(report.offset).to be >= 0
187
-
188
- # Flush and close producer
189
- producer.flush
190
- producer.close
191
-
192
- # Consume message and verify its content
193
- message = wait_for_message(
194
- topic: "produce_test_topic",
195
- delivery_report: report,
196
- consumer: consumer
197
- )
198
- expect(message.partition).to eq 1
199
- expect(message.payload).to eq "payload"
200
- expect(message.key).to eq "key"
201
- # Since api.version.request is on by default we will get
202
- # the message creation timestamp if it's not set.
203
- expect(message.timestamp).to be_within(10).of(Time.now)
204
- end
205
-
206
- it "should produce a message with a specified partition" do
207
- # Produce a message
208
- handle = producer.produce(
209
- topic: "produce_test_topic",
210
- payload: "payload partition",
211
- key: "key partition",
212
- partition: 1
213
- )
214
- report = handle.wait(max_wait_timeout: 5)
215
-
216
- # Consume message and verify its content
217
- message = wait_for_message(
218
- topic: "produce_test_topic",
219
- delivery_report: report,
220
- consumer: consumer
221
- )
222
- expect(message.partition).to eq 1
223
- expect(message.key).to eq "key partition"
224
- end
225
-
226
- it "should produce a message to the same partition with a similar partition key" do
227
- # Avoid partitioner collisions.
228
- while true
229
- key = ('a'..'z').to_a.shuffle.take(10).join('')
230
- partition_key = ('a'..'z').to_a.shuffle.take(10).join('')
231
- partition_count = producer.partition_count('partitioner_test_topic')
232
- break if (Zlib.crc32(key) % partition_count) != (Zlib.crc32(partition_key) % partition_count)
233
- end
234
-
235
- # Produce a message with key, partition_key and key + partition_key
236
- messages = [{key: key}, {partition_key: partition_key}, {key: key, partition_key: partition_key}]
237
-
238
- messages = messages.map do |m|
239
- handle = producer.produce(
240
- topic: "partitioner_test_topic",
241
- payload: "payload partition",
242
- key: m[:key],
243
- partition_key: m[:partition_key]
244
- )
245
- report = handle.wait(max_wait_timeout: 5)
246
-
247
- wait_for_message(
248
- topic: "partitioner_test_topic",
249
- delivery_report: report,
250
- )
251
- end
252
-
253
- expect(messages[0].partition).not_to eq(messages[2].partition)
254
- expect(messages[1].partition).to eq(messages[2].partition)
255
- expect(messages[0].key).to eq key
256
- expect(messages[1].key).to be_nil
257
- expect(messages[2].key).to eq key
258
- end
259
-
260
- it "should produce a message with empty string without crashing" do
261
- messages = [{key: 'a', partition_key: ''}]
262
-
263
- messages = messages.map do |m|
264
- handle = producer.produce(
265
- topic: "partitioner_test_topic",
266
- payload: "payload partition",
267
- key: m[:key],
268
- partition_key: m[:partition_key]
269
- )
270
- report = handle.wait(max_wait_timeout: 5)
271
-
272
- wait_for_message(
273
- topic: "partitioner_test_topic",
274
- delivery_report: report,
275
- )
276
- end
277
-
278
- expect(messages[0].partition).to eq 0
279
- expect(messages[0].key).to eq 'a'
280
- end
281
-
282
- it "should produce a message with utf-8 encoding" do
283
- handle = producer.produce(
284
- topic: "produce_test_topic",
285
- payload: "Τη γλώσσα μου έδωσαν ελληνική",
286
- key: "key utf8"
287
- )
288
- report = handle.wait(max_wait_timeout: 5)
289
-
290
- # Consume message and verify its content
291
- message = wait_for_message(
292
- topic: "produce_test_topic",
293
- delivery_report: report,
294
- consumer: consumer
295
- )
296
-
297
- expect(message.partition).to eq 1
298
- expect(message.payload.force_encoding("utf-8")).to eq "Τη γλώσσα μου έδωσαν ελληνική"
299
- expect(message.key).to eq "key utf8"
300
- end
301
-
302
- context "timestamp" do
303
- it "should raise a type error if not nil, integer or time" do
304
- expect {
305
- producer.produce(
306
- topic: "produce_test_topic",
307
- payload: "payload timestamp",
308
- key: "key timestamp",
309
- timestamp: "10101010"
310
- )
311
- }.to raise_error TypeError
312
- end
313
-
314
- it "should produce a message with an integer timestamp" do
315
- handle = producer.produce(
316
- topic: "produce_test_topic",
317
- payload: "payload timestamp",
318
- key: "key timestamp",
319
- timestamp: 1505069646252
320
- )
321
- report = handle.wait(max_wait_timeout: 5)
322
-
323
- # Consume message and verify its content
324
- message = wait_for_message(
325
- topic: "produce_test_topic",
326
- delivery_report: report,
327
- consumer: consumer
328
- )
329
-
330
- expect(message.partition).to eq 2
331
- expect(message.key).to eq "key timestamp"
332
- expect(message.timestamp).to eq Time.at(1505069646, 252_000)
333
- end
334
-
335
- it "should produce a message with a time timestamp" do
336
- handle = producer.produce(
337
- topic: "produce_test_topic",
338
- payload: "payload timestamp",
339
- key: "key timestamp",
340
- timestamp: Time.at(1505069646, 353_000)
341
- )
342
- report = handle.wait(max_wait_timeout: 5)
343
-
344
- # Consume message and verify its content
345
- message = wait_for_message(
346
- topic: "produce_test_topic",
347
- delivery_report: report,
348
- consumer: consumer
349
- )
350
-
351
- expect(message.partition).to eq 2
352
- expect(message.key).to eq "key timestamp"
353
- expect(message.timestamp).to eq Time.at(1505069646, 353_000)
354
- end
355
- end
356
-
357
- it "should produce a message with nil key" do
358
- handle = producer.produce(
359
- topic: "produce_test_topic",
360
- payload: "payload no key"
361
- )
362
- report = handle.wait(max_wait_timeout: 5)
363
-
364
- # Consume message and verify its content
365
- message = wait_for_message(
366
- topic: "produce_test_topic",
367
- delivery_report: report,
368
- consumer: consumer
369
- )
370
-
371
- expect(message.key).to be_nil
372
- expect(message.payload).to eq "payload no key"
373
- end
374
-
375
- it "should produce a message with nil payload" do
376
- handle = producer.produce(
377
- topic: "produce_test_topic",
378
- key: "key no payload"
379
- )
380
- report = handle.wait(max_wait_timeout: 5)
381
-
382
- # Consume message and verify its content
383
- message = wait_for_message(
384
- topic: "produce_test_topic",
385
- delivery_report: report,
386
- consumer: consumer
387
- )
388
-
389
- expect(message.key).to eq "key no payload"
390
- expect(message.payload).to be_nil
391
- end
392
-
393
- it "should produce a message with headers" do
394
- handle = producer.produce(
395
- topic: "produce_test_topic",
396
- payload: "payload headers",
397
- key: "key headers",
398
- headers: { foo: :bar, baz: :foobar }
399
- )
400
- report = handle.wait(max_wait_timeout: 5)
401
-
402
- # Consume message and verify its content
403
- message = wait_for_message(
404
- topic: "produce_test_topic",
405
- delivery_report: report,
406
- consumer: consumer
407
- )
408
-
409
- expect(message.payload).to eq "payload headers"
410
- expect(message.key).to eq "key headers"
411
- expect(message.headers["foo"]).to eq "bar"
412
- expect(message.headers["baz"]).to eq "foobar"
413
- expect(message.headers["foobar"]).to be_nil
414
- end
415
-
416
- it "should produce a message with empty headers" do
417
- handle = producer.produce(
418
- topic: "produce_test_topic",
419
- payload: "payload headers",
420
- key: "key headers",
421
- headers: {}
422
- )
423
- report = handle.wait(max_wait_timeout: 5)
424
-
425
- # Consume message and verify its content
426
- message = wait_for_message(
427
- topic: "produce_test_topic",
428
- delivery_report: report,
429
- consumer: consumer
430
- )
431
-
432
- expect(message.payload).to eq "payload headers"
433
- expect(message.key).to eq "key headers"
434
- expect(message.headers).to be_empty
435
- end
436
-
437
- it "should produce message that aren't waited for and not crash" do
438
- 5.times do
439
- 200.times do
440
- producer.produce(
441
- topic: "produce_test_topic",
442
- payload: "payload not waiting",
443
- key: "key not waiting"
444
- )
445
- end
446
-
447
- # Allow some time for a GC run
448
- sleep 1
449
- end
450
-
451
- # Wait for the delivery notifications
452
- 10.times do
453
- break if Rdkafka::Producer::DeliveryHandle::REGISTRY.empty?
454
- sleep 1
455
- end
456
- end
457
-
458
- it "should produce a message in a forked process", skip: defined?(JRUBY_VERSION) && "Kernel#fork is not available" do
459
- # Fork, produce a message, send the report over a pipe and
460
- # wait for and check the message in the main process.
461
- reader, writer = IO.pipe
462
-
463
- pid = fork do
464
- reader.close
465
-
466
- # Avoid sharing the client between processes.
467
- producer = rdkafka_producer_config.producer
468
-
469
- handle = producer.produce(
470
- topic: "produce_test_topic",
471
- payload: "payload-forked",
472
- key: "key-forked"
473
- )
474
-
475
- report = handle.wait(max_wait_timeout: 5)
476
-
477
- report_json = JSON.generate(
478
- "partition" => report.partition,
479
- "offset" => report.offset,
480
- "topic_name" => report.topic_name
481
- )
482
-
483
- writer.write(report_json)
484
- writer.close
485
- producer.flush
486
- producer.close
487
- end
488
- Process.wait(pid)
489
-
490
- writer.close
491
- report_hash = JSON.parse(reader.read)
492
- report = Rdkafka::Producer::DeliveryReport.new(
493
- report_hash["partition"],
494
- report_hash["offset"],
495
- report_hash["topic_name"]
496
- )
497
-
498
- reader.close
499
-
500
- # Consume message and verify its content
501
- message = wait_for_message(
502
- topic: "produce_test_topic",
503
- delivery_report: report,
504
- consumer: consumer
505
- )
506
- expect(message.partition).to eq 0
507
- expect(message.payload).to eq "payload-forked"
508
- expect(message.key).to eq "key-forked"
509
- end
510
-
511
- it "should raise an error when producing fails" do
512
- expect(Rdkafka::Bindings).to receive(:rd_kafka_producev).and_return(20)
513
-
514
- expect {
515
- producer.produce(
516
- topic: "produce_test_topic",
517
- key: "key error"
518
- )
519
- }.to raise_error Rdkafka::RdkafkaError
520
- end
521
-
522
- it "should raise a timeout error when waiting too long" do
523
- handle = producer.produce(
524
- topic: "produce_test_topic",
525
- payload: "payload timeout",
526
- key: "key timeout"
527
- )
528
- expect {
529
- handle.wait(max_wait_timeout: 0)
530
- }.to raise_error Rdkafka::Producer::DeliveryHandle::WaitTimeoutError
531
-
532
- # Waiting a second time should work
533
- handle.wait(max_wait_timeout: 5)
534
- end
535
-
536
- context "methods that should not be called after a producer has been closed" do
537
- before do
538
- producer.close
539
- end
540
-
541
- # Affected methods and a non-invalid set of parameters for the method
542
- {
543
- :produce => { topic: nil },
544
- :partition_count => nil,
545
- }.each do |method, args|
546
- it "raises an exception if #{method} is called" do
547
- expect {
548
- if args.is_a?(Hash)
549
- producer.public_send(method, **args)
550
- else
551
- producer.public_send(method, args)
552
- end
553
- }.to raise_exception(Rdkafka::ClosedProducerError, /#{method.to_s}/)
554
- end
555
- end
556
- end
557
- end
data/spec/spec_helper.rb DELETED
@@ -1,155 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- unless ENV["CI"] == "true"
4
- require "simplecov"
5
- SimpleCov.start do
6
- add_filter "/spec/"
7
- end
8
- end
9
-
10
- require "pry"
11
- require "rspec"
12
- require "rdkafka"
13
- require "timeout"
14
-
15
- def rdkafka_base_config
16
- {
17
- :"api.version.request" => false,
18
- :"broker.version.fallback" => "1.0",
19
- :"bootstrap.servers" => "localhost:9092",
20
- }
21
- end
22
-
23
- def rdkafka_config(config_overrides={})
24
- # Generate the base config
25
- config = rdkafka_base_config
26
- # Merge overrides
27
- config.merge!(config_overrides)
28
- # Return it
29
- Rdkafka::Config.new(config)
30
- end
31
-
32
- def rdkafka_consumer_config(config_overrides={})
33
- # Generate the base config
34
- config = rdkafka_base_config
35
- # Add consumer specific fields to it
36
- config[:"auto.offset.reset"] = "earliest"
37
- config[:"enable.partition.eof"] = false
38
- config[:"group.id"] = "ruby-test-#{Random.new.rand(0..1_000_000)}"
39
- # Enable debug mode if required
40
- if ENV["DEBUG_CONSUMER"]
41
- config[:debug] = "cgrp,topic,fetch"
42
- end
43
- # Merge overrides
44
- config.merge!(config_overrides)
45
- # Return it
46
- Rdkafka::Config.new(config)
47
- end
48
-
49
- def rdkafka_producer_config(config_overrides={})
50
- # Generate the base config
51
- config = rdkafka_base_config
52
- # Enable debug mode if required
53
- if ENV["DEBUG_PRODUCER"]
54
- config[:debug] = "broker,topic,msg"
55
- end
56
- # Merge overrides
57
- config.merge!(config_overrides)
58
- # Return it
59
- Rdkafka::Config.new(config)
60
- end
61
-
62
- def new_native_client
63
- config = rdkafka_consumer_config
64
- config.send(:native_kafka, config.send(:native_config), :rd_kafka_producer)
65
- end
66
-
67
- def new_native_topic(topic_name="topic_name", native_client: )
68
- Rdkafka::Bindings.rd_kafka_topic_new(
69
- native_client,
70
- topic_name,
71
- nil
72
- )
73
- end
74
-
75
- def wait_for_message(topic:, delivery_report:, timeout_in_seconds: 30, consumer: nil)
76
- new_consumer = consumer.nil?
77
- consumer ||= rdkafka_consumer_config.consumer
78
- consumer.subscribe(topic)
79
- timeout = Time.now.to_i + timeout_in_seconds
80
- loop do
81
- if timeout <= Time.now.to_i
82
- raise "Timeout of #{timeout_in_seconds} seconds reached in wait_for_message"
83
- end
84
- message = consumer.poll(100)
85
- if message &&
86
- message.partition == delivery_report.partition &&
87
- message.offset == delivery_report.offset
88
- return message
89
- end
90
- end
91
- ensure
92
- consumer.close if new_consumer
93
- end
94
-
95
- def wait_for_assignment(consumer)
96
- 10.times do
97
- break if !consumer.assignment.empty?
98
- sleep 1
99
- end
100
- end
101
-
102
- def wait_for_unassignment(consumer)
103
- 10.times do
104
- break if consumer.assignment.empty?
105
- sleep 1
106
- end
107
- end
108
-
109
- def notify_listener(listener, &block)
110
- # 1. subscribe and poll
111
- consumer.subscribe("consume_test_topic")
112
- wait_for_assignment(consumer)
113
- consumer.poll(100)
114
-
115
- block.call if block
116
-
117
- # 2. unsubscribe
118
- consumer.unsubscribe
119
- wait_for_unassignment(consumer)
120
- consumer.close
121
- end
122
-
123
- RSpec.configure do |config|
124
- config.filter_run focus: true
125
- config.run_all_when_everything_filtered = true
126
-
127
- config.before(:suite) do
128
- admin = rdkafka_config.admin
129
- {
130
- consume_test_topic: 3,
131
- empty_test_topic: 3,
132
- load_test_topic: 3,
133
- produce_test_topic: 3,
134
- rake_test_topic: 3,
135
- watermarks_test_topic: 3,
136
- partitioner_test_topic: 25,
137
- }.each do |topic, partitions|
138
- create_topic_handle = admin.create_topic(topic.to_s, partitions, 1)
139
- begin
140
- create_topic_handle.wait(max_wait_timeout: 15)
141
- rescue Rdkafka::RdkafkaError => ex
142
- raise unless ex.message.match?(/topic_already_exists/)
143
- end
144
- end
145
- admin.close
146
- end
147
-
148
- config.around(:each) do |example|
149
- # Timeout specs after a minute. If they take longer
150
- # they are probably stuck
151
- Timeout::timeout(60) do
152
- example.run
153
- end
154
- end
155
- end