File: atomic_boolean_spec.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; javascript: 1,114; ansic: 288; makefile: 10; sh: 6
file content (186 lines) | stat: -rw-r--r-- 4,541 bytes parent folder | download | duplicates (2)
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
RSpec.shared_examples :atomic_boolean do

  describe 'construction' do

    it 'sets the initial value' do
      expect(described_class.new(true).value).to be true
    end

    it 'defaults the initial value to false' do
      expect(described_class.new.value).to be false
    end

    it 'evaluates the truthiness of a true value' do
      expect(described_class.new(10).value).to be true
    end

    it 'evaluates the truthiness of a false value' do
      expect(described_class.new(nil).value).to be false
    end
  end

  describe '#value' do

    it 'returns the current value' do
      counter = described_class.new(true)
      expect(counter.value).to be true
      counter.make_false
      expect(counter.value).to be false
      counter.make_true
      expect(counter.value).to be true
    end
  end

  describe '#value=' do

    it 'sets the #value to the given `Boolean`' do
      atomic = described_class.new(true)
      atomic.value = false
      expect(atomic.value).to be false
    end

    it 'returns the new value' do
      atomic = described_class.new(false)
      expect(atomic.value = true).to be true
    end

    it 'evaluates the truthiness of a true value' do
      atomic = described_class.new(false)
      atomic.value = 10
      expect(atomic.value).to be true
    end

    it 'evaluates the truthiness of a false value' do
      atomic = described_class.new(true)
      atomic.value = nil
      expect(atomic.value).to be false
    end
  end

  describe '#true?' do

    specify { expect(described_class.new(true).true?).to be true }

    specify { expect(described_class.new(false).true?).to be false }
  end

  describe '#false?' do

    specify { expect(described_class.new(true).false?).to be false }

    specify { expect(described_class.new(false).false?).to be true }
  end

  describe '#make_true' do

    it 'makes a false value true and returns true' do
      subject = described_class.new(false)
      expect(subject.make_true).to be true
      expect(subject.value).to be true
    end

    it 'keeps a true value true and returns false' do
      subject = described_class.new(true)
      expect(subject.make_true).to be false
      expect(subject.value).to be true
    end
  end

  describe '#make_false' do

    it 'makes a true value false and returns true' do
      subject = described_class.new(true)
      expect(subject.make_false).to be true
      expect(subject.value).to be false
    end

    it 'keeps a false value false and returns false' do
      subject = described_class.new(false)
      expect(subject.make_false).to be false
      expect(subject.value).to be false
    end
  end
end

module Concurrent

  RSpec.describe MutexAtomicBoolean do

    it_should_behave_like :atomic_boolean

    context 'instance methods' do

      before(:each) do
        expect(subject).to receive(:synchronize).with(no_args).and_return(true)
      end

      specify 'value is synchronized' do
        subject.value
      end

      specify 'value= is synchronized' do
        subject.value = 10
      end

      specify 'true? is synchronized' do
        subject.true?
      end

      specify 'false? is synchronized' do
        subject.false?
      end

      specify 'make_true is synchronized' do
        subject.make_true
      end

      specify 'make_false is synchronized' do
        subject.make_false
      end
    end
  end

  if defined? Concurrent::CAtomicBoolean

    RSpec.describe CAtomicBoolean do
      it_should_behave_like :atomic_boolean
    end
  end

  if Concurrent.on_jruby?

    RSpec.describe JavaAtomicBoolean do
      it_should_behave_like :atomic_boolean
    end
  end

  RSpec.describe AtomicBoolean do
    if RUBY_ENGINE != 'ruby'
      it 'does not load the C extension' do
        expect(defined?(Concurrent::CAtomicBoolean)).to be_falsey
      end
    end

    if Concurrent.on_jruby?
      it 'inherits from JavaAtomicBoolean' do
        expect(AtomicBoolean.ancestors).to include(JavaAtomicBoolean)
      end
    elsif defined? Concurrent::CAtomicBoolean
      it 'inherits from CAtomicBoolean' do
        expect(AtomicBoolean.ancestors).to include(CAtomicBoolean)
      end
    else
      it 'inherits from MutexAtomicBoolean' do
        expect(AtomicBoolean.ancestors).to include(MutexAtomicBoolean)
      end
    end

    describe '#to_s and #inspect' do
      it 'includes the value' do
        subject = described_class.new(true)
        expect(subject.to_s).to include('true')
        expect(subject.inspect).to include('true')
      end
    end
  end
end