Skip to content

Commit

Permalink
Fixed race condition in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed May 27, 2009
1 parent 6fe3f8e commit 4dda757
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Expand Up @@ -102,7 +102,9 @@ begin
'--sort coverage' '--sort coverage'
] + FileList['rakelib/*.rake'].pathmap("-x%p") ] + FileList['rakelib/*.rake'].pathmap("-x%p")
t.test_files = FileList[ t.test_files = FileList[
'test/test*.rb', 'test/functional.rb' 'test/lib/*_test.rb',
'test/contrib/*_test.rb',
'test/functional/*_test.rb'
] ]
t.output_dir = 'coverage' t.output_dir = 'coverage'
t.verbose = true t.verbose = true
Expand Down
18 changes: 13 additions & 5 deletions test/lib/multitask_test.rb
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby


require 'test/unit' require 'test/unit'
require 'thread'
require 'rake' require 'rake'


###################################################################### ######################################################################
Expand All @@ -10,11 +11,18 @@ class TestMultiTask < Test::Unit::TestCase
def setup def setup
Task.clear Task.clear
@runs = Array.new @runs = Array.new
@mutex = Mutex.new
end

def add_run(obj)
@mutex.synchronize do
@runs << obj
end
end end


def test_running_multitasks def test_running_multitasks
task :a do 3.times do |i| @runs << "A#{i}"; sleep 0.01; end end task :a do 3.times do |i| add_run("A#{i}"); sleep 0.01; end end
task :b do 3.times do |i| @runs << "B#{i}"; sleep 0.01; end end task :b do 3.times do |i| add_run("B#{i}"); sleep 0.01; end end
multitask :both => [:a, :b] multitask :both => [:a, :b]
Task[:both].invoke Task[:both].invoke
assert_equal 6, @runs.size assert_equal 6, @runs.size
Expand All @@ -25,9 +33,9 @@ def test_running_multitasks
end end


def test_all_multitasks_wait_on_slow_prerequisites def test_all_multitasks_wait_on_slow_prerequisites
task :slow do 3.times do |i| @runs << "S#{i}"; sleep 0.05 end end task :slow do 3.times do |i| add_run("S#{i}"); sleep 0.05 end end
task :a => [:slow] do 3.times do |i| @runs << "A#{i}"; sleep 0.01 end end task :a => [:slow] do 3.times do |i| add_run("A#{i}"); sleep 0.01 end end
task :b => [:slow] do 3.times do |i| @runs << "B#{i}"; sleep 0.01 end end task :b => [:slow] do 3.times do |i| add_run("B#{i}"); sleep 0.01 end end
multitask :both => [:a, :b] multitask :both => [:a, :b]
Task[:both].invoke Task[:both].invoke
assert_equal 9, @runs.size assert_equal 9, @runs.size
Expand Down

0 comments on commit 4dda757

Please sign in to comment.