Skip to content

Commit

Permalink
update specs; support ActiveRecord 4.1 configurations; improved to_s …
Browse files Browse the repository at this point in the history
…on connections
  • Loading branch information
Brian Durand committed Jun 19, 2014
1 parent 0d95a26 commit a89b49f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
8 changes: 8 additions & 0 deletions HISTORY.txt
@@ -1,3 +1,11 @@
1.0.15

Implement less wordy connection to string method so logs don't fill up with long messages on connection errors.

Update specs to remove deprecation warnings

Fix adapter specs to work with ActiveRecord 4.1 configuration changes

1.0.14

Remove custom connection timeout logic; Use the underlying driver's timeouts instead.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.0.14
1.0.15
Expand Up @@ -257,6 +257,10 @@ def use_master_connection
end
end

def to_s
"#<#{self.class.name}:0x#{object_id.to_s(16)} #{all_connections.size} connections>"
end

class DatabaseConnectionError < StandardError
end

Expand Down Expand Up @@ -371,6 +375,7 @@ def do_to_connections
raise e if conn == master_connection
end
end
nil
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/connection_adapters_spec.rb
Expand Up @@ -13,7 +13,11 @@
let(:master_connection){ connection.master_connection }

before(:all) do
ActiveRecord::Base.configurations = {'adapter' => "sqlite3", 'database' => ":memory:"}
if ActiveRecord::VERSION::MAJOR < 3 || (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0)
ActiveRecord::Base.configurations = {'adapter' => "sqlite3", 'database' => ":memory:"}
else
ActiveRecord::Base.configurations = {"test" => {'adapter' => "sqlite3", 'database' => ":memory:"}}
end
ActiveRecord::Base.establish_connection('adapter' => "sqlite3", 'database' => ":memory:")
ActiveRecord::Base.connection
SeamlessDatabasePool::TestModel.db_model(adapter).create_tables
Expand Down
19 changes: 12 additions & 7 deletions spec/seamless_database_pool_adapter_spec.rb
Expand Up @@ -51,7 +51,7 @@ def columns (table_name, name = nil); end
]
}

pool_connection = mock(:connection)
pool_connection = double(:connection)
master_connection = SeamlessDatabasePool::MockConnection.new("master")
read_connection_1 = SeamlessDatabasePool::MockConnection.new("read_1")
read_connection_2 = SeamlessDatabasePool::MockConnection.new("read_2")
Expand All @@ -62,7 +62,7 @@ def columns (table_name, name = nil); end
ActiveRecord::Base.should_receive(:reader_connection).with('adapter' => 'reader', 'host' => 'read_host_1', 'username' => 'user', 'pool_weight' => 1).and_return(read_connection_1)
ActiveRecord::Base.should_receive(:reader_connection).with('adapter' => 'reader', 'host' => 'read_host_2', 'username' => 'user', 'pool_weight' => 2).and_return(read_connection_2)

klass = mock(:class)
klass = double(:class)
ActiveRecord::ConnectionAdapters::SeamlessDatabasePoolAdapter.should_receive(:adapter_class).with(master_connection).and_return(klass)
klass.should_receive(:new).with(nil, logger, master_connection, [read_connection_1, read_connection_2], weights).and_return(pool_connection)

Expand All @@ -88,6 +88,10 @@ def columns (table_name, name = nil); end
connection_class.new(nil, nil, master_connection, [read_connection_1, read_connection_2], weights)
end

it "should be able to be converted to a string" do
pool_connection.to_s.should =~ /\A#<ActiveRecord::ConnectionAdapters::SeamlessDatabasePoolAdapter::Abstract:0x[0-9a-f]+ 3 connections>\z/
end

context "selecting a connection from the pool" do
it "should initialize the connection pool" do
pool_connection.master_connection.should == master_connection
Expand All @@ -104,7 +108,8 @@ def columns (table_name, name = nil); end
end

it "should select a random read connection" do
mock_connection = stub(:connection, :active? => true)
mock_connection = double(:connection)
mock_connection.stub(:active? => true)
pool_connection.should_receive(:available_read_connections).and_return([:fake1, :fake2, mock_connection])
pool_connection.should_receive(:rand).with(3).and_return(2)
pool_connection.random_read_connection.should == mock_connection
Expand All @@ -117,7 +122,7 @@ def columns (table_name, name = nil); end

