flipped worker test to test in foreground and background. more accurate rcov results

This commit is contained in:
Nick Gauthier 2010-01-28 16:52:11 -05:00
parent 90b04216aa
commit 9a38b0853b
2 changed files with 48 additions and 25 deletions

View File

@ -18,23 +18,23 @@ class RunnerTest < Test::Unit::TestCase
# flip it around to the parent is in the fork, this gives # flip it around to the parent is in the fork, this gives
# us more direct control over the runner and proper test # us more direct control over the runner and proper test
# coverage output # coverage output
@pipe = Hydra::Pipe.new pipe = Hydra::Pipe.new
@parent = Process.fork do parent = Process.fork do
request_a_file_and_verify_completion(@pipe) request_a_file_and_verify_completion(pipe)
end end
run_the_runner(@pipe) run_the_runner(pipe)
Process.wait(@parent) Process.wait(parent)
end end
# this flips the above test, so that the main process runs a bit of the parent # this flips the above test, so that the main process runs a bit of the parent
# code, but only with minimal assertion # code, but only with minimal assertion
should "be able to tell a runner to run a test" do should "be able to tell a runner to run a test" do
@pipe = Hydra::Pipe.new pipe = Hydra::Pipe.new
@child = Process.fork do child = Process.fork do
run_the_runner(@pipe) run_the_runner(pipe)
end end
request_a_file_and_verify_completion(@pipe) request_a_file_and_verify_completion(pipe)
Process.wait(@child) Process.wait(child)
end end
end end

View File

@ -13,30 +13,53 @@ class WorkerTest < Test::Unit::TestCase
FileUtils.rm_f(TARGET) FileUtils.rm_f(TARGET)
end end
# run the worker in the foreground and the requests in the background
should "run a test" do should "run a test" do
num_runners = 4 num_runners = 4
@pipe = Hydra::Pipe.new pipe = Hydra::Pipe.new
@child = Process.fork do child = Process.fork do
@pipe.identify_as_child request_a_file_and_verify_completion(pipe, num_runners)
Hydra::Worker.new(@pipe, num_runners) pipe.close
@pipe.close
end end
@pipe.identify_as_parent run_the_worker(pipe, num_runners)
num_runners.times do Process.wait(child)
assert @pipe.gets.is_a?(Hydra::Messages::Worker::RequestFile) end
end
@pipe.write(Hydra::Messages::Worker::RunFile.new(:file => TESTFILE))
response = @pipe.gets # inverse of the above test to run the worker in the background
should "be able to tell a worker to run a test" do
num_runners = 4
pipe = Hydra::Pipe.new
child = Process.fork do
run_the_worker(pipe, num_runners)
end
request_a_file_and_verify_completion(pipe, num_runners)
Process.wait(child)
pipe.close
end
end
module WorkerTestHelper
def run_the_worker(pipe, num_runners)
pipe.identify_as_child
Hydra::Worker.new(pipe, num_runners)
pipe.close
end
def request_a_file_and_verify_completion(pipe, num_runners)
pipe.identify_as_parent
num_runners.times do
assert pipe.gets.is_a?(Hydra::Messages::Worker::RequestFile)
end
pipe.write(Hydra::Messages::Worker::RunFile.new(:file => TESTFILE))
response = pipe.gets
assert response.is_a?(Hydra::Messages::Worker::Results) assert response.is_a?(Hydra::Messages::Worker::Results)
@pipe.write(Hydra::Messages::Worker::Shutdown.new) pipe.write(Hydra::Messages::Worker::Shutdown.new)
assert File.exists?(TARGET) assert File.exists?(TARGET)
assert_equal "HYDRA", File.read(TARGET) assert_equal "HYDRA", File.read(TARGET)
Process.wait(@child)
@pipe.close
end end
end end
include WorkerTestHelper
end end