Skip to content

Commit

Permalink
Support older rubies.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 1, 2024
1 parent e3555e0 commit be8bd28
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
21 changes: 17 additions & 4 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,28 @@ def address_resolve(hostname)
::Resolv.getaddresses(hostname)
end


if IO.method_defined?(:timeout)
private def get_timeout(io)
io.timeout
end
else
private def get_timeout(io)
nil
end
end

# @asynchronous May be non-blocking..
def io_wait(io, events, timeout = nil)
fiber = Fiber.current

if timeout
# If an explicit timeout is specified, we expect that the user will handle it themselves:
timer = @timers.after(timeout) do
fiber.transfer
end
elsif timeout = io.timeout
elsif timeout = get_timeout(io)
# Otherwise, if we default to the io's timeout, we raise an exception:
timer = @timers.after(timeout) do
fiber.raise(::IO::TimeoutError, "Timeout while waiting for IO to become ready!")
end
Expand All @@ -183,12 +196,12 @@ def io_wait(io, events, timeout = nil)
ensure
timer&.cancel
end

if ::IO::Event::Support.buffer?
def io_read(io, buffer, length, offset = 0)
fiber = Fiber.current

if timeout = io.timeout
if timeout = get_timeout(io)
timer = @timers.after(timeout) do
fiber.raise(::IO::TimeoutError, "execution expired")
end
Expand All @@ -203,7 +216,7 @@ def io_read(io, buffer, length, offset = 0)
def io_write(io, buffer, length, offset = 0)
fiber = Fiber.current

if timeout = io.timeout
if timeout = get_timeout(io)
timer = @timers.after(timeout) do
fiber.raise(::IO::TimeoutError, "execution expired")
end
Expand Down
4 changes: 2 additions & 2 deletions lib/async/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def dup

# Wait for the io to become readable.
def wait_readable(timeout = @timeout)
@io.to_io.wait_readable(timeout) or raise ::IO::TimeoutError
@io.to_io.wait_readable(timeout) or raise TimeoutError
end

# Wait for the io to become writable.
def wait_priority(timeout = @timeout)
@io.to_io.wait_priority(timeout) or raise ::IO::TimeoutError
@io.to_io.wait_priority(timeout) or raise TimeoutError
end

# Wait for the io to become writable.
Expand Down
2 changes: 1 addition & 1 deletion test/async/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def after
it "can timeout if no event occurs" do
expect do
output.wait_readable(0.001)
end.to raise_exception(IO::TimeoutError)
end.to raise_exception(Async::TimeoutError)
end

it "can wait for readability in sequential tasks" do
Expand Down
6 changes: 5 additions & 1 deletion test/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
end

it "can read with timeout" do
skip_unless_constant_defined(:TimeoutError, IO)

input, output = IO.pipe
input.timeout = 0.001

Expand All @@ -41,6 +43,8 @@
end

it "can wait readable with default timeout" do
skip_unless_constant_defined(:TimeoutError, IO)

input, output = IO.pipe
input.timeout = 0.001

Expand All @@ -51,7 +55,7 @@
end.to raise_exception(::IO::TimeoutError)
end

it "can wait readable with 0 timeout" do
it "can wait readable with explicit timeout" do
input, output = IO.pipe

expect(input.wait_readable(0)).to be_nil
Expand Down

0 comments on commit be8bd28

Please sign in to comment.