aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/cache/behaviors/connection_pool_behavior.rb
blob: 4d1901a17367754bb1a783202f0915373a4d7909 (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
# frozen_string_literal: true

module ConnectionPoolBehavior
  def test_connection_pool
    Thread.report_on_exception, original_report_on_exception = false, Thread.report_on_exception

    threads = []

    emulating_latency do
      begin
        cache = ActiveSupport::Cache.lookup_store(store, { pool_size: 2, pool_timeout: 1 }.merge(store_options))
        cache.clear

        assert_raises Timeout::Error do
          # One of the three threads will fail in 1 second because our pool size
          # is only two.
          3.times do
            threads << Thread.new do
              cache.read("latency")
            end
          end

          threads.each(&:join)
        end
      ensure
        threads.each(&:kill)
      end
    end
  ensure
    Thread.report_on_exception = original_report_on_exception
  end

  def test_no_connection_pool
    threads = []

    emulating_latency do
      begin
        cache = ActiveSupport::Cache.lookup_store(store, store_options)
        cache.clear

        assert_nothing_raised do
          # Default connection pool size is 5, assuming 10 will make sure that
          # the connection pool isn't used at all.
          10.times do
            threads << Thread.new do
              cache.read("latency")
            end
          end

          threads.each(&:join)
        end
      ensure
        threads.each(&:kill)
      end
    end
  end

  private
    def store_options; {}; end
end