Skip to content

Instantly share code, notes, and snippets.

@jarib
Created October 17, 2012 10:33
Show Gist options
  • Save jarib/3904878 to your computer and use it in GitHub Desktop.
Save jarib/3904878 to your computer and use it in GitHub Desktop.
diff --git a/spec/io_spec.rb b/spec/io_spec.rb
index 2e36bc4..d3c4f48 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) == "hello\n" }
process.io.stdin.putc "n"
- sleep 0.1
- out_receiver.read.should == "n"
+ wait_until { rewind_and_read(out_receiver) == "hello\nn" }
process.io.stdin.print "e"
- sleep 0.1
- out_receiver.read.should == "e"
+ wait_until { rewind_and_read(out_receiver) == "hello\nne" }
process.io.stdin.printf "w"
- sleep 0.1
- out_receiver.read.should == "w"
+ wait_until { rewind_and_read(out_receiver) == "hello\nnew" }
process.io.stdin.write "\nworld\n"
- sleep 0.1
- out_receiver.read.should == "\nworld\n"
+ wait_until { rewind_and_read(out_receiver) == "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) == "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..5ea4856 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -147,7 +147,7 @@ module ChildProcessSpecHelper
end
end
- def wait_until(timeout = 10, &blk)
+ def wait_until(timeout = 10, msg = 'timed out', &blk)
end_time = Time.now + timeout
until Time.now >= end_time
@@ -155,7 +155,7 @@ module ChildProcessSpecHelper
sleep 0.05
end
- raise "timed out"
+ raise msg
end
def can_bind?(host, port)
@@ -165,6 +165,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