Skip to content

Commit dbcd08c

Browse files
committed
Merge pull request #264 from ruby-concurrency/intermittently-failing-tests
Fix Intermittently Failing Tests
2 parents 635c748 + c39962e commit dbcd08c

File tree

8 files changed

+43
-28
lines changed

8 files changed

+43
-28
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ matrix:
2626
- rvm: jruby-head
2727
- rvm: 1.9.3
2828

29-
script: "rake compile && bundle exec rspec --color --backtrace --tag ~unfinished --seed 1 --format documentation ./spec"
29+
script: "bundle exec rake compile && bundle exec rspec --color --backtrace --tag ~unfinished --seed 1 --format documentation ./spec"

examples/actor_stress_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def run
4747
"with #{@threads} thread#{plural.call(@threads)} each " +
4848
"and #{@loops} loop#{plural.call(@loops)} per thread..."
4949

50-
Benchmark.bm do |bm|
50+
Benchmark.bmbm do |bm|
5151
@tests.times do
5252
bm.report do
5353
test(@threads, @loops)

examples/benchmark_atomic_boolean.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ def atomic_test(clazz, opts = {})
1414
latch = Concurrent::CountDownLatch.new(threads)
1515

1616
print "Testing with #{clazz}...\n"
17-
stats = Benchmark.measure do
18-
threads.times do |i|
19-
Thread.new do
20-
tests.times{ atomic.value = true }
21-
latch.count_down
17+
Benchmark.bmbm do |bm|
18+
bm.report do
19+
threads.times do |i|
20+
Thread.new do
21+
tests.times{ atomic.value = true }
22+
latch.count_down
23+
end
2224
end
25+
latch.wait
2326
end
24-
latch.wait
2527
end
26-
print stats
2728
end
2829

2930
puts "Testing with #{RbConfig::CONFIG['ruby_install_name']} #{RUBY_VERSION}"

examples/benchmark_atomic_fixnum.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ def atomic_test(clazz, opts = {})
1414
latch = Concurrent::CountDownLatch.new(threads)
1515

1616
print "Testing with #{clazz}...\n"
17-
stats = Benchmark.measure do
18-
threads.times do |i|
19-
Thread.new do
20-
tests.times{ num.up }
21-
latch.count_down
17+
Benchmark.bmbm do |bm|
18+
bm.report do
19+
threads.times do |i|
20+
Thread.new do
21+
tests.times{ num.up }
22+
latch.count_down
23+
end
2224
end
25+
latch.wait
2326
end
24-
latch.wait
2527
end
26-
print stats
2728
end
2829

2930
puts "Testing with #{RbConfig::CONFIG['ruby_install_name']} #{RUBY_VERSION}"
File renamed without changes.

spec/concurrent/exchanger_spec.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,44 @@ module Concurrent
2525
it 'should receive the other value' do
2626
first_value = nil
2727
second_value = nil
28+
latch = Concurrent::CountDownLatch.new(2)
2829

29-
thread_1 = Thread.new { first_value = subject.exchange(2) }
30-
thread_2 = Thread.new { second_value = subject.exchange(4) }
30+
threads = [
31+
Thread.new { first_value = subject.exchange(2); latch.count_down },
32+
Thread.new { second_value = subject.exchange(4); latch.count_down }
33+
]
34+
35+
latch.wait(1)
3136

32-
[thread_1, thread_2].each(&:join)
3337
expect(first_value).to eq 4
3438
expect(second_value).to eq 2
39+
40+
threads.each {|t| t.kill }
3541
end
3642

3743
it 'can be reused' do
3844
first_value = nil
3945
second_value = nil
46+
latch_1 = Concurrent::CountDownLatch.new(2)
47+
latch_2 = Concurrent::CountDownLatch.new(2)
4048

41-
thread_1 = Thread.new { first_value = subject.exchange(1) }
42-
thread_2 = Thread.new { second_value = subject.exchange(0) }
49+
threads = [
50+
Thread.new { first_value = subject.exchange(1); latch_1.count_down },
51+
Thread.new { second_value = subject.exchange(0); latch_1.count_down }
52+
]
4353

44-
[thread_1, thread_2].each(&:join)
45-
46-
thread_1 = Thread.new { first_value = subject.exchange(10) }
47-
thread_2 = Thread.new { second_value = subject.exchange(12) }
54+
latch_1.wait(1)
55+
threads.each {|t| t.kill }
4856

49-
[thread_1, thread_2].each(&:join)
57+
threads = [
58+
Thread.new { first_value = subject.exchange(10); latch_2.count_down },
59+
Thread.new { second_value = subject.exchange(12); latch_2.count_down }
60+
]
5061

62+
latch_2.wait(1)
5163
expect(first_value).to eq 12
5264
expect(second_value).to eq 10
65+
threads.each {|t| t.kill }
5366
end
5467
end
5568

spec/concurrent/scheduled_task_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def trigger_observable(observable)
9696
context 'instance #execute' do
9797

9898
it 'does nothing unless the state is :unscheduled' do
99-
expect(Thread).not_to receive(:new).with(any_args)
99+
expect(Concurrent).not_to receive(:timer).with(any_args)
100100
task = ScheduledTask.new(1){ nil }
101101
task.instance_variable_set(:@state, :pending)
102102
task.execute

yardoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 5aff4706f70d757c15830edd66d74759ff364eda
1+
Subproject commit 1d2f9299b93339d8dc4ef72eb17bb892e91bd6f5

0 commit comments

Comments
 (0)