Skip to content

Commit a50454a

Browse files
committed
Don't fail on broker.to_s
This would fail if there's a problem with connecting to a broker
1 parent 102fdea commit a50454a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/kafka/broker.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def address_match?(host, port)
1919

2020
# @return [String]
2121
def to_s
22-
"#{connection} (node_id=#{@node_id.inspect})"
22+
"#{connection_to_s} (node_id=#{@node_id.inspect})"
2323
end
2424

2525
# @return [nil]
@@ -163,5 +163,9 @@ def send_request(request)
163163
def connection
164164
@connection ||= @connection_builder.build_connection(@host, @port)
165165
end
166+
167+
def connection_to_s
168+
connection.to_s rescue nil
169+
end
166170
end
167171
end

spec/broker_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,14 @@ def close
132132
broker.disconnect
133133
end
134134
end
135+
136+
describe "#to_s" do
137+
it "doesn't fail when building connection fails" do
138+
expect(connection_builder).to receive(:build_connection).and_raise(Kafka::ConnectionError)
139+
140+
expect do
141+
broker.to_s
142+
end.not_to raise_error
143+
end
144+
end
135145
end

0 commit comments

Comments
 (0)