From e2ee8634fd9db70050039fbcfeb191abd911542f Mon Sep 17 00:00:00 2001 From: TAGOMORI Satoshi Date: Fri, 9 Dec 2016 12:13:09 +0900 Subject: [PATCH] ensure to kill all processes always just after execution --- test/command/test_fluentd.rb | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/test/command/test_fluentd.rb b/test/command/test_fluentd.rb index a19aaae141..b8724be77c 100644 --- a/test/command/test_fluentd.rb +++ b/test/command/test_fluentd.rb @@ -12,20 +12,9 @@ class TestFluentdCommand < ::Test::Unit::TestCase setup do FileUtils.rm_rf(TMP_DIR) FileUtils.mkdir_p(TMP_DIR) - @pid = nil @worker_pids = [] end - teardown do - if @pid - Process.kill(:KILL, @pid) rescue nil - @worker_pids.each do |pid| - Process.kill(:KILL, pid) rescue nil - end - Timeout.timeout(10){ sleep 0.1 while process_exist?(@pid) } - end - end - def process_exist?(pid) begin r = Process.waitpid(pid, Process::WNOHANG) @@ -65,8 +54,16 @@ def execute_command(cmdline, chdir=TMP_DIR) "BUNDLE_GEMFILE" => gemfile_path, } IO.popen(env, cmdline, chdir: chdir, err: [:child, :out]) do |io| - yield io.pid, io - Process.kill(:KILL, io.pid) rescue nil + pid = io.pid + begin + yield io.pid, io + ensure + Process.kill(:KILL, pid) rescue nil + @worker_pids.each do |cpid| + Process.kill(:KILL, cpid) rescue nil + end + Timeout.timeout(10){ sleep 0.1 while process_exist?(pid) } + end end end @@ -76,9 +73,8 @@ def assert_log_matches(cmdline, *pattern_list, timeout: 10) stdio_buf = "" begin execute_command(cmdline) do |pid, stdout| - @pid = pid waiting(timeout) do - while process_exist?(@pid) && !matched + while process_exist?(pid) && !matched readables, _, _ = IO.select([stdout], nil, nil, 1) next unless readables buf = readables.first.readpartial(1024) @@ -109,9 +105,8 @@ def assert_fluentd_fails_to_start(cmdline, *pattern_list, timeout: 10) stdio_buf = "" begin execute_command(cmdline) do |pid, stdout| - @pid = pid waiting(timeout) do - while process_exist?(@pid) && !running + while process_exist?(pid) && !running readables, _, _ = IO.select([stdout], nil, nil, 1) next unless readables next if readables.first.eof?