[ruby/timeout] Only the timeout method should be public on the Timeout module

https://github.com/ruby/timeout/commit/cd51eac3ca
This commit is contained in:
Benoit Daloze 2025-12-05 12:32:35 +01:00 committed by git
parent 1cad20e2d5
commit 95ea3bd5ee
2 changed files with 13 additions and 3 deletions

View File

@ -133,6 +133,7 @@ module Timeout
end
end
end
private_class_method :ensure_timeout_thread_created
# We keep a private reference so that time mocking libraries won't break
# Timeout.
@ -167,7 +168,7 @@ module Timeout
# 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
@ -177,7 +178,7 @@ module Timeout
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
@ -197,5 +198,8 @@ module Timeout
Error.handle_timeout(message, &perform)
end
end
module_function :timeout
private def timeout(*args, &block)
Timeout.timeout(*args, &block)
end
end

View File

@ -4,6 +4,12 @@ require 'timeout'
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