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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
RSpec.shared_examples 'exchanger method with indefinite timeout' do
before(:each) do
subject # ensure proper initialization
end
it 'blocks indefinitely' do
latch_1 = Concurrent::CountDownLatch.new
latch_2 = Concurrent::CountDownLatch.new
t = in_thread do
latch_1.count_down
subject.send(method_name, 0.1)
latch_2.count_down
end
is_sleeping t
expect(latch_1.count).to eq 0
expect(latch_2.count).to eq 1
end
it 'receives the other value' do
first_value = nil
second_value = nil
latch = Concurrent::CountDownLatch.new(2)
in_thread { first_value = subject.send(method_name, 2); latch.count_down }
in_thread { second_value = subject.send(method_name, 4); latch.count_down }
latch.wait(1)
expect(get_value(first_value)).to eq 4
expect(get_value(second_value)).to eq 2
end
it 'can be reused' do
first_value = nil
second_value = nil
latch_1 = Concurrent::CountDownLatch.new(2)
latch_2 = Concurrent::CountDownLatch.new(2)
in_thread { first_value = subject.send(method_name, 1); latch_1.count_down }
in_thread { second_value = subject.send(method_name, 0); latch_1.count_down }
latch_1.wait(1)
in_thread { first_value = subject.send(method_name, 10); latch_2.count_down }
in_thread { second_value = subject.send(method_name, 12); latch_2.count_down }
latch_2.wait(1)
expect(get_value(first_value)).to eq 12
expect(get_value(second_value)).to eq 10
end
end
RSpec.shared_examples 'exchanger method with finite timeout' do
it 'blocks until timeout' do
duration = Concurrent::TestHelpers.monotonic_interval do
begin
subject.send(method_name, 2, 0.1)
rescue Concurrent::TimeoutError
# do nothing
end
end
expect(duration).to be_within(0.05).of(0.1)
end
it 'receives the other value' do
first_value = nil
second_value = nil
latch = Concurrent::CountDownLatch.new(2)
in_thread { first_value = subject.send(method_name, 2, 1); latch.count_down }
in_thread { second_value = subject.send(method_name, 4, 1); latch.count_down }
latch.wait(1)
expect(get_value(first_value)).to eq 4
expect(get_value(second_value)).to eq 2
end
it 'can be reused' do
first_value = nil
second_value = nil
latch_1 = Concurrent::CountDownLatch.new(2)
latch_2 = Concurrent::CountDownLatch.new(2)
in_thread { first_value = subject.send(method_name, 1, 1); latch_1.count_down }
in_thread { second_value = subject.send(method_name, 0, 1); latch_1.count_down }
latch_1.wait(1)
in_thread { first_value = subject.send(method_name, 10, 1); latch_2.count_down }
in_thread { second_value = subject.send(method_name, 12, 1); latch_2.count_down }
latch_2.wait(1)
expect(get_value(first_value)).to eq 12
expect(get_value(second_value)).to eq 10
end
end
RSpec.shared_examples 'exchanger method cross-thread interactions' do
it 'when first, waits for a second' do
first_value = nil
second_value = nil
latch = Concurrent::CountDownLatch.new(1)
t1 = in_thread do
first_value = subject.send(method_name, :foo, 1)
latch.count_down
end
t1.join(0.1)
second_value = subject.send(method_name, :bar, 0)
latch.wait(1)
expect(get_value(first_value)).to eq :bar
expect(get_value(second_value)).to eq :foo
end
it 'allows multiple firsts to cancel if necessary', notravis: true do
first_value = nil
second_value = nil
cancels = 3
cancel_latch = Concurrent::CountDownLatch.new(cancels)
success_latch = Concurrent::CountDownLatch.new(1)
threads = cancels.times.collect do
in_thread do
begin
first_value = subject.send(method_name, :foo, 0.1)
rescue Concurrent::TimeoutError
# suppress
ensure
cancel_latch.count_down
end
end
end
threads.each { |t| t.join(1) }
cancel_latch.wait(1)
t1 = in_thread do
first_value = subject.send(method_name, :bar, 1)
success_latch.count_down
end
t1.join(0.1)
second_value = subject.send(method_name, :baz, 0)
success_latch.wait(1)
expect(get_value(first_value)).to eq :baz
expect(get_value(second_value)).to eq :bar
end
end
RSpec.shared_examples :exchanger do
context '#exchange' do
let!(:method_name) { :exchange }
def get_value(result)
result
end
it_behaves_like 'exchanger method with indefinite timeout'
it_behaves_like 'exchanger method with finite timeout'
it_behaves_like 'exchanger method cross-thread interactions'
end
context '#exchange!' do
let!(:method_name) { :exchange! }
def get_value(result)
result
end
it_behaves_like 'exchanger method with indefinite timeout'
it_behaves_like 'exchanger method with finite timeout'
it_behaves_like 'exchanger method cross-thread interactions'
end
context '#try_exchange' do
let!(:method_name) { :try_exchange }
def get_value(result)
result.value
end
it_behaves_like 'exchanger method with indefinite timeout'
it_behaves_like 'exchanger method with finite timeout'
it_behaves_like 'exchanger method cross-thread interactions'
end
end
module Concurrent
RSpec.describe RubyExchanger do
it_behaves_like :exchanger
if Concurrent.on_cruby?
specify 'stress test', notravis: true do
thread_count = 100
exchange_count = 100
latch = Concurrent::CountDownLatch.new(thread_count)
good = Concurrent::AtomicFixnum.new(0)
bad = Concurrent::AtomicFixnum.new(0)
ugly = Concurrent::AtomicFixnum.new(0)
thread_count.times.collect do |i|
in_thread do
exchange_count.times do |j|
begin
result = subject.exchange!(i, 1)
result == i ? ugly.up : good.up
rescue Concurrent::TimeoutError
bad.up
end
end
latch.count_down
end
end
latch.wait
puts "Good: #{good.value}, Bad (timeout): #{bad.value}, Ugly: #{ugly.value}"
expect(good.value + bad.value + ugly.value).to eq thread_count * exchange_count
expect(ugly.value).to eq 0
end
end
end
if defined? JavaExchanger
RSpec.describe JavaExchanger do
it_behaves_like :exchanger
end
end
RSpec.describe Exchanger do
context 'class hierarchy' do
if Concurrent.on_jruby?
it 'inherits from JavaExchanger' do
expect(Exchanger.ancestors).to include(JavaExchanger)
end
else
it 'inherits from RubyExchanger' do
expect(Exchanger.ancestors).to include(RubyExchanger)
end
end
end
end
end
|