Skip to content

Commit

Permalink
ensure to kill all processes always just after execution
Browse files Browse the repository at this point in the history
  • Loading branch information
tagomoris committed Dec 9, 2016
1 parent fcda956 commit e2ee863
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions test/command/test_fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit e2ee863

Please sign in to comment.