Skip to content

Instantly share code, notes, and snippets.

@jarib
Created October 17, 2012 10:45
Show Gist options
  • Save jarib/3904922 to your computer and use it in GitHub Desktop.
Save jarib/3904922 to your computer and use it in GitHub Desktop.
diff --git a/spec/io_spec.rb b/spec/io_spec.rb
index 2e36bc4..339096c 100644
--- a/spec/io_spec.rb
+++ b/spec/io_spec.rb
@@ -57,15 +57,15 @@ describe ChildProcess do
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
@@ -112,34 +112,26 @@ describe ChildProcess do
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
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a81c329..e798995 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -149,13 +149,22 @@ module ChildProcessSpecHelper
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)
@@ -165,6 +174,11 @@ module ChildProcessSpecHelper
false
end
+ def rewind_and_read(io)
+ io.rewind
+ io.read
+ end
+
end # ChildProcessSpecHelper
Thread.abort_on_exception = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment