aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-11-26 20:02:57 +1030
committerGitHub <noreply@github.com>2017-11-26 20:02:57 +1030
commit3ba20c7fa29b62fa5e39b234f970f93b755b8042 (patch)
treeb0099689812f2ab2ad93b828b1160ed37c985f8b /activerecord/test
parent4b2bbcf89787d8fd96fbf67675ba78a50a5d1570 (diff)
parent9027fafff6da932e6e64ddb828665f4b01fc8902 (diff)
downloadrails-3ba20c7fa29b62fa5e39b234f970f93b755b8042.tar.gz
rails-3ba20c7fa29b62fa5e39b234f970f93b755b8042.tar.bz2
rails-3ba20c7fa29b62fa5e39b234f970f93b755b8042.zip
Merge pull request #31221 from matthewd/flush-idle-connections
Flush idle database connections
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/connection_pool_test.rb47
-rw-r--r--activerecord/test/cases/reaper_test.rb6
2 files changed, 53 insertions, 0 deletions
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb
index cb2fefb4f6..1e08cc74dc 100644
--- a/activerecord/test/cases/connection_pool_test.rb
+++ b/activerecord/test/cases/connection_pool_test.rb
@@ -156,6 +156,53 @@ module ActiveRecord
@pool.connections.each { |conn| conn.close if conn.in_use? }
end
+ def test_flush
+ idle_conn = @pool.checkout
+ recent_conn = @pool.checkout
+ active_conn = @pool.checkout
+
+ @pool.checkin idle_conn
+ @pool.checkin recent_conn
+
+ assert_equal 3, @pool.connections.length
+
+ def idle_conn.seconds_idle
+ 1000
+ end
+
+ @pool.flush(30)
+
+ assert_equal 2, @pool.connections.length
+
+ assert_equal [recent_conn, active_conn].sort_by(&:__id__), @pool.connections.sort_by(&:__id__)
+ ensure
+ @pool.checkin active_conn
+ end
+
+ def test_flush_bang
+ idle_conn = @pool.checkout
+ recent_conn = @pool.checkout
+ active_conn = @pool.checkout
+ _dead_conn = Thread.new { @pool.checkout }.join
+
+ @pool.checkin idle_conn
+ @pool.checkin recent_conn
+
+ assert_equal 4, @pool.connections.length
+
+ def idle_conn.seconds_idle
+ 1000
+ end
+
+ @pool.flush!
+
+ assert_equal 1, @pool.connections.length
+
+ assert_equal [active_conn].sort_by(&:__id__), @pool.connections.sort_by(&:__id__)
+ ensure
+ @pool.checkin active_conn
+ end
+
def test_remove_connection
conn = @pool.checkout
assert conn.in_use?
diff --git a/activerecord/test/cases/reaper_test.rb b/activerecord/test/cases/reaper_test.rb
index 49170abe6f..6c7727ab1b 100644
--- a/activerecord/test/cases/reaper_test.rb
+++ b/activerecord/test/cases/reaper_test.rb
@@ -18,6 +18,7 @@ module ActiveRecord
class FakePool
attr_reader :reaped
+ attr_reader :flushed
def initialize
@reaped = false
@@ -26,6 +27,10 @@ module ActiveRecord
def reap
@reaped = true
end
+
+ def flush
+ @flushed = true
+ end
end
# A reaper with nil time should never reap connections
@@ -47,6 +52,7 @@ module ActiveRecord
Thread.pass
end
assert fp.reaped
+ assert fp.flushed
end
def test_pool_has_reaper