From 4dda757c172e89a494057a464974f05211c78376 Mon Sep 17 00:00:00 2001 From: Jim Weirich Date: Wed, 27 May 2009 09:34:09 -0400 Subject: [PATCH] Fixed race condition in tests. --- Rakefile | 4 +++- test/lib/multitask_test.rb | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Rakefile b/Rakefile index f78863bec..1df5bc62a 100644 --- a/Rakefile +++ b/Rakefile @@ -102,7 +102,9 @@ begin '--sort coverage' ] + FileList['rakelib/*.rake'].pathmap("-x%p") 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.verbose = true diff --git a/test/lib/multitask_test.rb b/test/lib/multitask_test.rb index ee9be773c..04d9ee31e 100644 --- a/test/lib/multitask_test.rb +++ b/test/lib/multitask_test.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require 'test/unit' +require 'thread' require 'rake' ###################################################################### @@ -10,11 +11,18 @@ class TestMultiTask < Test::Unit::TestCase def setup Task.clear @runs = Array.new + @mutex = Mutex.new + end + + def add_run(obj) + @mutex.synchronize do + @runs << obj + end end def test_running_multitasks - task :a do 3.times do |i| @runs << "A#{i}"; sleep 0.01; end end - task :b do 3.times do |i| @runs << "B#{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| add_run("B#{i}"); sleep 0.01; end end multitask :both => [:a, :b] Task[:both].invoke assert_equal 6, @runs.size @@ -25,9 +33,9 @@ def test_running_multitasks end def test_all_multitasks_wait_on_slow_prerequisites - task :slow do 3.times do |i| @runs << "S#{i}"; sleep 0.05 end end - task :a => [:slow] do 3.times do |i| @runs << "A#{i}"; sleep 0.01 end end - task :b => [:slow] do 3.times do |i| @runs << "B#{i}"; sleep 0.01 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| add_run("A#{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] Task[:both].invoke assert_equal 9, @runs.size