2010-03-11 16:37:16 -05:00
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
def progress(n = 5)
|
2018-03-22 09:01:17 -04:00
|
|
|
n.times {|i| print i; STDOUT.flush; sleep 1}
|
2010-03-11 16:37:16 -05:00
|
|
|
puts "never reach"
|
|
|
|
end
|
|
|
|
|
2018-03-16 10:09:40 -04:00
|
|
|
p Timeout.timeout(5) {
|
2010-03-11 16:37:16 -05:00
|
|
|
45
|
|
|
|
}
|
2018-03-16 10:09:40 -04:00
|
|
|
p Timeout.timeout(5, Timeout::Error) {
|
2010-03-11 16:37:16 -05:00
|
|
|
45
|
|
|
|
}
|
2018-03-16 10:09:40 -04:00
|
|
|
p Timeout.timeout(nil) {
|
2010-03-11 16:37:16 -05:00
|
|
|
54
|
|
|
|
}
|
2018-03-16 10:09:40 -04:00
|
|
|
p Timeout.timeout(0) {
|
2010-03-11 16:37:16 -05:00
|
|
|
54
|
|
|
|
}
|
|
|
|
begin
|
2018-03-16 10:09:40 -04:00
|
|
|
Timeout.timeout(5) {progress}
|
2010-03-11 16:37:16 -05:00
|
|
|
rescue => e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
begin
|
2018-03-16 10:09:40 -04:00
|
|
|
Timeout.timeout(3) {
|
2010-03-11 16:37:16 -05:00
|
|
|
begin
|
2018-03-16 10:09:40 -04:00
|
|
|
Timeout.timeout(5) {progress}
|
2010-03-11 16:37:16 -05:00
|
|
|
rescue => e
|
|
|
|
puts "never reach"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rescue => e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
class MyTimeout < StandardError
|
|
|
|
end
|
|
|
|
begin
|
2018-03-16 10:09:40 -04:00
|
|
|
Timeout.timeout(2, MyTimeout) {progress}
|
2010-03-11 16:37:16 -05:00
|
|
|
rescue MyTimeout => e
|
|
|
|
puts e.message
|
|
|
|
end
|