From 7b4866e0aef1ed8ec08fd9eac104df42bce3c1c9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Mar 2011 16:08:42 -0700 Subject: namespacing connection management tests. :heart: --- .../test/cases/connection_management_test.rb | 40 ++++++++++++---------- 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'activerecord/test/cases/connection_management_test.rb') diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index c535119972..c5511673ac 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -1,25 +1,29 @@ require "cases/helper" -class ConnectionManagementTest < ActiveRecord::TestCase - def setup - @env = {} - @app = stub('App') - @management = ActiveRecord::ConnectionAdapters::ConnectionManagement.new(@app) +module ActiveRecord + module ConnectionAdapters + class ConnectionManagementTest < ActiveRecord::TestCase + def setup + @env = {} + @app = stub('App') + @management = ConnectionManagement.new(@app) - @connections_cleared = false - ActiveRecord::Base.stubs(:clear_active_connections!).with { @connections_cleared = true } - end + @connections_cleared = false + ActiveRecord::Base.stubs(:clear_active_connections!).with { @connections_cleared = true } + end - test "clears active connections after each call" do - @app.expects(:call).with(@env) - @management.call(@env) - assert @connections_cleared - end + test "clears active connections after each call" do + @app.expects(:call).with(@env) + @management.call(@env) + assert @connections_cleared + end - test "doesn't clear active connections when running in a test case" do - @env['rack.test'] = true - @app.expects(:call).with(@env) - @management.call(@env) - assert !@connections_cleared + test "doesn't clear active connections when running in a test case" do + @env['rack.test'] = true + @app.expects(:call).with(@env) + @management.call(@env) + assert !@connections_cleared + end + end end end -- cgit v1.2.3 From ec0cacc2938cb7159ae6085fc653b2be906a748f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Mar 2011 16:22:37 -0700 Subject: testing app delegation from the ConnectionManagement middleware --- .../test/cases/connection_management_test.rb | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'activerecord/test/cases/connection_management_test.rb') diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index c5511673ac..82ea46b41f 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -3,24 +3,41 @@ require "cases/helper" 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 = stub('App') + @app = App.new @management = ConnectionManagement.new(@app) @connections_cleared = false ActiveRecord::Base.stubs(:clear_active_connections!).with { @connections_cleared = true } end + def test_app_delegation + manager = ConnectionManagement.new(@app) + + manager.call @env + assert_equal [@env], @app.calls + end + test "clears active connections after each call" do - @app.expects(:call).with(@env) @management.call(@env) assert @connections_cleared end test "doesn't clear active connections when running in a test case" do @env['rack.test'] = true - @app.expects(:call).with(@env) @management.call(@env) assert !@connections_cleared end -- cgit v1.2.3 From aea1477362b640ebe52cf991b915ad32e7bf2571 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Mar 2011 17:47:46 -0700 Subject: make sure we have an active database connection before running each connection management test --- activerecord/test/cases/connection_management_test.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activerecord/test/cases/connection_management_test.rb') diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index 82ea46b41f..1313e28bb1 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -20,8 +20,9 @@ module ActiveRecord @app = App.new @management = ConnectionManagement.new(@app) - @connections_cleared = false - ActiveRecord::Base.stubs(:clear_active_connections!).with { @connections_cleared = true } + # make sure we have an active connection + assert ActiveRecord::Base.connection + assert ActiveRecord::Base.connection_handler.active_connections? end def test_app_delegation @@ -33,13 +34,13 @@ module ActiveRecord test "clears active connections after each call" do @management.call(@env) - assert @connections_cleared + 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 !@connections_cleared + assert ActiveRecord::Base.connection_handler.active_connections? end end end -- cgit v1.2.3 From e5246092d1ce30961af4f7d9b5ad86071298cf1c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 29 Mar 2011 15:37:07 -0700 Subject: proxy body responses so we close database connections after body is flushed --- .../test/cases/connection_management_test.rb | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/connection_management_test.rb') diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index 1313e28bb1..3734f8e5f0 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -11,7 +11,7 @@ module ActiveRecord def call(env) @calls << env - [200, {}, [['hi mom']]] + [200, {}, ['hi mom']] end end @@ -32,11 +32,31 @@ module ActiveRecord assert_equal [@env], @app.calls end - test "clears active connections after each call" do + 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 + test "doesn't clear active connections when running in a test case" do @env['rack.test'] = true @management.call(@env) -- cgit v1.2.3 From 3b2a032677a2261499aa5d2de019f31f1173a858 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 29 Mar 2011 15:42:32 -0700 Subject: clearing active connections in the ConnectionManagement middleware if an exception happens --- activerecord/test/cases/connection_management_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases/connection_management_test.rb') diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index 3734f8e5f0..0d4a9a287e 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -57,6 +57,13 @@ module ActiveRecord assert ActiveRecord::Base.connection_handler.active_connections? end + def test_connections_closed_if_exception + 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 + test "doesn't clear active connections when running in a test case" do @env['rack.test'] = true @management.call(@env) -- cgit v1.2.3 From c7b7c6ad1c773102753f1a11b857d0e37ceb6a21 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 29 Mar 2011 15:47:16 -0700 Subject: make sure that active connections are not cleared during test when an exception happens --- activerecord/test/cases/connection_management_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/connection_management_test.rb') diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index 0d4a9a287e..85871aebdf 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -64,6 +64,14 @@ module ActiveRecord 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 + test "doesn't clear active connections when running in a test case" do @env['rack.test'] = true @management.call(@env) -- cgit v1.2.3