Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def self.ensure_timeout_thread_created
end
end
end
private_class_method :ensure_timeout_thread_created

# We keep a private reference so that time mocking libraries won't break
# Timeout.
Expand Down Expand Up @@ -167,7 +168,7 @@ def self.ensure_timeout_thread_created
# Note that this is both a method of module Timeout, so you can <tt>include
# Timeout</tt> into your classes so they have a #timeout method, as well as
# a module method, so you can call it directly as Timeout.timeout().
def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
def self.timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
return yield(sec) if sec == nil or sec.zero?
raise ArgumentError, "Timeout sec must be a non-negative number" if 0 > sec

Expand All @@ -177,7 +178,7 @@ def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
return scheduler.timeout_after(sec, klass || Error, message, &block)
end

Timeout.ensure_timeout_thread_created
ensure_timeout_thread_created
perform = Proc.new do |exc|
request = Request.new(Thread.current, sec, exc, message)
QUEUE_MUTEX.synchronize do
Expand All @@ -197,5 +198,8 @@ def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
Error.handle_timeout(message, &perform)
end
end
module_function :timeout

private def timeout(*args, &block)
Timeout.timeout(*args, &block)
end
end
6 changes: 6 additions & 0 deletions test/test_timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

class TestTimeout < Test::Unit::TestCase

def test_public_methods
assert_equal [:timeout], Timeout.private_instance_methods(false)
assert_equal [], Timeout.public_instance_methods(false)
assert_equal [:timeout], Timeout.singleton_class.public_instance_methods(false)
end

def test_work_is_done_in_same_thread_as_caller
assert_equal Thread.current, Timeout.timeout(10){ Thread.current }
end
Expand Down