Skip to content

Commit

Permalink
Merge pull request #40 from softace/softace/interactive_streams
Browse files Browse the repository at this point in the history
Making the streams interactive for jruby
  • Loading branch information
jarib committed Oct 17, 2012
2 parents 1eec9dd + 98f38de commit 985b770
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/childprocess/jruby/process.rb
Expand Up @@ -90,6 +90,10 @@ def setup_io
if duplex?
stdin = @process.getOutputStream.to_io
stdin.sync = true
stdin.instance_variable_set(:@java_stream, @process.getOutputStream)
def stdin.__flushit; @java_stream.flush; end #The stream provided is a BufferedeOutputStream, so we have to flush it to make the bytes flow to the process
def stdin.flush; super; self.__flushit; end
def stdin.puts(*args); super(*args); self.__flushit; end

io._stdin = stdin
else
Expand Down
34 changes: 34 additions & 0 deletions spec/io_spec.rb
Expand Up @@ -98,6 +98,40 @@
end
end

it "can write to stdin interactively if duplex = true" do
process = cat

out = Tempfile.new("duplex")
out.sync = true

out_receiver = File.open(out.path, "rb")
begin
process.io.stdout = out
process.io.stderr = out
process.duplex = true

process.start
process.io.stdin.puts "hello"
sleep 0.1
out_receiver.read.should == "hello\n"
process.io.stdin.puts "new"
sleep 0.1
out_receiver.read.should == "new\n"
process.io.stdin.write "world\n"
process.io.stdin.flush
sleep 0.1
out_receiver.read.should == "world\n"
process.io.stdin.close
process.poll_for_exit(exit_timeout)

out_receiver.rewind
out_receiver.read.should == "hello\nnew\nworld\n"
ensure
out_receiver.close
out.close
end
end

#
# this works on JRuby 1.6.5 on my Mac, but for some reason
# hangs on Travis (running 1.6.5.1 + OpenJDK).
Expand Down

0 comments on commit 985b770

Please sign in to comment.