From b83fb847337b690ebbc96c55414157f71bbf8aa2 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 4 May 2016 00:46:38 -0500 Subject: Refactor connection handler ConnectionHandler will not have any knowlodge of AR models now, it will only know about the specs. Like that we can decouple the two, and allow the same model to use more than one connection. Historically, folks used to create abstract AR classes on the fly in order to have multiple connections for the same model, and override the connection methods. With this, now we can override the `specificiation_id` method in the model, to return a key, that will be used to find the connection_pool from the handler. --- .../connection_adapters/connection_handler_test.rb | 44 +++++++++++----------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'activerecord/test/cases/connection_adapters/connection_handler_test.rb') diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index 9b1865e8bb..c3cdb29380 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -4,49 +4,51 @@ module ActiveRecord module ConnectionAdapters class ConnectionHandlerTest < ActiveRecord::TestCase def setup - @klass = Class.new(Base) { def self.name; 'klass'; end } - @subklass = Class.new(@klass) { def self.name; 'subklass'; end } - @handler = ConnectionHandler.new - @pool = @handler.establish_connection(@klass, Base.connection_pool.spec) + resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new Base.configurations + spec = resolver.spec(:arunit) + + @spec_id = "primary" + @pool = @handler.establish_connection(spec) end def test_retrieve_connection - assert @handler.retrieve_connection(@klass) + assert @handler.retrieve_connection(@spec_id) end def test_active_connections? assert !@handler.active_connections? - assert @handler.retrieve_connection(@klass) + assert @handler.retrieve_connection(@spec_id) assert @handler.active_connections? @handler.clear_active_connections! assert !@handler.active_connections? end - def test_retrieve_connection_pool_with_ar_base - assert_nil @handler.retrieve_connection_pool(ActiveRecord::Base) - end +# def test_retrieve_connection_pool_with_ar_base +# assert_nil @handler.retrieve_connection_pool(ActiveRecord::Base) +# end def test_retrieve_connection_pool - assert_not_nil @handler.retrieve_connection_pool(@klass) + assert_not_nil @handler.retrieve_connection_pool(@spec_id) end - def test_retrieve_connection_pool_uses_superclass_when_no_subclass_connection - assert_not_nil @handler.retrieve_connection_pool(@subklass) - end - - def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove - sub_pool = @handler.establish_connection(@subklass, Base.connection_pool.spec) - assert_same sub_pool, @handler.retrieve_connection_pool(@subklass) +# def test_retrieve_connection_pool_uses_superclass_when_no_subclass_connection +# assert_not_nil @handler.retrieve_connection_pool(@subklass) +# end - @handler.remove_connection @subklass - assert_same @pool, @handler.retrieve_connection_pool(@subklass) - end +# def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove +# sub_pool = @handler.establish_connection(@subklass, Base.connection_pool.spec) +# assert_same sub_pool, @handler.retrieve_connection_pool(@subklass) +# +# @handler.remove_connection @subklass +# assert_same @pool, @handler.retrieve_connection_pool(@subklass) +# end def test_connection_pools assert_equal([@pool], @handler.connection_pools) end + # TODO if Process.respond_to?(:fork) def test_connection_pool_per_pid object_id = ActiveRecord::Base.connection.object_id @@ -79,7 +81,7 @@ module ActiveRecord pid = fork { rd.close - pool = @handler.retrieve_connection_pool(@klass) + pool = @handler.retrieve_connection_pool(@spec_id) wr.write Marshal.dump pool.schema_cache.size wr.close exit! -- cgit v1.2.3 From 79154a3281eb25a573dfcb5d5db31c3c481311f9 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 4 May 2016 14:05:31 -0500 Subject: Use spec key, when given as spec_id --- .../connection_adapters/connection_handler_test.rb | 34 ++++++++++------------ 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'activerecord/test/cases/connection_adapters/connection_handler_test.rb') diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index c3cdb29380..47e8486ded 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -6,10 +6,19 @@ module ActiveRecord def setup @handler = ConnectionHandler.new resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new Base.configurations - spec = resolver.spec(:arunit) - @spec_id = "primary" - @pool = @handler.establish_connection(spec) + @pool = @handler.establish_connection(resolver.spec(:arunit, @spec_id)) + end + + def test_establish_connection_uses_spec_id + config = {"readonly" => {"adapter" => 'sqlite3'}} + resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(config) + spec = resolver.spec(:readonly) + @handler.establish_connection(spec) + + assert_not_nil @handler.retrieve_connection_pool('readonly') + ensure + @handler.remove_connection('readonly') end def test_retrieve_connection @@ -24,31 +33,18 @@ module ActiveRecord assert !@handler.active_connections? end -# def test_retrieve_connection_pool_with_ar_base -# assert_nil @handler.retrieve_connection_pool(ActiveRecord::Base) -# end - def test_retrieve_connection_pool assert_not_nil @handler.retrieve_connection_pool(@spec_id) end -# def test_retrieve_connection_pool_uses_superclass_when_no_subclass_connection -# assert_not_nil @handler.retrieve_connection_pool(@subklass) -# end - -# def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove -# sub_pool = @handler.establish_connection(@subklass, Base.connection_pool.spec) -# assert_same sub_pool, @handler.retrieve_connection_pool(@subklass) -# -# @handler.remove_connection @subklass -# assert_same @pool, @handler.retrieve_connection_pool(@subklass) -# end + def test_retrieve_connection_pool_with_invalid_id + assert_nil @handler.retrieve_connection_pool("foo") + end def test_connection_pools assert_equal([@pool], @handler.connection_pools) end - # TODO if Process.respond_to?(:fork) def test_connection_pool_per_pid object_id = ActiveRecord::Base.connection.object_id -- cgit v1.2.3 From 598e7c9e2004b85146386571372423020ba7c52a Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Thu, 5 May 2016 15:25:08 -0500 Subject: s/specification_id/specification_name --- .../cases/connection_adapters/connection_handler_test.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'activerecord/test/cases/connection_adapters/connection_handler_test.rb') diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index 47e8486ded..fc5ca8865b 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -6,11 +6,11 @@ module ActiveRecord def setup @handler = ConnectionHandler.new resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new Base.configurations - @spec_id = "primary" - @pool = @handler.establish_connection(resolver.spec(:arunit, @spec_id)) + @spec_name = "primary" + @pool = @handler.establish_connection(resolver.spec(:arunit, @spec_name)) end - def test_establish_connection_uses_spec_id + def test_establish_connection_uses_spec_name config = {"readonly" => {"adapter" => 'sqlite3'}} resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(config) spec = resolver.spec(:readonly) @@ -22,19 +22,19 @@ module ActiveRecord end def test_retrieve_connection - assert @handler.retrieve_connection(@spec_id) + assert @handler.retrieve_connection(@spec_name) end def test_active_connections? assert !@handler.active_connections? - assert @handler.retrieve_connection(@spec_id) + assert @handler.retrieve_connection(@spec_name) assert @handler.active_connections? @handler.clear_active_connections! assert !@handler.active_connections? end def test_retrieve_connection_pool - assert_not_nil @handler.retrieve_connection_pool(@spec_id) + assert_not_nil @handler.retrieve_connection_pool(@spec_name) end def test_retrieve_connection_pool_with_invalid_id @@ -77,7 +77,7 @@ module ActiveRecord pid = fork { rd.close - pool = @handler.retrieve_connection_pool(@spec_id) + pool = @handler.retrieve_connection_pool(@spec_name) wr.write Marshal.dump pool.schema_cache.size wr.close exit! -- cgit v1.2.3