summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-14 02:22:51 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-14 02:22:51 +0000
commit7d909eb218100aa110031e2aadf0fead94146e8b (patch)
tree6b61e355a5c0bb50a9952ae58e3fb09b6266cc62
parent8f6fed850a08ffbc7aa9abb2a941faa6c47b3a18 (diff)
webrick/utils.rb: wakeup immediately
* lib/webrick/utils.rb (WEBrick::Utils::TimeoutHandler#register): notify the handler thread of new timeout registration. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--lib/webrick/utils.rb10
-rw-r--r--test/webrick/test_utils.rb16
3 files changed, 19 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index c55354e..fe06140 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-Tue Jul 14 11:21:31 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Tue Jul 14 11:22:42 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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.
diff --git a/lib/webrick/utils.rb b/lib/webrick/utils.rb
index c6da844..da6386c 100644
--- a/lib/webrick/utils.rb
+++ b/lib/webrick/utils.rb
@@ -153,7 +153,7 @@ module WEBrick
# 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
# +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
##
diff --git a/test/webrick/test_utils.rb b/test/webrick/test_utils.rb
index 3ab4f2b..9d135f7 100644
--- a/test/webrick/test_utils.rb
+++ b/test/webrick/test_utils.rb
@@ -5,7 +5,7 @@ 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? })
+ assert_equal(flag, handler.instance_variable_get(:@timeout_info).empty?)
end
end
@@ -21,8 +21,8 @@ class TestWEBrickUtils < Test::Unit::TestCase
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::TestCase
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::TestCase
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::TestCase
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
}