From ad67595c01e6ac07197051def93e16c6dcb830dc Mon Sep 17 00:00:00 2001 From: Alessandro Verlato Date: Thu, 2 Mar 2017 17:38:14 +0100 Subject: [PATCH 1/2] Fix #465 --- lib/bunny/exceptions.rb | 6 +++++ lib/bunny/session.rb | 11 +++++++- spec/issues/issue465_spec.rb | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 spec/issues/issue465_spec.rb diff --git a/lib/bunny/exceptions.rb b/lib/bunny/exceptions.rb index 0a202f91a..c26543524 100644 --- a/lib/bunny/exceptions.rb +++ b/lib/bunny/exceptions.rb @@ -97,6 +97,12 @@ def initialize(frame) end end + class ConnectionManuallyClosed < Exception + def initialize + super('Connection has been manually closed. Call #start to reopen it') + end + end + class ShutdownSignal < Exception end diff --git a/lib/bunny/session.rb b/lib/bunny/session.rb index e895fe455..b80c0586e 100644 --- a/lib/bunny/session.rb +++ b/lib/bunny/session.rb @@ -325,6 +325,7 @@ def start @status_mutex.synchronize { @status = :not_connected } raise TCPConnectionFailedForAllHosts end + @status_mutex.synchronize { @manually_closed = false } self end @@ -343,6 +344,7 @@ def transport_write_timeout # @return [Bunny::Channel] Newly opened channel def create_channel(n = nil, consumer_pool_size = 1, consumer_pool_abort_on_exception = false, consumer_pool_shutdown_timeout = 60) raise ArgumentError, "channel number 0 is reserved in the protocol and cannot be used" if 0 == n + raise ConnectionManuallyClosed if manually_closed? @channel_mutex.synchronize do if n && (ch = @channels[n]) @@ -369,7 +371,10 @@ def close clean_up_on_shutdown end - @status_mutex.synchronize { @status = :closed } + @status_mutex.synchronize do + @status = :closed + @manually_closed = true + end end alias stop close @@ -404,6 +409,10 @@ def closed? @status_mutex.synchronize { @status == :closed } end + def manually_closed? + @status_mutex.synchronize { @manually_closed == true } + end + # @return [Boolean] true if this AMQP 0.9.1 connection is open def open? @status_mutex.synchronize do diff --git a/spec/issues/issue465_spec.rb b/spec/issues/issue465_spec.rb new file mode 100644 index 000000000..78a170410 --- /dev/null +++ b/spec/issues/issue465_spec.rb @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +require 'spec_helper' + +describe Bunny::Session do + let(:connection) do + c = Bunny.new( + user: 'bunny_gem', password: 'bunny_password', + vhost: 'bunny_testbed', + port: ENV.fetch('RABBITMQ_PORT', 5672) + ) + c.start + c + end + + context 'after the connection has been manually closed' do + before :each do + connection.close + end + + after :each do + connection.close if connection.open? + end + + describe '#create_channel' do + it 'should raise an exception' do + expect { + connection.create_channel + }.to raise_error(Bunny::ConnectionManuallyClosed) + end + end + + describe '#start' do + it 'should be possible to reopen it' do + connection.start + expect(connection.status).to eq :open + end + end + + context 'and reopened' do + before :each do + connection.start + end + + describe '#create_channel' do + it 'should create a new channel' do + expect(connection.create_channel).to be_kind_of(Bunny::Channel) + end + end + end + end +end From 503367b9e8517df73215250919252e19d6422217 Mon Sep 17 00:00:00 2001 From: Alessandro Verlato Date: Thu, 2 Mar 2017 19:47:59 +0100 Subject: [PATCH 2/2] Close pull request review #24791986 by removing specs supporting connection reopen after it has been manually closed Change ConnectionManuallyClosed exception name to ConnectionAlreadyClosed Minor improvements --- lib/bunny/exceptions.rb | 4 ++-- lib/bunny/session.rb | 3 ++- spec/issues/issue465_spec.rb | 21 +-------------------- 3 files changed, 5 insertions(+), 23 deletions(-) diff --git a/lib/bunny/exceptions.rb b/lib/bunny/exceptions.rb index c26543524..c00efc194 100644 --- a/lib/bunny/exceptions.rb +++ b/lib/bunny/exceptions.rb @@ -97,9 +97,9 @@ def initialize(frame) end end - class ConnectionManuallyClosed < Exception + class ConnectionAlreadyClosed < Exception def initialize - super('Connection has been manually closed. Call #start to reopen it') + super('Connection has been already closed') end end diff --git a/lib/bunny/session.rb b/lib/bunny/session.rb index b80c0586e..f6e88cd10 100644 --- a/lib/bunny/session.rb +++ b/lib/bunny/session.rb @@ -344,7 +344,7 @@ def transport_write_timeout # @return [Bunny::Channel] Newly opened channel def create_channel(n = nil, consumer_pool_size = 1, consumer_pool_abort_on_exception = false, consumer_pool_shutdown_timeout = 60) raise ArgumentError, "channel number 0 is reserved in the protocol and cannot be used" if 0 == n - raise ConnectionManuallyClosed if manually_closed? + raise ConnectionAlreadyClosed if manually_closed? @channel_mutex.synchronize do if n && (ch = @channels[n]) @@ -409,6 +409,7 @@ def closed? @status_mutex.synchronize { @status == :closed } end + # @return [Boolean] true if this AMQP 0.9.1 connection has been programmatically closed def manually_closed? @status_mutex.synchronize { @manually_closed == true } end diff --git a/spec/issues/issue465_spec.rb b/spec/issues/issue465_spec.rb index 78a170410..efbb83a72 100644 --- a/spec/issues/issue465_spec.rb +++ b/spec/issues/issue465_spec.rb @@ -25,26 +25,7 @@ it 'should raise an exception' do expect { connection.create_channel - }.to raise_error(Bunny::ConnectionManuallyClosed) - end - end - - describe '#start' do - it 'should be possible to reopen it' do - connection.start - expect(connection.status).to eq :open - end - end - - context 'and reopened' do - before :each do - connection.start - end - - describe '#create_channel' do - it 'should create a new channel' do - expect(connection.create_channel).to be_kind_of(Bunny::Channel) - end + }.to raise_error(Bunny::ConnectionAlreadyClosed) end end end