Skip to content

Commit

Permalink
Keep a private reference to Process.clock_gettime
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
byroot committed Jun 8, 2022
1 parent 01554f1 commit 171eef4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Request

def initialize(thread, timeout, exception_class, message)
@thread = thread
@deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
@deadline = GET_TIME.call(Process::CLOCK_MONOTONIC) + timeout
@exception_class = exception_class
@message = message

Expand Down Expand Up @@ -109,7 +109,7 @@ def self.create_timeout_thread

now = 0.0
QUEUE_MUTEX.synchronize do
while (now = Process.clock_gettime(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
end
end
Expand All @@ -134,6 +134,12 @@ def self.ensure_timeout_thread_created
end
end
end

# We keep a private reference so that time mocking libraries won't break
# Timeout.
GET_TIME = Process.method(:clock_gettime)
private_constant :GET_TIME

# :startdoc:

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

0 comments on commit 171eef4

Please sign in to comment.