From 52a34f19b8b6a3e556de5eb77d91d7af3676309d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 12 May 2015 14:54:29 -0700 Subject: fixes the timing on the chef-shell specs also makes timing errors hard errors --- spec/functional/shell_spec.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/spec/functional/shell_spec.rb b/spec/functional/shell_spec.rb index fa9de77b0e..122d7a698c 100644 --- a/spec/functional/shell_spec.rb +++ b/spec/functional/shell_spec.rb @@ -29,6 +29,8 @@ describe Shell do describe "smoke tests", :unix_only => true do include Chef::Mixin::Command::Unix + TIMEOUT=30 + def read_until(io, expected_value) start = Time.new buffer = "" @@ -38,15 +40,30 @@ describe Shell do rescue Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EIO, EOFError sleep 0.01 end - if Time.new - start > 30 - STDERR.puts "did not read expected value `#{expected_value}' within 15s" - STDERR.puts "Buffer so far: `#{buffer}'" - break + if Time.new - start > TIMEOUT + raise "did not read expected value `#{expected_value}' within #{TIMEOUT}s\n" + + "Buffer so far: `#{buffer}'" end end buffer end + def flush_output(io) + start = Time.new + loop do + begin + io.read_nonblock(1) + rescue Errno::EWOULDBLOCK, Errno::EAGAIN + sleep 0.01 + rescue EOFError, Errno::EIO + break + end + if Time.new - start > TIMEOUT + raise "timed out after #{TIMEOUT}s waiting for output to end" + end + end + end + def wait_or_die(pid) start = Time.new @@ -67,12 +84,12 @@ describe Shell do path_to_chef_shell = File.expand_path("../../../bin/chef-shell", __FILE__) output = '' status = popen4("#{path_to_chef_shell} -c #{config} #{options}", :waitlast => true) do |pid, stdin, stdout, stderr| - read_until(stdout, "chef >") + read_until(stdout, "chef (#{Chef::VERSION})>") yield stdout, stdin if block_given? stdin.write("'done'\n") output = read_until(stdout, '=> "done"') stdin.print("exit\n") - read_until(stdout, "\n") + flush_output(stdout) end [output, status.exitstatus] @@ -84,14 +101,12 @@ describe Shell do config = File.expand_path("shef-config.rb", CHEF_SPEC_DATA) path_to_chef_shell = File.expand_path("../../../bin/chef-shell", __FILE__) reader, writer, pid = PTY.spawn("#{path_to_chef_shell} -c #{config} #{options}") - read_until(reader, "chef >") + read_until(reader, "chef (#{Chef::VERSION})>") yield reader, writer if block_given? writer.puts('"done"') output = read_until(reader, '=> "done"') writer.print("exit\n") - read_until(reader, "exit") - read_until(reader, "\n") - read_until(reader, "\n") + flush_output(reader) writer.close exitstatus = wait_or_die(pid) -- cgit v1.2.1