-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
178 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- encoding: utf-8 -*- | ||
require_relative '../../spec_helper' | ||
|
||
describe "IO#timeout" do | ||
before :each do | ||
@fname = tmp("io_timeout.txt") | ||
@file = File.open(@fname, "a+") | ||
|
||
@rpipe, @wpipe = IO.pipe | ||
# There is no strict long term standard for pipe limits (2**16 bytes currently). This is an attempt to set a safe | ||
# enough size to test a full pipe. | ||
@more_than_pipe_limit = 1 << 18 | ||
end | ||
|
||
after :each do | ||
@rpipe.close | ||
@wpipe.close | ||
|
||
@file.close | ||
rm_r @fname | ||
end | ||
|
||
ruby_version_is "3.2" do | ||
it "files have timeout attribute" do | ||
@fname = tmp("io_timeout_attribute.txt") | ||
touch(@fname) | ||
|
||
@file.timeout.should == nil | ||
|
||
@file.timeout = 1.23 | ||
@file.timeout.should == 1.23 | ||
end | ||
|
||
it "IO instances have timeout attribute" do | ||
@rpipe.timeout.should == nil | ||
@wpipe.timeout.should == nil | ||
|
||
@rpipe.timeout = 1.23 | ||
@wpipe.timeout = 4.56 | ||
|
||
@rpipe.timeout.should == 1.23 | ||
@wpipe.timeout.should == 4.56 | ||
end | ||
|
||
it "raises IO::TimeoutError when timeout is exceeded for .read" do | ||
@rpipe.timeout = 0.01 | ||
-> { @rpipe.read.should }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "raises IO::TimeoutError when timeout is exceeded for .read(n)" do | ||
@rpipe.timeout = 0.01 | ||
-> { @rpipe.read(3) }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "raises IO::TimeoutError when timeout is exceeded for .gets" do | ||
@rpipe.timeout = 0.01 | ||
-> { @rpipe.gets }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "raises IO::TimeoutError when timeout is exceeded for .write" do | ||
@wpipe.timeout = 0.01 | ||
-> { @wpipe.write("x" * @more_than_pipe_limit) }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "raises IO::TimeoutError when timeout is exceeded for .puts" do | ||
@wpipe.timeout = 0.01 | ||
-> { @wpipe.puts("x" * @more_than_pipe_limit) }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "times out with .read when there is no EOF" do | ||
@wpipe.write("hello") | ||
@rpipe.timeout = 0.01 | ||
|
||
-> { @rpipe.read }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "returns content with .read when there is EOF" do | ||
@wpipe.write("hello") | ||
@wpipe.close | ||
|
||
@rpipe.timeout = 0.01 | ||
|
||
@rpipe.read.should == "hello" | ||
end | ||
|
||
it "times out with .read(N) when there is not enough bytes" do | ||
@wpipe.write("hello") | ||
@rpipe.timeout = 0.01 | ||
|
||
@rpipe.read(2).should == "he" | ||
-> { @rpipe.read(5) }.should raise_error(IO::TimeoutError) | ||
end | ||
|
||
it "returns partial content with .read(N) when there is not enough bytes but there is EOF" do | ||
@wpipe.write("hello") | ||
@rpipe.timeout = 0.01 | ||
|
||
@rpipe.read(2).should == "he" | ||
|
||
@wpipe.close | ||
@rpipe.read(5).should == "llo" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters