Skip to content

Commit b120e65

Browse files
committed
Ensure timeout_after yields duration.
1 parent 4321cf2 commit b120e65

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

lib/async/reactor.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def initialize(...)
3434
Fiber.set_scheduler(self)
3535
end
3636

37-
alias with_timeout timeout_after
3837
public :sleep
3938
end
4039
end

lib/async/scheduler.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ def fiber(...)
282282

283283
# Invoke the block, but after the specified timeout, raise {TimeoutError} in any currenly blocking operation. If the block runs to completion before the timeout occurs or there are no non-blocking operations after the timeout expires, the code will complete without any exception.
284284
# @parameter duration [Numeric] The time in seconds, in which the task should complete.
285-
def timeout_after(timeout, exception = TimeoutError, message = "execution expired", &block)
285+
def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block)
286286
fiber = Fiber.current
287287

288-
timer = @timers.after(timeout) do
288+
timer = @timers.after(duration) do
289289
if fiber.alive?
290290
fiber.raise(exception, message)
291291
end
@@ -295,5 +295,11 @@ def timeout_after(timeout, exception = TimeoutError, message = "execution expire
295295
ensure
296296
timer.cancel if timer
297297
end
298+
299+
def timeout_after(duration, exception, message, &block)
300+
with_timeout(duration, exception, message) do |timer|
301+
yield duration
302+
end
303+
end
298304
end
299305
end

lib/async/task.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ def sleep(duration = nil)
9292
super
9393
end
9494

95-
# @deprecated Replaced by {Scheduler#timeout_after}.
96-
def with_timeout(timeout, exception = TimeoutError, message = "execution expired", &block)
97-
Fiber.scheduler.timeout_after(timeout, exception, message, &block)
95+
# Execute the given block of code, raising the specified exception if it exceeds the given duration during a non-blocking operation.
96+
def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block)
97+
Fiber.scheduler.with_timeout(duration, exception, message, &block)
9898
end
9999

100100
# Yield back to the reactor and allow other fibers to execute.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
require 'async/scheduler'
22+
23+
require 'timeout'
24+
25+
RSpec.describe Async::Scheduler, if: Async::Scheduler.supported? do
26+
include_context Async::RSpec::Reactor
27+
28+
describe ::Timeout do
29+
it "can invoke timeout and receive timeout as block argument" do
30+
::Timeout.timeout(1.0) do |duration|
31+
expect(duration).to be == 1.0
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)