Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check decorated connection responds to open_transactions before invoking #341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/makara/connection_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion spec/connection_wrapper_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -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
12 changes: 7 additions & 5 deletions spec/support/mock_objects.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,10 +17,6 @@ def active?
true
end

def open_transactions
(config || {}).fetch(:open_transactions, 0)
end

def disconnect!
true
end
Expand All @@ -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)
[]
Expand Down