summaryrefslogtreecommitdiff
path: root/test/webrick/test_utils.rb
blob: e63e0ebbfd40c20bd6711ad99c37688684bb98d4 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require "test/unit"
require "webrick/utils"

class TestWEBrickUtils < Test::Unit::TestCase
  def assert_expired(flag, m)
    if m == WEBrick::Utils
      handler = WEBrick::Utils::TimeoutHandler.instance
      assert_equal(flag, handler.instance_eval{ @timeout_info.empty? })
    end
  end

  def do_test_timeout(m)
    ex = Class.new(StandardError)

    assert_equal(:foo, m.timeout(10){ :foo })
    assert_expired(true, m)

    i = 0
    assert_raise(Timeout::Error){
      m.timeout(2){
        assert_raise(Timeout::Error){ m.timeout(1){ i += 1; sleep } }
        assert_expired(false, m)
        i += 1
        sleep
      }
    }
    assert_equal(2, i)
    assert_expired(true, m)

    assert_raise(Timeout::Error){ m.timeout(0.1){ sleep } }
    assert_expired(true, m)

    assert_raise(ex){ m.timeout(0.1, ex){ sleep } }
    assert_expired(true, m)

    i = 0
    assert_raise(ex){
      m.timeout(10){
        m.timeout(1, ex){ i += 1; sleep }
      }
      sleep
    }
    assert_equal(1, i)
    assert_expired(true, m)

    i = 0
    assert_raise(Timeout::Error){
      m.timeout(1){
        m.timeout(10, ex){ i += 1; sleep }
      }
      sleep
    }
    assert_equal(1, i)
    assert_expired(true, m)
  end

  def test_webrick_timeout
    do_test_timeout(WEBrick::Utils)
  end

  #def test_timeout
  #  do_test_timeout(Timeout)
  #end
end