/
atomic_reference_spec.rb
235 lines (196 loc) · 7.14 KB
/
atomic_reference_spec.rb
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
RSpec.shared_examples :atomic_reference do
specify :test_construct do
atomic = described_class.new
expect(atomic.value).to be_nil
atomic = described_class.new(0)
expect(atomic.value).to eq 0
end
specify :test_value do
atomic = described_class.new(0)
atomic.value = 1
expect(atomic.value).to eq 1
end
specify :test_update do
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
atomic = described_class.new(1000)
res = atomic.update {|v| v + 1}
expect(atomic.value).to eq 1001
expect(res).to eq 1001
end
specify :test_try_update do
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
atomic = described_class.new(1000)
res = atomic.try_update {|v| v + 1}
expect(atomic.value).to eq 1001
expect(res).to eq 1001
end
specify :test_try_update_bang do
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
atomic = described_class.new(1000)
res = atomic.try_update! {|v| v + 1}
expect(atomic.value).to eq 1001
expect(res).to eq 1001
end
specify :test_swap do
atomic = described_class.new(1000)
res = atomic.swap(1001)
expect(atomic.value).to eq 1001
expect(res).to eq 1000
end
specify :test_try_update_fails do
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
atomic = described_class.new(1000)
expect(
# assigning within block exploits implementation detail for test
atomic.try_update {|v| atomic.value = 1001 ; v + 1}
).to be_falsey
end
specify :test_try_update_bang_fails do
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
atomic = described_class.new(1000)
expect {
# assigning within block exploits implementation detail for test
atomic.try_update! {|v| atomic.value = 1001 ; v + 1}
}.to raise_error Concurrent::ConcurrentUpdateError
end
specify :test_update_retries do
tries = 0
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
atomic = described_class.new(1000)
# assigning within block exploits implementation detail for test
atomic.update{|v| tries += 1 ; atomic.value = 1001 ; v + 1}
expect(tries).to eq 2
end
specify :test_numeric_cas do
atomic = described_class.new(0)
# 9-bit idempotent Fixnum (JRuby)
max_8 = 2**256 - 1
min_8 = -(2**256)
atomic.set(max_8)
max_8.upto(max_8 + 2) do |i|
expect(atomic.compare_and_swap(i, i+1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
end
atomic.set(min_8)
min_8.downto(min_8 - 2) do |i|
expect(atomic.compare_and_swap(i, i-1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
end
# 64-bit idempotent Fixnum (MRI, Rubinius)
max_64 = 2**62 - 1
min_64 = -(2**62)
atomic.set(max_64)
max_64.upto(max_64 + 2) do |i|
expect(atomic.compare_and_swap(i, i+1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
end
atomic.set(min_64)
min_64.downto(min_64 - 2) do |i|
expect(atomic.compare_and_swap(i, i-1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
end
## 64-bit overflow into Bignum (JRuby)
max_64 = 2**63 - 1
min_64 = (-2**63)
atomic.set(max_64)
max_64.upto(max_64 + 2) do |i|
expect(atomic.compare_and_swap(i, i+1)).to be_truthy, "CAS failed for numeric #{i} => #{i + 1}"
end
atomic.set(min_64)
min_64.downto(min_64 - 2) do |i|
expect(atomic.compare_and_swap(i, i-1)).to be_truthy, "CAS failed for numeric #{i} => #{i - 1}"
end
# non-idempotent Float (JRuby, Rubinius, MRI < 2.0.0 or 32-bit)
atomic.set(1.0 + 0.1)
expect(atomic.compare_and_set(1.0 + 0.1, 1.2)).to be_truthy, "CAS failed for #{1.0 + 0.1} => 1.2"
# Bignum
atomic.set(2**100)
expect(atomic.compare_and_set(2**100, 0)).to be_truthy, "CAS failed for #{2**100} => 0"
# Rational
require 'rational' unless ''.respond_to? :to_r
atomic.set(Rational(1,3))
expect(atomic.compare_and_set(Rational(1,3), 0)).to be_truthy, "CAS failed for #{Rational(1,3)} => 0"
# Complex
require 'complex' unless ''.respond_to? :to_c
atomic.set(Complex(1,2))
expect(atomic.compare_and_set(Complex(1,2), 0)).to be_truthy, "CAS failed for #{Complex(1,2)} => 0"
end
end
module Concurrent
RSpec.describe AtomicReference do
it_should_behave_like :atomic_reference
describe '#to_s and #inspect' do
it 'includes the value' do
subject = described_class.new('kajhsd')
expect(subject.to_s).to include('kajhsd')
expect(subject.inspect).to include('kajhsd')
end
end
end
RSpec.describe MutexAtomicReference do
it_should_behave_like :atomic_reference
end
if defined? Concurrent::CAtomicReference
RSpec.describe CAtomicReference, ext: true do
it_should_behave_like :atomic_reference
end
elsif defined? Concurrent::JavaAtomicReference
RSpec.describe JavaAtomicReference do
it_should_behave_like :atomic_reference
end
elsif defined? Concurrent::RbxAtomicReference
RSpec.describe RbxAtomicReference do
it_should_behave_like :atomic_reference
end
end
RSpec.describe AtomicReference do
if Concurrent.on_jruby?
it 'inherits from JavaAtomicReference' do
expect(AtomicReference.ancestors).to include(Concurrent::JavaAtomicReference)
end
elsif Concurrent.allow_c_extensions?
it 'inherits from CAtomicReference' do
expect(AtomicReference.ancestors).to include(Concurrent::CAtomicReference)
end
elsif Concurrent.on_rbx?
it 'inherits from RbxAtomicReference' do
expect(AtomicReference.ancestors).to include(Concurrent::RbxAtomicReference)
end
else
it 'inherits from MutexAtomicReference' do
expect(AtomicReference.ancestors).to include(Concurrent::MutexAtomicReference)
end
end
end
RSpec.describe MutexAtomicReference do
it_should_behave_like :atomic_reference
end
if defined? Concurrent::CAtomicReference
RSpec.describe CAtomicReference, ext: true do
it_should_behave_like :atomic_reference
end
elsif defined? Concurrent::JavaAtomicReference
RSpec.describe JavaAtomicReference do
it_should_behave_like :atomic_reference
end
elsif defined? Concurrent::RbxAtomicReference
RSpec.describe RbxAtomicReference do
it_should_behave_like :atomic_reference
end
end
RSpec.describe AtomicReference do
if Concurrent.on_jruby?
it 'inherits from JavaAtomicReference' do
expect(described_class.ancestors).to include(Concurrent::JavaAtomicReference)
end
elsif Concurrent.allow_c_extensions?
it 'inherits from CAtomicReference', ext: true do
expect(described_class.ancestors).to include(Concurrent::CAtomicReference)
end
elsif Concurrent.on_rbx?
it 'inherits from RbxAtomicReference' do
expect(described_class.ancestors).to include(Concurrent::RbxAtomicReference)
end
else
it 'inherits from MutexAtomicReference' do
expect(described_class.ancestors).to include(Concurrent::MutexAtomicReference)
end
end
end
end