diff --git a/lib/makara/connection_wrapper.rb b/lib/makara/connection_wrapper.rb index 51ccf617..17749ab2 100644 --- a/lib/makara/connection_wrapper.rb +++ b/lib/makara/connection_wrapper.rb @@ -44,7 +44,7 @@ def _makara_blacklisted? end def _makara_in_transaction? - @connection && @connection.open_transactions > 0 + @connection && @connection.respond_to?(:open_transactions) && @connection.open_transactions > 0 end # blacklist this node for @config[:blacklist_duration] seconds diff --git a/spec/connection_wrapper_spec.rb b/spec/connection_wrapper_spec.rb index 0d64efcc..d7091f2e 100644 --- a/spec/connection_wrapper_spec.rb +++ b/spec/connection_wrapper_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' describe Makara::ConnectionWrapper do - let(:proxy){ FakeProxy.new({makara: {blacklist_duration: 5, connections: [{role: 'primary'}, {role: 'replica'}, {role: 'replica'}]}}) } + let(:proxy_config){ {makara: {blacklist_duration: 5, connections: [{role: 'primary'}, {role: 'replica'}, {role: 'replica'}]}} } + let(:proxy){ FakeProxy.new(proxy_config) } let(:connection){ subject._makara_connection } subject{ proxy.primary_pool.connections.first } @@ -39,4 +40,27 @@ end end end + + context '#_makara_in_transaction?' do + context 'open_transactions is 0' do + it 'should return false' do + expect(subject._makara_in_transaction?).to eq(false) + end + end + + context 'connection does not respond to open_transactions' do + let(:non_ar_proxy) do + Class.new(FakeProxy) do + def connection_for(config) + FakeConnectionBase.new(config) + end + end + end + let(:proxy){ non_ar_proxy.new(proxy_config) } + + it 'should return false' do + expect(subject._makara_in_transaction?).to eq(false) + end + end + end end diff --git a/spec/support/mock_objects.rb b/spec/support/mock_objects.rb index f088f5d6..4ab4dffe 100644 --- a/spec/support/mock_objects.rb +++ b/spec/support/mock_objects.rb @@ -1,6 +1,6 @@ require 'active_record/connection_adapters/makara_abstract_adapter' -class FakeConnection < Struct.new(:config) +class FakeConnectionBase < Struct.new(:config) def ping 'ping!' end @@ -17,10 +17,6 @@ def active? true end - def open_transactions - (config || {}).fetch(:open_transactions, 0) - end - def disconnect! true end @@ -30,6 +26,12 @@ def something end end +class FakeConnection < FakeConnectionBase + def open_transactions + (config || {}).fetch(:open_transactions, 0) + end +end + class FakeDatabaseAdapter < Struct.new(:config) def execute(sql, name = nil) []