Skip to content

Commit cd51eac

Browse files
committed
Only the timeout method should be public on the Timeout module
1 parent 56d5305 commit cd51eac

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/timeout.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def self.ensure_timeout_thread_created
133133
end
134134
end
135135
end
136+
private_class_method :ensure_timeout_thread_created
136137

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

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

180-
Timeout.ensure_timeout_thread_created
181+
ensure_timeout_thread_created
181182
perform = Proc.new do |exc|
182183
request = Request.new(Thread.current, sec, exc, message)
183184
QUEUE_MUTEX.synchronize do
@@ -197,5 +198,8 @@ def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
197198
Error.handle_timeout(message, &perform)
198199
end
199200
end
200-
module_function :timeout
201+
202+
private def timeout(*args, &block)
203+
Timeout.timeout(*args, &block)
204+
end
201205
end

test/test_timeout.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
class TestTimeout < Test::Unit::TestCase
66

7+
def test_public_methods
8+
assert_equal [:timeout], Timeout.private_instance_methods(false)
9+
assert_equal [], Timeout.public_instance_methods(false)
10+
assert_equal [:timeout], Timeout.singleton_class.public_instance_methods(false)
11+
end
12+
713
def test_work_is_done_in_same_thread_as_caller
814
assert_equal Thread.current, Timeout.timeout(10){ Thread.current }
915
end

0 commit comments

Comments
 (0)