File: timeout.rb

package info (click to toggle)
ruby-open4 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 184 kB
  • sloc: ruby: 806; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 629 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

require 'open4'

def show_failure
  fork{ yield }
  Process.wait
  puts
end

#
# command timeout
#
  show_failure{
    open4.spawn 'sleep 42', 'timeout' => 1
  }

#
# stdin timeout
#
  show_failure{

    producer = 'ruby -e" STDOUT.sync = true; loop{sleep(rand+rand) and puts 42} " 2>/dev/null'

    consumer = 'ruby -e" STDOUT.sync = true; STDIN.each{|line| puts line} "'

    open4(producer) do |pid, i, o, e|
      open4.spawn consumer, 0=>o, 1=>STDOUT, :stdin_timeout => 1.4
    end
  }

#
# stdout timeout (stderr is similar)
#

  show_failure{
    open4.spawn 'ruby -e"  sleep 2 and puts 42  "', 'stdout_timeout' => 1
  }