Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Add option to discard output
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Suraci and Maria Shaldibina committed Sep 9, 2013
1 parent daf5bd2 commit 6554ce1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions em-posix-spawn/lib/em/posix/spawn/child.rb
Expand Up @@ -47,6 +47,7 @@ def initialize(*args)
@input = @options.delete(:input)
@timeout = @options.delete(:timeout)
@max = @options.delete(:max)
@discard_output = @options.delete(:discard_output)
@prepend_stdout = @options.delete(:prepend_stdout) || ""
@prepend_stderr = @options.delete(:prepend_stderr) || ""
@options.delete(:chdir) if @options[:chdir].nil?
Expand Down Expand Up @@ -220,8 +221,8 @@ def exec!

# watch fds
@cin = EM.watch stdin, WritableStream, (@input || "").dup, "stdin"
@cout = EM.watch stdout, ReadableStream, @prepend_stdout, "stdout"
@cerr = EM.watch stderr, ReadableStream, @prepend_stderr, "stderr"
@cout = EM.watch stdout, ReadableStream, @prepend_stdout, "stdout", @discard_output
@cerr = EM.watch stderr, ReadableStream, @prepend_stderr, "stderr", @discard_output

# register events
@cin.notify_writable = true
Expand Down Expand Up @@ -401,8 +402,9 @@ def slice_from_buffer(buffer)
# Maximum buffer size for reading
BUFSIZE = (64 * 1024)

def initialize(buffer, name, &block)
def initialize(buffer, name, discard_output = false, &block)
super(buffer, name, &block)
@discard_output = discard_output
@after_read = []
end

Expand Down Expand Up @@ -449,7 +451,8 @@ def notify_readable
return if closed?

begin
@buffer << @io.read_nonblock(BUFSIZE)
out = @io.read_nonblock(BUFSIZE)
@buffer << out unless @discard_output
@after_read.each { |listener| listener.call(@buffer) }
rescue Errno::EAGAIN, Errno::EINTR
rescue EOFError
Expand Down
11 changes: 11 additions & 0 deletions em-posix-spawn/test/test_child.rb
Expand Up @@ -131,6 +131,17 @@ def test_max
end
end

def test_discard_output
em do
p = Child.new('echo hi', :discard_output => true)
p.callback do
assert_equal 0, p.out.size
assert_equal 0, p.err.size
done
end
end
end

def test_max_with_child_hierarchy
em do
p = Child.new('/bin/sh', '-c', 'yes', :max => 100_000)
Expand Down

0 comments on commit 6554ce1

Please sign in to comment.