From d6ff3ea5d22265358a51bd4350bbb2ad2a1f3f98 Mon Sep 17 00:00:00 2001 From: Arturo Pie Date: Thu, 2 Jun 2011 15:02:58 -0400 Subject: [PATCH] Some fixes to the tests and removed some commented-out code --- lib/hydra/master.rb | 2 -- lib/hydra/worker.rb | 1 - test/master_test.rb | 46 +++++++++++++++++++++++++-------------------- test/runner_test.rb | 20 +++----------------- test/test_helper.rb | 4 +++- 5 files changed, 32 insertions(+), 41 deletions(-) diff --git a/lib/hydra/master.rb b/lib/hydra/master.rb index 0296e99..8fb0903 100644 --- a/lib/hydra/master.rb +++ b/lib/hydra/master.rb @@ -173,8 +173,6 @@ module Hydra #:nodoc: def boot_ssh_worker(worker) sync = Sync.new(worker, @sync, @verbose) -# @environment+=" bundle exec" #used for manually testing - runners = worker.fetch('runners') { raise "You must specify the number of runners" } command = worker.fetch('command') { "RAILS_ENV=#{@environment} ruby -e \"require 'rubygems'; require 'hydra'; Hydra::Worker.new(:io => Hydra::Stdio.new, :runners => #{runners}, :verbose => #{@verbose}, :runner_listeners => \'#{@string_runner_event_listeners}\' );\"" diff --git a/lib/hydra/worker.rb b/lib/hydra/worker.rb index 7b3c5a5..01350cd 100644 --- a/lib/hydra/worker.rb +++ b/lib/hydra/worker.rb @@ -130,7 +130,6 @@ module Hydra #:nodoc: rescue IOError => ex trace "Worker lost Master" shutdown - #Thread.exit end end end diff --git a/test/master_test.rb b/test/master_test.rb index d4be7c4..2d9cc21 100644 --- a/test/master_test.rb +++ b/test/master_test.rb @@ -201,9 +201,8 @@ class MasterTest < Test::Unit::TestCase end context "running a local worker" do - setup do - capture_stderr do # redirect stderr - @pid = Process.fork do + should "run runner_end on successful termination" do + @pid = Process.fork do Hydra::Master.new( :files => [test_file] * 6, :autosort => false, @@ -212,27 +211,37 @@ class MasterTest < Test::Unit::TestCase :verbose => false ) end - end - end - - should "run runner_end on successful termination" do Process.waitpid @pid - assert File.exists?( target_file ) - - wait_for_file_for_a_while alternate_target_file, 2 - assert File.exists? alternate_target_file + assert_file_exists alternate_target_file end should "run runner_end after interruption signal" do + + class << @master_listener + def worker_begin( worker ) + super + sleep 1 while true #ensure the process doesn't finish before killing it + end + end + + capture_stderr do # redirect stderr + @pid = Process.fork do + Hydra::Master.new( + :files => [test_file], + :autosort => false, + :listeners => [@master_listener], + :runner_listeners => [@runner_listener], + :verbose => false + ) + end + end wait_for_runner_to_begin Process.kill 'SIGINT', @pid - Process.waitpid @pid - wait_for_file_for_a_while alternate_target_file, 2 - assert File.exists? alternate_target_file # runner_end should create this file + assert_file_exists alternate_target_file end end @@ -242,7 +251,7 @@ class MasterTest < Test::Unit::TestCase capture_stderr do # redirect stderr @pid = Process.fork do Hydra::Master.new( - :files => [test_file] * 6, + :files => [test_file], :autosort => false, :listeners => [@master_listener], :runner_listeners => [@runner_listener], @@ -265,9 +274,7 @@ class MasterTest < Test::Unit::TestCase should "run runner_end on successful termination" do Process.waitpid @pid - wait_for_file_for_a_while alternate_target_file, 2 - assert File.exists? target_file - assert File.exists? alternate_target_file + assert_file_exists alternate_target_file end end end @@ -277,8 +284,7 @@ class MasterTest < Test::Unit::TestCase def wait_for_runner_to_begin FileUtils.rm_f(@worker_began_flag) - wait_for_file_for_a_while @worker_began_flag, 2 - assert File.exists?( @worker_began_flag ), "The worker didn't begin!!" + assert_file_exists @worker_began_flag end # with a protection to avoid erasing something important in lib diff --git a/test/runner_test.rb b/test/runner_test.rb index 80eb796..3b04189 100644 --- a/test/runner_test.rb +++ b/test/runner_test.rb @@ -137,17 +137,14 @@ class RunnerTest < Test::Unit::TestCase Process.wait(@parent) # ensure runner_begin was fired - assert File.exists?( alternate_target_file ) + assert_file_exists alternate_target_file end - should "fire runner_end event after successful shutting down" do + should "fire runner_end event" do run_the_runner(@pipe, [HydraExtension::RunnerListener::RunnerEndTest.new] ) Process.wait(@parent) - wait_for_file_for_a_while alternate_target_file, 2 - - # ensure runner_end was fired - assert File.exists?( alternate_target_file ) + assert_file_exists alternate_target_file end end @@ -171,17 +168,6 @@ class RunnerTest < Test::Unit::TestCase end module RunnerTestHelper - - #this method allow us to wait for a file for a maximum number of time, so the - #test can pass in slower machines. This helps to speed up the tests - def wait_for_file_for_a_while file, time_to_wait - time_begin = Time.now - - until Time.now - time_begin >= time_to_wait or File.exists?( file ) do - sleep 0.01 - end - end - def request_a_file_and_verify_completion(pipe, file) pipe.identify_as_parent diff --git a/test/test_helper.rb b/test/test_helper.rb index ca7383c..a05954c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -80,12 +80,14 @@ class Test::Unit::TestCase #this method allow us to wait for a file for a maximum number of time, so the #test can pass in slower machines. This helps to speed up the tests - def wait_for_file_for_a_while file, time_to_wait + def assert_file_exists file, time_to_wait = 2 time_begin = Time.now until Time.now - time_begin >= time_to_wait or File.exists?( file ) do sleep 0.01 end + + assert File.exists?( file ) end end