1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/connection_management_test.rb
Eugene Kenny 19bc570832 Add schema cache to new connection pool after fork
Active Record detects when the process has forked and automatically
creates a new connection pool to avoid sharing file descriptors.

If the existing connection pool had a schema cache associated with it,
the new pool should copy it to avoid unnecessarily querying the database
for its schema.

The code to detect that the process has forked is in ConnectionHandler,
but the existing test for it was in the ConnectionManagement test file.
I moved it to the right place while I was writing the new test for this
change.
2015-05-17 00:57:52 +01:00

99 lines
3.2 KiB
Ruby

require "cases/helper"
require "rack"
module ActiveRecord
module ConnectionAdapters
class ConnectionManagementTest < ActiveRecord::TestCase
class App
attr_reader :calls
def initialize
@calls = []
end
def call(env)
@calls << env
[200, {}, ['hi mom']]
end
end
def setup
@env = {}
@app = App.new
@management = ConnectionManagement.new(@app)
# make sure we have an active connection
assert ActiveRecord::Base.connection
assert ActiveRecord::Base.connection_handler.active_connections?
end
def test_app_delegation
manager = ConnectionManagement.new(@app)
manager.call @env
assert_equal [@env], @app.calls
end
def test_connections_are_active_after_call
@management.call(@env)
assert ActiveRecord::Base.connection_handler.active_connections?
end
def test_body_responds_to_each
_, _, body = @management.call(@env)
bits = []
body.each { |bit| bits << bit }
assert_equal ['hi mom'], bits
end
def test_connections_are_cleared_after_body_close
_, _, body = @management.call(@env)
body.close
assert !ActiveRecord::Base.connection_handler.active_connections?
end
def test_active_connections_are_not_cleared_on_body_close_during_test
@env['rack.test'] = true
_, _, body = @management.call(@env)
body.close
assert ActiveRecord::Base.connection_handler.active_connections?
end
def test_connections_closed_if_exception
app = Class.new(App) { def call(env); raise NotImplementedError; end }.new
explosive = ConnectionManagement.new(app)
assert_raises(NotImplementedError) { explosive.call(@env) }
assert !ActiveRecord::Base.connection_handler.active_connections?
end
def test_connections_not_closed_if_exception_and_test
@env['rack.test'] = true
app = Class.new(App) { def call(env); raise; end }.new
explosive = ConnectionManagement.new(app)
assert_raises(RuntimeError) { explosive.call(@env) }
assert ActiveRecord::Base.connection_handler.active_connections?
end
def test_connections_closed_if_exception_and_explicitly_not_test
@env['rack.test'] = false
app = Class.new(App) { def call(env); raise NotImplementedError; end }.new
explosive = ConnectionManagement.new(app)
assert_raises(NotImplementedError) { explosive.call(@env) }
assert !ActiveRecord::Base.connection_handler.active_connections?
end
test "doesn't clear active connections when running in a test case" do
@env['rack.test'] = true
@management.call(@env)
assert ActiveRecord::Base.connection_handler.active_connections?
end
test "proxy is polite to its body and responds to it" do
body = Class.new(String) { def to_path; "/path"; end }.new
app = lambda { |_| [200, {}, body] }
response_body = ConnectionManagement.new(app).call(@env)[2]
assert response_body.respond_to?(:to_path)
assert_equal response_body.to_path, "/path"
end
end
end
end