2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2012-01-16 06:36:41 -05:00
|
|
|
require "rack"
|
2009-02-25 04:29:22 -05:00
|
|
|
|
2011-03-28 19:08:42 -04:00
|
|
|
module ActiveRecord
|
|
|
|
module ConnectionAdapters
|
|
|
|
class ConnectionManagementTest < ActiveRecord::TestCase
|
2016-04-22 14:56:43 -04:00
|
|
|
self.use_transactional_tests = false
|
|
|
|
|
2011-03-28 19:22:37 -04:00
|
|
|
class App
|
|
|
|
attr_reader :calls
|
|
|
|
def initialize
|
|
|
|
@calls = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@calls << env
|
2011-03-29 18:37:07 -04:00
|
|
|
[200, {}, ['hi mom']]
|
2011-03-28 19:22:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-28 19:08:42 -04:00
|
|
|
def setup
|
|
|
|
@env = {}
|
2011-03-28 19:22:37 -04:00
|
|
|
@app = App.new
|
2016-02-21 20:25:52 -05:00
|
|
|
@management = middleware(@app)
|
2010-07-16 16:50:58 -04:00
|
|
|
|
2011-03-28 20:47:46 -04:00
|
|
|
# make sure we have an active connection
|
|
|
|
assert ActiveRecord::Base.connection
|
|
|
|
assert ActiveRecord::Base.connection_handler.active_connections?
|
2011-03-28 19:08:42 -04:00
|
|
|
end
|
2010-07-16 16:50:58 -04:00
|
|
|
|
2011-03-28 19:22:37 -04:00
|
|
|
def test_app_delegation
|
2016-02-21 20:25:52 -05:00
|
|
|
manager = middleware(@app)
|
2011-03-28 19:22:37 -04:00
|
|
|
|
|
|
|
manager.call @env
|
|
|
|
assert_equal [@env], @app.calls
|
|
|
|
end
|
|
|
|
|
2011-03-29 18:37:07 -04:00
|
|
|
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
|
2011-03-28 20:47:46 -04:00
|
|
|
assert !ActiveRecord::Base.connection_handler.active_connections?
|
2011-03-28 19:08:42 -04:00
|
|
|
end
|
2010-07-16 16:50:58 -04:00
|
|
|
|
2016-04-22 14:56:43 -04:00
|
|
|
def test_active_connections_are_not_cleared_on_body_close_during_transaction
|
|
|
|
ActiveRecord::Base.transaction do
|
2016-02-21 20:25:52 -05:00
|
|
|
_, _, body = @management.call(@env)
|
|
|
|
body.close
|
|
|
|
assert ActiveRecord::Base.connection_handler.active_connections?
|
|
|
|
end
|
2011-03-29 18:37:07 -04:00
|
|
|
end
|
|
|
|
|
2011-03-29 18:42:32 -04:00
|
|
|
def test_connections_closed_if_exception
|
2013-07-21 16:11:41 -04:00
|
|
|
app = Class.new(App) { def call(env); raise NotImplementedError; end }.new
|
2016-02-21 20:25:52 -05:00
|
|
|
explosive = middleware(app)
|
2013-07-21 16:11:41 -04:00
|
|
|
assert_raises(NotImplementedError) { explosive.call(@env) }
|
2011-03-29 18:42:32 -04:00
|
|
|
assert !ActiveRecord::Base.connection_handler.active_connections?
|
|
|
|
end
|
|
|
|
|
2016-04-22 14:56:43 -04:00
|
|
|
def test_connections_not_closed_if_exception_inside_transaction
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
app = Class.new(App) { def call(env); raise RuntimeError; end }.new
|
2016-02-21 20:25:52 -05:00
|
|
|
explosive = middleware(app)
|
|
|
|
assert_raises(RuntimeError) { explosive.call(@env) }
|
|
|
|
assert ActiveRecord::Base.connection_handler.active_connections?
|
|
|
|
end
|
2014-09-22 22:26:54 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 19:08:42 -04:00
|
|
|
test "doesn't clear active connections when running in a test case" do
|
2016-02-21 20:25:52 -05:00
|
|
|
executor.wrap do
|
|
|
|
@management.call(@env)
|
|
|
|
assert ActiveRecord::Base.connection_handler.active_connections?
|
|
|
|
end
|
2011-03-28 19:08:42 -04:00
|
|
|
end
|
2011-06-18 17:30:01 -04:00
|
|
|
|
2015-04-15 02:13:14 -04:00
|
|
|
test "proxy is polite to its body and responds to it" do
|
2011-06-18 17:30:01 -04:00
|
|
|
body = Class.new(String) { def to_path; "/path"; end }.new
|
2012-01-16 06:36:41 -05:00
|
|
|
app = lambda { |_| [200, {}, body] }
|
2016-02-21 20:25:52 -05:00
|
|
|
response_body = middleware(app).call(@env)[2]
|
2012-01-16 06:36:41 -05:00
|
|
|
assert response_body.respond_to?(:to_path)
|
2015-11-07 05:35:01 -05:00
|
|
|
assert_equal "/path", response_body.to_path
|
2011-06-18 17:30:01 -04:00
|
|
|
end
|
2015-10-15 15:25:47 -04:00
|
|
|
|
|
|
|
test "doesn't mutate the original response" do
|
|
|
|
original_response = [200, {}, 'hi']
|
|
|
|
app = lambda { |_| original_response }
|
2016-02-21 20:25:52 -05:00
|
|
|
middleware(app).call(@env)[2]
|
2015-11-07 05:35:01 -05:00
|
|
|
assert_equal 'hi', original_response.last
|
2015-10-15 15:25:47 -04:00
|
|
|
end
|
2016-02-21 20:25:52 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
def executor
|
|
|
|
@executor ||= Class.new(ActiveSupport::Executor).tap do |exe|
|
|
|
|
ActiveRecord::QueryCache.install_executor_hooks(exe)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def middleware(app)
|
|
|
|
lambda do |env|
|
|
|
|
a, b, c = executor.wrap { app.call(env) }
|
|
|
|
[a, b, Rack::BodyProxy.new(c) { }]
|
|
|
|
end
|
|
|
|
end
|
2011-03-28 19:08:42 -04:00
|
|
|
end
|
2009-02-25 04:29:22 -05:00
|
|
|
end
|
2010-07-16 16:50:58 -04:00
|
|
|
end
|