Skip to content

Commit b504941

Browse files
committed
Ensure a clean connection pool state for the deferred test cases
1 parent 68a7f91 commit b504941

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

lib/active_record/connection_adapters/mysqlplus_adapter/deferrable/macro.rb

+19-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ module SingletonMethods
3939
# ....
4040
#
4141
def preload_associations_with_defer(records, associations, preload_options={})
42-
if preload_options.key?(:defer)
42+
if preload_deferred?( associations )
43+
associations.delete(:defer)
4344
records = [records].flatten.compact.uniq
4445
return if records.empty?
4546
case associations
46-
when Array then associations.each {|association| ActiveRecord::Deferrable::Result.new{ preload_associations(records, association, preload_options) } }
47+
when Array then associations.each {|association| ActiveRecord::Deferrable::Result.new{ preload_associations_without_defer(records, association, preload_options) } }
4748
when Symbol, String then ActiveRecord::Deferrable::Result.new{ preload_one_association(records, associations.to_sym, preload_options) }
4849
when Hash then
4950
associations.each do |parent, child|
@@ -111,7 +112,7 @@ def find_every_with_defer(options) #:nodoc:
111112
else
112113
records = find_by_sql(construct_finder_sql(options))
113114
if include_associations.any?
114-
preload_associations(records, include_associations, options)
115+
preload_associations(records, preload_deferred_includes( include_associations, options ))
115116
end
116117
end
117118

@@ -120,6 +121,21 @@ def find_every_with_defer(options) #:nodoc:
120121
records
121122
end
122123

124+
def preload_deferred_includes( include_associations, options )
125+
options[:defer] ? (Array(include_associations) << :defer) : include_associations
126+
end
127+
128+
def preload_deferred?( associations ) #:nodoc:
129+
begin
130+
associations.respond_to?(:include?) && associations.include?(:defer)
131+
rescue TypeError
132+
#failing test cases :
133+
# * test_eager_with_valid_association_as_string_not_symbol(EagerAssociationTest) &&
134+
# * test_eager_with_invalid_association_reference(EagerAssociationTest)
135+
false
136+
end
137+
end
138+
123139
end
124140

125141
end

lib/active_record/connection_adapters/mysqlplus_adapter/deferrable/result.rb

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def initialize( &deferrable )
1616
#
1717
def defer!( deferrable )
1818
@result = Thread.new( deferrable ) do |deferrable|
19-
puts '*'
2019
begin
2120
deferrable.call
2221
rescue => exception

test/deferrable/macro_test.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
require "#{File.dirname(__FILE__)}/../helper"
22
Mysqlplus::Test.prepare!
33

4-
class MacroTest < ActiveSupport::TestCase
4+
class MacroTest < ActiveRecord::TestCase
5+
6+
def teardown
7+
ActiveRecord::Base.clear_all_connections!
8+
ActiveRecord::Base.establish_connection(Mysqlplus::Test::CONNECTION)
9+
super
10+
end
511

612
def test_should_be_able_to_find_records_in_a_background_thread
713
ActiveRecord::Base.connection_pool.expects(:release_connection).twice
@@ -16,7 +22,8 @@ def test_should_be_able_to_find_records_by_sql_background_thread
1622

1723
def test_should_be_able_to_preload_related_records_on_multiple_connections
1824
ActiveRecord::Base.connection_pool.expects(:release_connection).twice
19-
assert_instance_of MysqlUser, MysqlUser.find( :first, :defer => true, :include => [:mysql_user_info])
25+
assert_instance_of MysqlUser, MysqlUser.find( :first, :defer => true, :include => :mysql_user_info)
26+
sleep(0.5)
2027
end
2128

2229
end

test/helper.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
module Mysqlplus
99
class Test
1010

11+
CONNECTION = { :adapter => 'mysqlplus',
12+
:username => 'root',
13+
:database => 'mysql',
14+
:pool => 5,
15+
:warmup => true }
16+
1117
MODELS_DIR = "#{File.dirname(__FILE__)}/models".freeze
1218

1319
class << self
@@ -40,11 +46,7 @@ def test_files
4046
private
4147

4248
def connect!
43-
::ActiveRecord::Base.establish_connection( :adapter => 'mysqlplus',
44-
:username => 'root',
45-
:database => 'mysql',
46-
:pool => 5,
47-
:warmup => true )
49+
::ActiveRecord::Base.establish_connection( CONNECTION )
4850
end
4951

5052
def require_models

0 commit comments

Comments
 (0)