Skip to content
Browse files

Read from a pipe, then wait. Not the other way around.

  • Loading branch information...
1 parent 4befe5b commit 69d6b6ca09732d327e0931ca6c52ab94201d18c0 @jyurek jyurek committed
View
5 lib/cocaine/command_line/runners/posix_runner.rb
@@ -6,9 +6,10 @@ class PosixRunner
def call(command, env = {})
input, output = IO.pipe
pid = spawn(env, command, :out => output)
- waitpid(pid)
output.close
- input.read
+ result = input.read
+ waitpid(pid)
+ result
end
private
View
5 lib/cocaine/command_line/runners/process_runner.rb
@@ -6,9 +6,10 @@ class ProcessRunner
def call(command, env = {})
input, output = IO.pipe
pid = spawn(env, command, :out => output)
- waitpid(pid)
output.close
- input.read
+ result = input.read
+ waitpid(pid)
+ result
end
private
View
2 spec/cocaine/command_line/runners/backticks_runner_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe Cocaine::CommandLine::BackticksRunner do
+ it_behaves_like 'a command that does not block'
+
it 'runs the command given' do
subject.call("echo hello").should == "hello\n"
end
View
2 spec/cocaine/command_line/runners/posix_runner_spec.rb
@@ -2,6 +2,8 @@
describe Cocaine::CommandLine::PosixRunner do
if Cocaine::CommandLine::posix_spawn_available?
+ it_behaves_like 'a command that does not block'
+
it 'runs the command given' do
subject.call("echo hello").should == "hello\n"
end
View
2 spec/cocaine/command_line/runners/process_runner_spec.rb
@@ -2,6 +2,8 @@
describe Cocaine::CommandLine::ProcessRunner do
if Process.respond_to?(:spawn)
+ it_behaves_like "a command that does not block"
+
it 'runs the command given' do
subject.call("echo hello").should == "hello\n"
end
View
6 spec/spec_helper.rb
@@ -1,9 +1,11 @@
require 'rspec'
-require './spec/support/with_exitstatus'
-require './spec/support/stub_os'
require 'mocha'
require 'bourne'
require 'cocaine'
+require 'timeout'
+require 'tempfile'
+
+Dir[File.dirname(__FILE__) + "/support/**.rb"].each{|support_file| require support_file }
RSpec.configure do |config|
config.mock_with :mocha
View
12 spec/support/nonblocking_examples.rb
@@ -0,0 +1,12 @@
+shared_examples_for "a command that does not block" do
+ it 'does not block if the command outputs a lot of data' do
+ garbage_file = Tempfile.new("garbage")
+ 10.times{ garbage_file.write("A" * 1024 * 1024) }
+
+ Timeout::timeout(5) do
+ output = subject.call("cat '#{garbage_file.path}'")
+ $?.exitstatus.should == 0
+ output.length.should == 10 * 1024 * 1024
+ end
+ end
+end

0 comments on commit 69d6b6c

Please sign in to comment.
Something went wrong with that request. Please try again.