This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
forked from zendesk/ruby-kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
broker_spec.rb
162 lines (131 loc) · 3.79 KB
/
broker_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# frozen_string_literal: true
require "kafka/protocol/message"
describe Kafka::Broker do
let(:logger) { LOGGER }
let(:connection) { FakeConnection.new }
let(:connection_builder) { double(:connection_builder) }
before do
allow(connection_builder).to receive(:build_connection) { connection }
end
let(:broker) {
Kafka::Broker.new(
connection_builder: connection_builder,
host: "x.com",
port: 9092,
node_id: 1,
logger: logger,
)
}
class FakeConnection
def initialize
@mocked_response = nil
end
def mock_response(response)
@mocked_response = response
end
def send_request(request)
@mocked_response
end
def close
end
end
describe "#address_match?" do
it "delegates to @connection" do
host = "test_host"
port = 333
broker = Kafka::Broker.new(
connection_builder: connection_builder,
host: host,
port: port,
node_id: 1,
logger: logger,
)
expect(broker.address_match?(host, port)).to be_truthy
end
end
describe "#metadata" do
it "fetches cluster metadata" do
response = Kafka::Protocol::MetadataResponse.new(brokers: [], controller_id: nil, topics: [])
connection.mock_response(response)
metadata = broker.fetch_metadata(topics: [])
expect(metadata).to eq response
end
end
describe "#produce" do
let(:message) { Kafka::Protocol::Message.new(key: "yo", value: "lo") }
it "waits for a response if acknowledgements are required" do
response = Kafka::Protocol::ProduceResponse.new
connection.mock_response(response)
actual_response = broker.produce(
required_acks: -1, # -1 means all replicas must ack
timeout: 1,
messages_for_topics: {
"yolos" => {
3 => [message],
}
}
)
expect(actual_response).to eq response
end
it "doesn't wait for a response if zero acknowledgements are required" do
response = broker.produce(
required_acks: 0, # 0 means the server doesn't respond or ack at all
timeout: 1,
messages_for_topics: {
"yolos" => {
3 => [message],
}
}
)
expect(response).to be_nil
end
end
describe "#fetch_messages" do
it "fetches messages from the specified topic/partition" do
response = Kafka::Protocol::ProduceResponse.new
connection.mock_response(response)
actual_response = broker.fetch_messages(
max_wait_time: 0,
min_bytes: 0,
max_bytes: 10 * 1024,
topics: {}
)
expect(actual_response.topics).to eq []
end
end
describe "#disconnect" do
it "doesn't close a connection if it's not connected yet " do
expect(connection).not_to receive(:close)
broker.disconnect
end
it "closes a connection if the connection is present" do
expect(connection).to receive(:close)
broker.fetch_messages(
max_wait_time: 0, min_bytes: 0, max_bytes: 10 * 1024, topics: {}
)
broker.disconnect
end
end
describe "#add_offsets_to_txn" do
it 'sends offsets to the transaction' do
allow(connection).to receive(:send_request)
broker.add_offsets_to_txn(
transactional_id: 1, producer_id: 2, producer_epoch: 3, group_id: 4
)
expect(connection).to have_received(:send_request)
broker.disconnect
end
end
describe "#txn_offset_commit" do
before do
allow(connection).to receive(:send_request)
end
it 'commits transaction' do
broker.txn_offset_commit(
transactional_id: 1, producer_id: 2, producer_epoch: 3, group_id: 4, offsets: []
)
expect(connection).to have_received(:send_request)
broker.disconnect
end
end
end