diff --git a/lib/bunny/heartbeat_sender.rb b/lib/bunny/heartbeat_sender.rb index 209f5c598..671fa078f 100644 --- a/lib/bunny/heartbeat_sender.rb +++ b/lib/bunny/heartbeat_sender.rb @@ -63,7 +63,7 @@ def beat if now > (@last_activity_time + @interval) @logger.debug { "Sending a heartbeat, last activity time: #{@last_activity_time}, interval (s): #{@interval}" } - @transport.write_without_timeout(AMQ::Protocol::HeartbeatFrame.encode) + @transport.write_without_timeout(AMQ::Protocol::HeartbeatFrame.encode, true) end end end diff --git a/lib/bunny/transport.rb b/lib/bunny/transport.rb index b187b5737..e05a26957 100644 --- a/lib/bunny/transport.rb +++ b/lib/bunny/transport.rb @@ -157,12 +157,13 @@ def write(data) end # Writes data to the socket without timeout checks - def write_without_timeout(data) + def write_without_timeout(data, raise_exceptions = false) begin @writes_mutex.synchronize { @socket.write(data) } @socket.flush rescue SystemCallError, Bunny::ConnectionError, IOError => e close + raise e if raise_exceptions if @session.automatically_recover? @session.handle_network_failure(e) diff --git a/spec/higher_level_api/integration/toxiproxy_spec.rb b/spec/higher_level_api/integration/toxiproxy_spec.rb index 5f464f3e2..303455f81 100644 --- a/spec/higher_level_api/integration/toxiproxy_spec.rb +++ b/spec/higher_level_api/integration/toxiproxy_spec.rb @@ -5,18 +5,19 @@ describe Bunny::Channel, "#basic_publish" do include RabbitMQ::Toxiproxy - before(:all) do - setup_toxiproxy - @connection = Bunny.new(:user => "bunny_gem", :password => "bunny_password", :vhost => "bunny_testbed", - host: "localhost:11111", heartbeat_timeout: 1) - @connection.start - end - after :all do + after :each do @connection.close if @connection.open? end - context "when the the connection detects missed heartbeats" do + context "when the the connection detects missed heartbeat with automatic recovery" do + before(:each) do + setup_toxiproxy + @connection = Bunny.new(:user => "bunny_gem", :password => "bunny_password", :vhost => "bunny_testbed", + host: "localhost:11111", heartbeat_timeout: 1, automatically_recover: true) + @connection.start + end + let(:queue_name) { "bunny.basic.publish.queue#{rand}" } it "raises a ConnectionClosedError" do @@ -30,6 +31,21 @@ cleanup_toxiproxy end end + + context "when the the connection detects missed heartbeats without automatic recovery" do + before(:each) do + setup_toxiproxy + @connection = Bunny.new(:user => "bunny_gem", :password => "bunny_password", :vhost => "bunny_testbed", + host: "localhost:11111", heartbeat_timeout: 1, automatically_recover: false, threaded: false) + @connection.start + end + + it "does not raise an exception on session thread" do + rabbitmq_toxiproxy.down do + sleep 5 + end + end + end end end else