aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_management_test.rb
blob: 85871aebdf887a0ef28451ce7283d3618478af3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 = 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; end }.new
        explosive = ConnectionManagement.new(app)
        assert_raises(RuntimeError) { 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

      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
    end
  end
end