File: connection_handling_test.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (187 lines) | stat: -rw-r--r-- 6,713 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# frozen_string_literal: true

require "cases/helper"
require "models/post"

module ActiveRecord
  class ConnectionHandlingTest < ActiveRecord::TestCase
    fixtures :posts

    setup do
      @_permanent_connection_checkout_was = ActiveRecord.permanent_connection_checkout
    end

    teardown do
      ActiveRecord.permanent_connection_checkout = @_permanent_connection_checkout_was
    end

    unless in_memory_db?
      test "#with_connection lease the connection for the duration of the block" do
        ActiveRecord::Base.release_connection
        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?

        ActiveRecord::Base.with_connection do |connection|
          assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
        end

        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?
      end

      test "#lease_connection makes the lease permanent even inside #with_connection" do
        ActiveRecord::Base.release_connection
        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?

        conn = nil
        ActiveRecord::Base.with_connection do |connection|
          conn = connection
          assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
          2.times do
            assert_same connection, ActiveRecord::Base.lease_connection
          end
        end

        assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
        assert_same conn, ActiveRecord::Base.lease_connection
      end

      test "#lease_connection makes the lease permanent even inside #with_connection(prevent_permanent_checkout: true)" do
        ActiveRecord::Base.release_connection

        ActiveRecord::Base.with_connection(prevent_permanent_checkout: true) do |connection|
          assert_same connection, ActiveRecord::Base.lease_connection
        end

        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?
      end

      test "#with_connection use the already leased connection if available" do
        leased_connection = ActiveRecord::Base.lease_connection
        assert_predicate ActiveRecord::Base.connection_pool, :active_connection?

        ActiveRecord::Base.with_connection do |connection|
          assert_same leased_connection, connection
          assert_same ActiveRecord::Base.lease_connection, connection
        end

        assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
        assert_same ActiveRecord::Base.lease_connection, leased_connection
      end

      test "#with_connection is reentrant" do
        leased_connection = ActiveRecord::Base.lease_connection
        assert_predicate ActiveRecord::Base.connection_pool, :active_connection?

        ActiveRecord::Base.with_connection do |connection|
          assert_same leased_connection, connection
          assert_same ActiveRecord::Base.lease_connection, connection

          ActiveRecord::Base.with_connection do |connection2|
            assert_same leased_connection, connection2
            assert_same ActiveRecord::Base.lease_connection, connection2
          end
        end

        assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
        assert_same ActiveRecord::Base.lease_connection, leased_connection
      end

      test "#connection is a soft-deprecated alias to #lease_connection" do
        ActiveRecord.permanent_connection_checkout = true

        ActiveRecord::Base.release_connection
        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?

        conn = nil
        ActiveRecord::Base.with_connection do |connection|
          conn = connection
          assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
          2.times do
            assert_same connection, ActiveRecord::Base.connection
          end
        end

        assert_predicate ActiveRecord::Base.connection_pool, :active_connection?
        assert_same conn, ActiveRecord::Base.connection

        ActiveRecord::Base.release_connection
      end

      test "#connection emits a deprecation warning if ActiveRecord.permanent_connection_checkout == :deprecated" do
        ActiveRecord.permanent_connection_checkout = :deprecated

        ActiveRecord::Base.release_connection

        assert_deprecated(ActiveRecord.deprecator) do
          ActiveRecord::Base.connection
        end

        assert_not_deprecated(ActiveRecord.deprecator) do
          ActiveRecord::Base.connection
        end

        ActiveRecord::Base.release_connection

        assert_deprecated(ActiveRecord.deprecator) do
          ActiveRecord::Base.connection
        end

        ActiveRecord::Base.release_connection

        ActiveRecord::Base.with_connection do
          assert_deprecated(ActiveRecord.deprecator) do
            ActiveRecord::Base.connection
          end
        end
      end

      test "#connection raises an error if ActiveRecord.permanent_connection_checkout == :disallowed" do
        ActiveRecord.permanent_connection_checkout = :disallowed

        ActiveRecord::Base.release_connection

        assert_raises(ActiveRecordError) do
          ActiveRecord::Base.connection
        end

        ActiveRecord::Base.with_connection do
          assert_raises(ActiveRecordError) do
            ActiveRecord::Base.connection
          end
        end

        ActiveRecord::Base.lease_connection

        assert_nothing_raised do
          ActiveRecord::Base.connection
        end
      end

      test "#connection doesn't make the lease permanent if inside #with_connection(prevent_permanent_checkout: true)" do
        ActiveRecord.permanent_connection_checkout = :disallowed

        ActiveRecord::Base.release_connection

        ActiveRecord::Base.with_connection(prevent_permanent_checkout: true) do |connection|
          assert_same connection, ActiveRecord::Base.connection
        end

        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?
      end

      test "common APIs don't permanently hold a connection when permanent checkout is deprecated or disallowed" do
        ActiveRecord.permanent_connection_checkout = :deprecated
        ActiveRecord::Base.release_connection
        assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection?

        Post.create!(title: "foo", body: "bar")
        assert_not_predicate Post.connection_pool, :active_connection?

        Post.first
        assert_not_predicate Post.connection_pool, :active_connection?

        Post.count
        assert_not_predicate Post.connection_pool, :active_connection?
      end
    end
  end
end