Skip to content

Instantly share code, notes, and snippets.

@leehambley
Created March 11, 2014 08:02
Show Gist options
  • Save leehambley/2ea90b2d7bec46153520 to your computer and use it in GitHub Desktop.
Save leehambley/2ea90b2d7bec46153520 to your computer and use it in GitHub Desktop.

stdouterr.rb

A script designed to stress SSHKit by outputting on stdout and stderr with and without buffering, with and without flushing it's outputs to try and find a reliable solution for capturing outputs from programs which are badly behaved.

Example Usage

$ ruby ./stdouterr.rb --unbuffered --succeed
0. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
0. Proin nec tristique nisl.
1. Pellentesque venenatis magna malesuada aliquam posuere.
2. Ut interdum ornare massa.
1. Suspendisse ultrices, mauris vitae lobortis tempus, ipsum mi facilisis purus, vel auctor purus nisl sit amet eros.
2. Aliquam nec varius ante.
succeeding

Available Options

 --buffered / --unbuffered   Sync stdout and stderr?
 --fail / --succeed          Exit with `1' or `0'
 --flush                     In case of --buffered, flush every line after writing?
require 'getoptlong'
output = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'Pellentesque venenatis magna malesuada aliquam posuere.',
'Ut interdum ornare massa.',
'Proin nec tristique nisl.',
'Suspendisse ultrices, mauris vitae lobortis tempus, ipsum mi facilisis purus, vel auctor purus nisl sit amet eros.',
'Aliquam nec varius ante.'
]
opts = GetoptLong.new(
[ '--buffered', '-b', GetoptLong::NO_ARGUMENT ],
[ '--unbuffered', '-u', GetoptLong::NO_ARGUMENT ],
[ '--fail', '-f', GetoptLong::NO_ARGUMENT ],
[ '--succeed', '-s', GetoptLong::NO_ARGUMENT ],
[ '--flush', '-l', GetoptLong::NO_ARGUMENT ]
)
mode = nil
code = nil
flush = nil
opts.each do |opt|
case opt
when '--buffered' then mode = :buffered
when '--unbuffered' then mode = :unbuffered
when '--fail' then code = 1
when '--succeed' then code = 0
when '--flush' then flush = true
end
end
unless mode
abort "Must specify one of --buffered or --unbuffered"
end
unless code
abort "Must specify one of --fail or --succeed"
end
if mode == :unbuffered and flush == true
abort "--flush (-l) only has meaning to buffered outputs"
end
if mode == :unbuffered
$stdout.sync = false
$stderr.sync = false
end
stdout_t = Thread.new do
output[0..2].each_with_index do |line, i|
$stdout.write "%d. %s\n" % [i, line]
$stdout.flush if flush
end
end
stderr_t = Thread.new do
output[3..5].each_with_index do |line, i|
$stderr.write "%d. %s\n" % [i, line]
$stderr.flush if flush
end
end
[stdout_t, stderr_t].map(&:join)
if code == 1
abort("failing")
else
puts("succeeding")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment