Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / pooled_connections_test.rb
1 require "cases/helper"
2
3 class PooledConnectionsTest < ActiveRecord::TestCase
4 def setup
5 super
6 @connection = ActiveRecord::Base.remove_connection
7 end
8
9 def teardown
10 ActiveRecord::Base.clear_all_connections!
11 ActiveRecord::Base.establish_connection(@connection)
12 super
13 end
14
15 def checkout_connections
16 ActiveRecord::Base.establish_connection(@connection.merge({:pool => 2, :wait_timeout => 0.3}))
17 @connections = []
18 @timed_out = 0
19
20 4.times do
21 Thread.new do
22 begin
23 @connections << ActiveRecord::Base.connection_pool.checkout
24 rescue ActiveRecord::ConnectionTimeoutError
25 @timed_out += 1
26 end
27 end.join
28 end
29 end
30
31 # Will deadlock due to lack of Monitor timeouts in 1.9
32 if RUBY_VERSION < '1.9'
33 def test_pooled_connection_checkout
34 checkout_connections
35 assert_equal @connections.length, 2
36 assert_equal @timed_out, 2
37 end
38 end
39
40 def checkout_checkin_connections(pool_size, threads)
41 ActiveRecord::Base.establish_connection(@connection.merge({:pool => pool_size, :wait_timeout => 0.5}))
42 @connection_count = 0
43 @timed_out = 0
44 threads.times do
45 Thread.new do
46 begin
47 conn = ActiveRecord::Base.connection_pool.checkout
48 sleep 0.1
49 ActiveRecord::Base.connection_pool.checkin conn
50 @connection_count += 1
51 rescue ActiveRecord::ConnectionTimeoutError
52 @timed_out += 1
53 end
54 end.join
55 end
56 end
57
58 def test_pooled_connection_checkin_one
59 checkout_checkin_connections 1, 2
60 assert_equal 2, @connection_count
61 assert_equal 0, @timed_out
62 end
63
64 def test_pooled_connection_checkin_two
65 checkout_checkin_connections 2, 3
66 assert_equal 3, @connection_count
67 assert_equal 0, @timed_out
68 end
69
70 def test_pooled_connection_checkout_existing_first
71 ActiveRecord::Base.establish_connection(@connection.merge({:pool => 1}))
72 conn_pool = ActiveRecord::Base.connection_pool
73 conn = conn_pool.checkout
74 conn_pool.checkin(conn)
75 conn = conn_pool.checkout
76 assert ActiveRecord::ConnectionAdapters::AbstractAdapter === conn
77 conn_pool.checkin(conn)
78 end
79
80 def test_not_connected_defined_connection_returns_false
81 ActiveRecord::Base.establish_connection(@connection)
82 assert ! ActiveRecord::Base.connected?
83 end
84
85 def test_undefined_connection_returns_false
86 old_handler = ActiveRecord::Base.connection_handler
87 ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
88 assert_equal false, ActiveRecord::Base.connected?
89 ensure
90 ActiveRecord::Base.connection_handler = old_handler
91 end
92 end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name
93
94 class AllowConcurrencyDeprecatedTest < ActiveRecord::TestCase
95 def test_allow_concurrency_is_deprecated
96 assert_deprecated('ActiveRecord::Base.allow_concurrency') do
97 ActiveRecord::Base.allow_concurrency
98 end
99 assert_deprecated('ActiveRecord::Base.allow_concurrency=') do
100 ActiveRecord::Base.allow_concurrency = true
101 end
102 end
103 end