Skip to content

Commit

Permalink
Poll for expected output.
Browse files Browse the repository at this point in the history
This was the only way I could get this spec consistently working on both MRI and JRuby.
  • Loading branch information
jarib committed Oct 17, 2012
1 parent 6125e50 commit ec712b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
30 changes: 11 additions & 19 deletions spec/io_spec.rb
Expand Up @@ -57,15 +57,15 @@
it "pumps all output" do
10.times do |i|
process = echo

out = Tempfile.new("duplex")

begin
process.io.stdout = out

process.start
process.poll_for_exit(exit_timeout)

out.rewind
out.read.should == "hello\n"
ensure
Expand Down Expand Up @@ -112,34 +112,26 @@

process.start
process.io.stdin.puts "hello"
sleep 0.1
out_receiver.read.should == "hello\n"

wait_until { rewind_and_read(out_receiver).should == "hello\n" }

process.io.stdin.putc "n"
sleep 0.1
out_receiver.read.should == "n"
wait_until { rewind_and_read(out_receiver).should == "hello\nn" }

process.io.stdin.print "e"
sleep 0.1
out_receiver.read.should == "e"
wait_until { rewind_and_read(out_receiver).should == "hello\nne" }

process.io.stdin.printf "w"
sleep 0.1
out_receiver.read.should == "w"
wait_until { rewind_and_read(out_receiver).should == "hello\nnew" }

process.io.stdin.write "\nworld\n"
sleep 0.1
out_receiver.read.should == "\nworld\n"
wait_until { rewind_and_read(out_receiver).should == "hello\nnew\nworld\n" }

process.io.stdin.write_nonblock "The end\n"
sleep 0.1
out_receiver.read.should == "The end\n"
wait_until { rewind_and_read(out_receiver).should == "hello\nnew\nworld\nThe end\n" }

process.io.stdin.close
process.poll_for_exit(exit_timeout)

out_receiver.rewind
out_receiver.read.should == "hello\nnew\nworld\nThe end\n"
ensure
out_receiver.close
out.close
Expand Down
18 changes: 16 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -149,13 +149,22 @@ def with_tmpdir(&blk)

def wait_until(timeout = 10, &blk)
end_time = Time.now + timeout
last_exception = nil

until Time.now >= end_time
return if yield
begin
return if yield
rescue RSpec::Expectations::ExpectationNotMetError => ex
last_exception = ex
end

sleep 0.05
end

raise "timed out"
msg = "timed out after #{timeout} seconds"
msg << ":\n#{last_exception.message}" if last_exception

raise msg
end

def can_bind?(host, port)
Expand All @@ -165,6 +174,11 @@ def can_bind?(host, port)
false
end

def rewind_and_read(io)
io.rewind
io.read
end

end # ChildProcessSpecHelper

Thread.abort_on_exception = true
Expand Down

0 comments on commit ec712b1

Please sign in to comment.