Skip to content

Commit e5911a3

Browse files
byrooteregon
authored andcommitted
Keep a private reference to Process.clock_gettime
`timeout 0.3.0` broke our test suite because we have some tests that stubs `Process.clock_gettime` making it return a value in the past, causing `Timeout` to trigger almost immediately. I beleive it wasn't a problem before because it was relying on `Process.sleep`.
1 parent 01554f1 commit e5911a3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/timeout.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Request
6262

6363
def initialize(thread, timeout, exception_class, message)
6464
@thread = thread
65-
@deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
65+
@deadline = GET_TIME.call(Process::CLOCK_MONOTONIC) + timeout
6666
@exception_class = exception_class
6767
@message = message
6868

@@ -109,7 +109,7 @@ def self.create_timeout_thread
109109

110110
now = 0.0
111111
QUEUE_MUTEX.synchronize do
112-
while (now = Process.clock_gettime(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
112+
while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
113113
CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
114114
end
115115
end
@@ -134,6 +134,12 @@ def self.ensure_timeout_thread_created
134134
end
135135
end
136136
end
137+
138+
# We keep a private reference so that time mocking libraries won't break
139+
# Timeout.
140+
GET_TIME = Process.method(:clock_gettime)
141+
private_constant :GET_TIME
142+
137143
# :startdoc:
138144

139145
# Perform an operation in a block, raising an error if it takes longer than

0 commit comments

Comments
 (0)