it "should use the master connection in a block" do
connection_class = ActiveRecord::ConnectionAdapters::SeamlessDatabasePoolAdapter.adapter_class(master_connection)
connection = connection_class.new(nil, mock(:logger), master_connection, [read_connection_1], {read_connection_1 => 1})
connection = connection_class.new(nil, double(:logger), master_connection, [read_connection_1], {read_connection_1 => 1})
connection.random_read_connection.should == read_connection_1
connection.use_master_connection do
connection.random_read_connection.should == master_connection
Expand All @@ -127,7 +132,7 @@ def columns (table_name, name = nil); end

it "should use the master connection inside a transaction" do
connection_class = ActiveRecord::ConnectionAdapters::SeamlessDatabasePoolAdapter.adapter_class(master_connection)
connection = connection_class.new(nil, mock(:logger), master_connection, [read_connection_1], {read_connection_1 => 1})
connection = connection_class.new(nil, double(:logger), master_connection, [read_connection_1], {read_connection_1 => 1})
master_connection.should_receive(:begin_db_transaction)
master_connection.should_receive(:commit_db_transaction)
master_connection.should_receive(:select).with('Transaction SQL', nil)
Expand Down Expand Up @@ -236,12 +241,12 @@ def columns (table_name, name = nil); end
end

it "should try to reconnect dead connections when they become available again" do
master_connection.stub!(:select).and_raise("SQL ERROR")
master_connection.stub(:select).and_raise("SQL ERROR")
master_connection.should_receive(:active?).and_return(false, false, true)
master_connection.should_receive(:reconnect!)
now = Time.now
lambda{pool_connection.select_value("SQL")}.should raise_error("SQL ERROR")
Time.stub!(:now).and_return(now + 31)
Time.stub(:now => now + 31)
lambda{pool_connection.select_value("SQL")}.should raise_error("SQL ERROR")
end

Expand Down
40 changes: 19 additions & 21 deletions spec/seamless_database_pool_spec.rb
Expand Up @@ -11,50 +11,49 @@
end

it "should use the master connection by default" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection = double(:connection, :master_connection => :master_db_connection, :using_master_connection? => false)
SeamlessDatabasePool.read_only_connection_type.should == :master
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end

it "should be able to set using persistent read connections" do
connection = mock(:connection)
connection = double(:connection)
connection.should_receive(:random_read_connection).once.and_return(:read_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :persistent
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
end

it "should be able to set using random read connections" do
connection = mock(:connection)
connection = double(:connection)
connection.should_receive(:random_read_connection).and_return(:read_db_connection_1, :read_db_connection_2)
connection.stub!(:using_master_connection?).and_return(false)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_random_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :random
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_1
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_2
end

it "should use the master connection if the connection is forcing it" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:using_master_connection?).and_return(true)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end

it "should be able to set using the master connection" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection = double(:connection, :master_connection => :master_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_master_connection
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end

it "should be able to use persistent read connections within a block" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).once.and_return(:read_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
SeamlessDatabasePool.use_persistent_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
Expand All @@ -65,9 +64,9 @@
end

it "should be able to use random read connections within a block" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).and_return(:read_db_connection_1, :read_db_connection_2)
connection.stub!(:using_master_connection?).and_return(false)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
SeamlessDatabasePool.use_random_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_1
Expand All @@ -78,9 +77,9 @@
end

it "should be able to use the master connection within a block" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).once.and_return(:read_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.use_master_connection do
Expand All @@ -92,9 +91,9 @@
end

it "should be able to use connection blocks within connection blocks" do
connection = stub(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).any_number_of_times.and_return(:read_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection = double(:connection, :master_connection => :master_db_connection)
connection.stub(:random_read_connection => :read_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.use_master_connection do
Expand All @@ -109,9 +108,8 @@
end

it "should be able to change the persistent connection" do
connection = mock(:connection)
connection.stub!(:random_read_connection).and_return(:read_db_connection)
connection.stub!(:using_master_connection?).and_return(false)
connection = double(:connection)
connection.stub(:random_read_connection => :read_db_connection, :using_master_connection? => false)

SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :persistent
Expand Down

0 comments on commit a89b49f

Please sign in to comment.