1 # frozen_string_literal: false
5 class TestTimeout < Test::Unit::TestCase
7 def test_non_timing_out_code_is_successful
8 assert_nothing_raised do
9 assert_equal :ok, Timeout.timeout(1){ :ok }
14 assert_equal [5, :ok], Timeout.timeout(5){|s| [s, :ok] }
19 assert_raise(Timeout::Error, "[ruby-dev:32935]") {
20 Timeout.timeout(0.01) { q.pop }
25 assert_raise(Timeout::Error) do
26 Timeout.timeout(0.1) {
32 def test_cannot_convert_into_time_interval
33 bug3168 = '[ruby-dev:41010]'
34 def (n = Object.new).zero?; false; end
35 assert_raise(TypeError, bug3168) {Timeout.timeout(n) { sleep 0.1 }}
39 bug8730 = '[Bug #8730]'
41 assert_raise_with_message(Timeout::Error, /execution expired/, bug8730) do
42 Timeout.timeout 0.01 do
49 assert_nil(e, bug8730)
53 exc = Class.new(RuntimeError)
55 assert_nothing_raised(exc) do
56 Timeout.timeout 0.01, exc do
63 assert_raise_with_message(exc, 'execution expired') {raise e if e}
66 def test_custom_exception
67 bug9354 = '[ruby-core:59511] [Bug #9354]'
68 err = Class.new(StandardError) do
69 def initialize(msg) super end
71 assert_nothing_raised(ArgumentError, bug9354) do
72 assert_equal(:ok, Timeout.timeout(100, err) {:ok})
74 assert_raise_with_message(err, 'execution expired') do
75 Timeout.timeout 0.01, err do
79 assert_raise_with_message(err, /connection to ruby-lang.org expired/) do
80 Timeout.timeout 0.01, err, "connection to ruby-lang.org expired" do
86 def test_exit_exception
87 assert_raise_with_message(Timeout::Error, "boon") do
88 Timeout.timeout(10, Timeout::Error) do
89 raise Timeout::Error, "boon"
94 def test_raise_with_message
95 bug17812 = '[ruby-core:103502] [Bug #17812]: Timeout::Error doesn\'t let two-argument raise() set a new message'
96 exc = Timeout::Error.new('foo')
97 assert_raise_with_message(Timeout::Error, 'bar', bug17812) do
102 def test_enumerator_next
103 bug9380 = '[ruby-dev:47872] [Bug #9380]: timeout in Enumerator#next'
104 e = (o=Object.new).to_enum
108 assert_raise_with_message(Timeout::Error, 'execution expired', bug9380) do
109 Timeout.timeout(0.01) {e.next}
113 def test_handle_interrupt
114 bug11344 = '[ruby-dev:49179] [Bug #11344]'
116 assert_raise(Timeout::Error) {
117 Thread.handle_interrupt(Timeout::Error => :never) {
118 Timeout.timeout(0.01) {
121 Thread.handle_interrupt(Timeout::Error => :on_blocking) {