aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/concurrency/load_interlock_aware_monitor_test.rb
blob: 2d0f45ec5f3800794f670e77988f5018559512ea (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
# frozen_string_literal: true

require "abstract_unit"
require "concurrent/atomic/count_down_latch"
require "active_support/concurrency/load_interlock_aware_monitor"

module ActiveSupport
  module Concurrency
    class LoadInterlockAwareMonitorTest < ActiveSupport::TestCase
      def setup
        @monitor = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
      end

      def test_entering_with_no_blocking
        assert @monitor.mon_enter
      end

      def test_entering_with_blocking
        load_interlock_latch = Concurrent::CountDownLatch.new
        monitor_latch = Concurrent::CountDownLatch.new

        able_to_use_monitor = false
        able_to_load = false

        thread_with_load_interlock = Thread.new do
          ActiveSupport::Dependencies.interlock.running do
            load_interlock_latch.count_down
            monitor_latch.wait

            @monitor.synchronize do
              able_to_use_monitor = true
            end
          end
        end

        thread_with_monitor_lock = Thread.new do
          @monitor.synchronize do
            monitor_latch.count_down
            load_interlock_latch.wait

            ActiveSupport::Dependencies.interlock.loading do
              able_to_load = true
            end
          end
        end

        thread_with_load_interlock.join
        thread_with_monitor_lock.join

        assert able_to_use_monitor
        assert able_to_load
      end
    end
  end
end