aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/module/synchronization_test.rb
blob: fe0f3b1a3bfa28f6a11e5b951124cddbda53934e (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
require 'abstract_unit'

class SynchronizationTest < Test::Unit::TestCase
  def setup
    @target = Class.new
    @target.cattr_accessor :mutex, :instance_writer => false
    @target.mutex = Mutex.new
    @instance = @target.new
  end

  def test_synchronize_aliases_method_chain_with_synchronize
    @target.module_eval do
      attr_accessor :value
      synchronize :value, :with => :mutex
    end
    assert @instance.respond_to?(:value_with_synchronization)
    assert @instance.respond_to?(:value_without_synchronization)
  end

  def test_synchronize_does_not_change_behavior
    @target.module_eval do
      attr_accessor :value
      synchronize :value, :with => :mutex
    end
    expected = "some state"
    @instance.value = expected
    assert_equal expected, @instance.value
  end

  def test_synchronize_with_no_mutex_raises_an_argument_error
    assert_raises(ArgumentError) do
      @target.synchronize :to_s
    end
  end

  def test_double_synchronize_raises_an_argument_error
    @target.synchronize :to_s, :with => :mutex
    assert_raises(ArgumentError) do
      @target.synchronize :to_s, :with => :mutex
    end
  end

  def test_mutex_is_entered_during_method_call
    dummy = Object.new
    def dummy.synchronize
      @sync_count ||= 0
      @sync_count += 1
      yield
    end
    def dummy.sync_count; @sync_count; end
    @target.mutex = dummy
    @target.synchronize :to_s, :with => :mutex
    @instance.to_s
    @instance.to_s
    assert_equal 2, dummy.sync_count
  end

  def test_can_synchronize_method_with_punctuation
    @target.module_eval do
      def dangerous?
        @dangerous
      end
      def dangerous!
        @dangerous = true
      end
    end
    @target.synchronize :dangerous?, :dangerous!, :with => :mutex
    @instance.dangerous!
    assert @instance.dangerous?
  end
end