From a89b49f7fd77e97092811d1da1208642275a178b Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Thu, 19 Jun 2014 10:32:19 -0700 Subject: [PATCH] update specs; support ActiveRecord 4.1 configurations; improved to_s on connections --- HISTORY.txt | 8 ++++ VERSION | 2 +- .../seamless_database_pool_adapter.rb | 5 +++ spec/connection_adapters_spec.rb | 6 ++- spec/seamless_database_pool_adapter_spec.rb | 19 +++++---- spec/seamless_database_pool_spec.rb | 40 +++++++++---------- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/HISTORY.txt b/HISTORY.txt index 5968df9..bf3df55 100644 --- a/HISTORY.txt +++ b/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. diff --git a/VERSION b/VERSION index 97bceaa..758a46e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.14 \ No newline at end of file +1.0.15 \ No newline at end of file diff --git a/lib/active_record/connection_adapters/seamless_database_pool_adapter.rb b/lib/active_record/connection_adapters/seamless_database_pool_adapter.rb index 14edc66..6953a37 100755 --- a/lib/active_record/connection_adapters/seamless_database_pool_adapter.rb +++ b/lib/active_record/connection_adapters/seamless_database_pool_adapter.rb @@ -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 @@ -371,6 +375,7 @@ def do_to_connections raise e if conn == master_connection end end + nil end end end diff --git a/spec/connection_adapters_spec.rb b/spec/connection_adapters_spec.rb index 7cecbcf..2404458 100644 --- a/spec/connection_adapters_spec.rb +++ b/spec/connection_adapters_spec.rb @@ -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 diff --git a/spec/seamless_database_pool_adapter_spec.rb b/spec/seamless_database_pool_adapter_spec.rb index a22bf7a..81c3011 100755 --- a/spec/seamless_database_pool_adapter_spec.rb +++ b/spec/seamless_database_pool_adapter_spec.rb @@ -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") @@ -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) @@ -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#\z/ + end + context "selecting a connection from the pool" do it "should initialize the connection pool" do pool_connection.master_connection.should == master_connection @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/spec/seamless_database_pool_spec.rb b/spec/seamless_database_pool_spec.rb index e5be649..1b6a32a 100644 --- a/spec/seamless_database_pool_spec.rb +++ b/spec/seamless_database_pool_spec.rb @@ -11,16 +11,15 @@ 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 @@ -28,9 +27,9 @@ 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 @@ -38,23 +37,23 @@ 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 @@ -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 @@ -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 @@ -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 @@ -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