From 5dd835e4cab9fab3185d6478e940a1e7b3ce8625 Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Thu, 13 Feb 2014 12:28:38 -0500 Subject: [PATCH] This fixes a stderr redirection test on fast machines. The write pipe was getting closed before the redirection thread had a chance to copy it's contents into it. --- spec/connection_spec.rb | 12 ++++++------ spec/support/matchers/include_response.rb | 24 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 spec/support/matchers/include_response.rb diff --git a/spec/connection_spec.rb b/spec/connection_spec.rb index c540d73..104dd3f 100644 --- a/spec/connection_spec.rb +++ b/spec/connection_spec.rb @@ -19,18 +19,18 @@ describe Capybara::Webkit::Connection do end it 'forwards stderr to the given IO object' do - read, write = IO.pipe - redirected_connection = Capybara::Webkit::Connection.new(:stderr => write) - script = 'console.log("hello world")' + read_io, write_io = IO.pipe + redirected_connection = Capybara::Webkit::Connection.new(:stderr => write_io) redirected_connection.puts "EnableLogging" redirected_connection.puts 0 + + script = 'console.log("hello world")' redirected_connection.puts "Execute" redirected_connection.puts 1 redirected_connection.puts script.to_s.bytesize redirected_connection.print script - sleep(0.5) - write.close - read.read.should =~ /hello world $/ + + expect(read_io).to include_response "hello world \n" end it 'does not forward stderr to nil' do diff --git a/spec/support/matchers/include_response.rb b/spec/support/matchers/include_response.rb new file mode 100644 index 0000000..59d02ba --- /dev/null +++ b/spec/support/matchers/include_response.rb @@ -0,0 +1,24 @@ +RSpec::Matchers.define :include_response do |expected_response| + read_timeout = 2 + read_bytes = 4096 + response = "" + + match do |read_io| + found_response = false + + while !found_response && IO.select([read_io], nil, nil, read_timeout) do + response += read_io.read_nonblock(read_bytes) + found_response = response.include?(expected_response) + end + + found_response + end + + failure_message_for_should do |actual| + "expected #{response} to include #{expected_response}" + end + + failure_message_for_should_not do |actual| + "expected #{response} to not include #{expected_response}" + end +end