ruby-changes:39154
From: nobu <ko1@a...>
Date: Tue, 14 Jul 2015 11:22:58 +0900 (JST)
Subject: [ruby-changes:39154] nobu:r51235 (trunk): webrick/utils.rb: wakeup immediately
nobu 2015-07-14 11:22:51 +0900 (Tue, 14 Jul 2015) New Revision: 51235 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51235 Log: webrick/utils.rb: wakeup immediately * lib/webrick/utils.rb (WEBrick::Utils::TimeoutHandler#register): notify the handler thread of new timeout registration. Modified files: trunk/ChangeLog trunk/lib/webrick/utils.rb trunk/test/webrick/test_utils.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 51234) +++ ChangeLog (revision 51235) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Tue Jul 14 11:21:31 2015 Nobuyoshi Nakada <nobu@r...> +Tue Jul 14 11:22:42 2015 Nobuyoshi Nakada <nobu@r...> + + * lib/webrick/utils.rb (WEBrick::Utils::TimeoutHandler#register): + notify the handler thread of new timeout registration. * lib/webrick/utils.rb (WEBrick::Utils::TimeoutHandler#initialize): make sleep intervals adaptive than fixed period intervals. Index: lib/webrick/utils.rb =================================================================== --- lib/webrick/utils.rb (revision 51234) +++ lib/webrick/utils.rb (revision 51235) @@ -153,7 +153,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/utils.rb#L153 # instead of creating the timeout handler directly. def initialize @timeout_info = Hash.new - Thread.start{ + @watcher = Thread.start{ while true now = Time.now wakeup = nil @@ -194,8 +194,12 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/utils.rb#L194 # +exception+:: Exception to raise when timeout elapsed def register(thread, time, exception) @timeout_info[thread] ||= Array.new - @timeout_info[thread] << [time, exception] - return @timeout_info[thread].last.object_id + @timeout_info[thread] << (info = [time, exception]) + begin + @watcher.wakeup + rescue ThreadError + end + return info.object_id end ## Index: test/webrick/test_utils.rb =================================================================== --- test/webrick/test_utils.rb (revision 51234) +++ test/webrick/test_utils.rb (revision 51235) @@ -5,7 +5,7 @@ class TestWEBrickUtils < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/webrick/test_utils.rb#L5 def assert_expired(flag, m) if m == WEBrick::Utils handler = WEBrick::Utils::TimeoutHandler.instance - assert_equal(flag, handler.instance_eval{ @timeout_info.empty? }) + assert_equal(flag, handler.instance_variable_get(:@timeout_info).empty?) end end @@ -21,8 +21,8 @@ class TestWEBrickUtils < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/webrick/test_utils.rb#L21 m = WEBrick::Utils i = 0 assert_raise(Timeout::Error){ - m.timeout(2){ - assert_raise(Timeout::Error){ m.timeout(1){ i += 1; sleep } } + m.timeout(0.02){ + assert_raise(Timeout::Error){ m.timeout(0.01){ i += 1; sleep } } assert_expired(false, m) i += 1 sleep @@ -34,14 +34,14 @@ class TestWEBrickUtils < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/webrick/test_utils.rb#L34 def test_timeout_default_execption m = WEBrick::Utils - assert_raise(Timeout::Error){ m.timeout(0.1){ sleep } } + assert_raise(Timeout::Error){ m.timeout(0.01){ sleep } } assert_expired(true, m) end def test_timeout_custom_exception m = WEBrick::Utils ex = EX - assert_raise(ex){ m.timeout(0.1, ex){ sleep } } + assert_raise(ex){ m.timeout(0.01, ex){ sleep } } assert_expired(true, m) end @@ -51,7 +51,7 @@ class TestWEBrickUtils < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/webrick/test_utils.rb#L51 i = 0 assert_raise(ex){ m.timeout(10){ - m.timeout(1, ex){ i += 1; sleep } + m.timeout(0.01, ex){ i += 1; sleep } } sleep } @@ -64,8 +64,8 @@ class TestWEBrickUtils < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/webrick/test_utils.rb#L64 ex = EX i = 0 assert_raise(Timeout::Error){ - m.timeout(1){ - m.timeout(10, ex){ i += 1; sleep } + m.timeout(0.01){ + m.timeout(1.0, ex){ i += 1; sleep } } sleep } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/