aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/reaper_test.rb
blob: 576ab600909b96f88b4f0ef1740e443e80f4a13c (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
require "cases/helper"

module ActiveRecord
  module ConnectionAdapters
    class ReaperTest < ActiveRecord::TestCase
      attr_reader :pool

      def setup
        super
        @pool = ConnectionPool.new ActiveRecord::Base.connection_pool.spec
      end

      def teardown
        super
        @pool.connections.each(&:close)
      end

      class FakePool
        attr_reader :reaped

        def initialize
          @reaped = false
        end

        def reap
          @reaped = true
        end
      end

      # A reaper with nil time should never reap connections
      def test_nil_time
        fp = FakePool.new
        assert !fp.reaped
        reaper = ConnectionPool::Reaper.new(fp, nil)
        reaper.run
        assert !fp.reaped
      end

      def test_some_time
        fp = FakePool.new
        assert !fp.reaped

        reaper = ConnectionPool::Reaper.new(fp, 0.0001)
        reaper.run
        until fp.reaped
          Thread.pass
        end
        assert fp.reaped
      end

      def test_pool_has_reaper
        assert pool.reaper
      end

      def test_reaping_frequency_configuration
        spec = ActiveRecord::Base.connection_pool.spec.dup
        spec.config[:reaping_frequency] = 100
        pool = ConnectionPool.new spec
        assert_equal 100, pool.reaper.frequency
      end

      def test_connection_pool_starts_reaper
        spec = ActiveRecord::Base.connection_pool.spec.dup
        spec.config[:reaping_frequency] = 0.0001

        pool = ConnectionPool.new spec
        pool.timeout = 0

        conn = pool.checkout
        count = pool.connections.length

        conn.extend(Module.new { def active?; false; end; })

        while count == pool.connections.length
          Thread.pass
        end
        assert_equal(count - 1, pool.connections.length)
      end
    end
  end
end