Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[io/wait] Add RBS for "io/wait" #756

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions core/io/wait.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class IO
# Returns number of bytes that can be read without blocking. Returns zero if no
# information available.
#
def nread: () -> Integer

# Returns `true` if input available without blocking, or `false`.
#
def ready?: () -> boolish

# Waits until the IO becomes ready for the specified events and returns the
# subset of events that become ready, or `false` when times out.
#
# The events can be a bit mask of `IO::READABLE`, `IO::WRITABLE` or
# `IO::PRIORITY`.
#
# Returns `true` immediately when buffered data is available.
#
# Optional parameter `mode` is one of `:read`, `:write`, or `:read_write`
# (deprecated).
#
def wait: (Integer events, ?Numeric timeout) -> self?
| (Integer timeout, ?(:read | :write | :read_write) mode) -> self?

# Waits until IO is readable and returns `true`, or `false` when times out.
# Returns `true` immediately when buffered data is available.
#
def wait_readable: (?Numeric timeout) -> self?

# Waits until IO is writable and returns `true` or `false` when times out.
#
def wait_writable: (?Numeric timeout) -> self?
end
29 changes: 29 additions & 0 deletions test/stdlib/IO_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "test_helper"
require "io/wait"

class IOSingletonTest < Test::Unit::TestCase
include TypeAssertions
Expand Down Expand Up @@ -241,3 +242,31 @@ def test_sync
end
end
end

class IOWaitTest < Test::Unit::TestCase
include TypeAssertions

testing "::IO"

def test_wait
r, w = IO.pipe
assert_send_type "() -> Integer",
r, :nread
assert_send_type "() -> nil",
r, :ready?
assert_send_type "(Float) -> nil",
r, :wait_readable, 0.2
assert_send_type "(Integer, Float) -> nil",
r, :wait, IO::READABLE, 0.2
assert_send_type "(Float) -> IO",
w, :wait_writable, 0.2
w.write("a")
assert_send_type "(Float) -> IO",
r, :wait_readable, 0.2
assert_send_type "() -> IO",
r, :ready?
ensure
r.close
w.close
end if RUBY_VERSION >= "3.0.0"
end