Commit 643c6ac2 authored by anonym's avatar anonym
Browse files

Test suite: DRY refactoring.

parent b29c07b9
......@@ -47,28 +47,9 @@ class Display
def screenshot(target)
FileUtils.rm_f(target)
p = IO.popen(['import', '-quality', '100%', '-window', 'root', target])
Process.wait(p.pid)
popen_wait(['import', '-quality', '100%', '-window', 'root', target])
assert($?.success?)
assert(File.exists?(target))
ensure
# If this method is run inside try_for() we might abort anywhere
# in the above code, possibly leaving defunct process around, so
# let's ensure such messes are cleaned up.
begin
begin
Process.kill("KILL", p.pid)
rescue IOError, Errno::ESRCH
# Process has already exited.
end
begin
Process.wait(p.pid)
rescue Errno::ECHILD
# Process has already exited.
end
rescue NameError
# We aborted before p was assigned, so no clean up needed.
end
end
end
......@@ -395,3 +395,32 @@ def defunct_processes
rescue Test::Unit::AssertionFailedError
return []
end
# This is IO.popen() that ensures that we wait() for the subprocess to
# finish. Please use this instead IO.popen() when running a subprocess
# inside a try_for() or other Timeout::timeout() block!
def popen_wait(*args, **opts)
p = IO.popen(*args, **opts)
Process.wait(p.pid)
return p
ensure
# If popen is run inside a Timeout::timeout() block we might abort
# while the subprocess is still running and before the above wait()
# does its clean up, which would leave a defunct process around
# unless we take care to finish wait():ing.
begin
begin
Process.wait(p.pid)
rescue Errno::ECHILD
# Process has already exited.
else
begin
Process.kill("KILL", p.pid)
rescue IOError, Errno::ESRCH
# Process has already exited.
end
end
rescue NameError
# We aborted before p was assigned, so no clean up needed.
end
end
......@@ -18,13 +18,12 @@ module OpenCV
else
env['DISPLAY'] = ENV['USER_DISPLAY']
end
p = IO.popen(
p = popen_wait(
[env, @python, "#{GIT_DIR}/features/scripts/opencv_match_template.py",
screen, image, sensitivity.to_s, show_match.to_s],
err: [:child, :out]
)
out = p.readlines.join("\n")
Process.wait(p.pid)
case $?.exitstatus
when 0
return out.chomp.split.map { |s| s.to_i }
......@@ -33,24 +32,6 @@ module OpenCV
else
raise OpenCVInternalError.new(out)
end
ensure
# If this method is run inside try_for() we might abort anywhere
# in the above code, possibly leaving defunct process around, so
# let's ensure such messes are cleaned up.
begin
begin
Process.kill("KILL", p.pid)
rescue IOError, Errno::ESRCH
# Process has already exited.
end
begin
Process.wait(p.pid)
rescue Errno::ECHILD
# Process has already exited.
end
rescue NameError
# We aborted before p was assigned, so no clean up needed.
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment