2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-08-11 01:13:37 -04:00
|
|
|
|
|
|
|
describe 'pool drainer' do
|
|
|
|
let(:pool) do
|
|
|
|
ActiveRecord::Base.connection_pool
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can correctly drain the connection pool' do
|
|
|
|
pool.drain
|
|
|
|
old = pool.connections.length
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(old).to eq(1)
|
2014-08-11 01:13:37 -04:00
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
conn = pool.checkout
|
|
|
|
pool.checkin conn
|
|
|
|
end.join
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(pool.connections.length).to eq(old + 1)
|
2014-08-11 01:13:37 -04:00
|
|
|
pool.drain
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(pool.connections.length).to eq(old)
|
2014-08-11 01:13:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can drain with idle time setting' do
|
|
|
|
pool.drain
|
|
|
|
old = pool.connections.length
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(old).to eq(1)
|
2014-08-11 01:13:37 -04:00
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
conn = pool.checkout
|
|
|
|
pool.checkin conn
|
|
|
|
end.join
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(pool.connections.length).to eq(old + 1)
|
2014-08-11 01:13:37 -04:00
|
|
|
pool.drain(1.minute)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(pool.connections.length).to eq(old + 1)
|
2014-08-11 01:13:37 -04:00
|
|
|
|
|
|
|
# make sure we don't corrupt internal state
|
|
|
|
20.times do
|
|
|
|
conn = pool.checkout
|
|
|
|
pool.checkin conn
|
|
|
|
pool.drain
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|