diff options
author | Andrew Konchin <andry.konchin@gmail.com> | 2024-04-01 17:52:57 +0300 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2024-04-02 10:50:30 +0200 |
commit | 1e5949bd60464ed8767fcc8aabc79eeea5727daa (patch) | |
tree | 1c24f4ca43c05a6093b2e49fb575acf44634cbc8 /spec/ruby/library | |
parent | 8b55aaa85ca3b5333e6659f0f0b1eabdd0b9491b (diff) |
Update to ruby/spec@573cf97
Diffstat (limited to 'spec/ruby/library')
11 files changed, 119 insertions, 31 deletions
diff --git a/spec/ruby/library/io-wait/wait_spec.rb b/spec/ruby/library/io-wait/wait_spec.rb index 3861281277..fc07c6a8d9 100644 --- a/spec/ruby/library/io-wait/wait_spec.rb +++ b/spec/ruby/library/io-wait/wait_spec.rb @@ -48,25 +48,18 @@ describe "IO#wait" do end it "waits for the READABLE event to be ready" do - queue = Queue.new - thread = Thread.new { queue.pop; sleep 1; @w.write('data to read') }; + @r.wait(IO::READABLE, 0).should == nil - queue.push('signal'); - @r.wait(IO::READABLE, 2).should_not == nil - - thread.join + @w.write('data to read') + @r.wait(IO::READABLE, 0).should_not == nil end it "waits for the WRITABLE event to be ready" do written_bytes = IOWaitSpec.exhaust_write_buffer(@w) + @w.wait(IO::WRITABLE, 0).should == nil - queue = Queue.new - thread = Thread.new { queue.pop; sleep 1; @r.read(written_bytes) }; - - queue.push('signal'); - @w.wait(IO::WRITABLE, 2).should_not == nil - - thread.join + @r.read(written_bytes) + @w.wait(IO::WRITABLE, 0).should_not == nil end it "returns nil when the READABLE event is not ready during the timeout" do @@ -89,6 +82,24 @@ describe "IO#wait" do -> { @w.wait(-1, 0) }.should raise_error(ArgumentError, "Events must be positive integer!") end end + + it "changes thread status to 'sleep' when waits for READABLE event" do + t = Thread.new { @r.wait(IO::READABLE, 10) } + sleep 1 + t.status.should == 'sleep' + t.kill + t.join # Thread#kill doesn't wait for the thread to end + end + + it "changes thread status to 'sleep' when waits for WRITABLE event" do + written_bytes = IOWaitSpec.exhaust_write_buffer(@w) + + t = Thread.new { @w.wait(IO::WRITABLE, 10) } + sleep 1 + t.status.should == 'sleep' + t.kill + t.join # Thread#kill doesn't wait for the thread to end + end end context "[timeout, mode] passed" do diff --git a/spec/ruby/library/net-http/httpresponse/inspect_spec.rb b/spec/ruby/library/net-http/httpresponse/inspect_spec.rb index 23b6bff581..43071ec8cd 100644 --- a/spec/ruby/library/net-http/httpresponse/inspect_spec.rb +++ b/spec/ruby/library/net-http/httpresponse/inspect_spec.rb @@ -8,7 +8,7 @@ describe "Net::HTTPResponse#inspect" do res.inspect.should == "#<Net::HTTPUnknownResponse ??? test response readbody=false>" res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") - socket = Net::BufferedIO.new(StringIO.new(+"test body")) + socket = Net::BufferedIO.new(StringIO.new("test body")) res.reading_body(socket, true) {} res.inspect.should == "#<Net::HTTPUnknownResponse ??? test response readbody=true>" end diff --git a/spec/ruby/library/net-http/httpresponse/read_body_spec.rb b/spec/ruby/library/net-http/httpresponse/read_body_spec.rb index 61a576d812..4530a26bfc 100644 --- a/spec/ruby/library/net-http/httpresponse/read_body_spec.rb +++ b/spec/ruby/library/net-http/httpresponse/read_body_spec.rb @@ -5,7 +5,7 @@ require 'stringio' describe "Net::HTTPResponse#read_body" do before :each do @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") - @socket = Net::BufferedIO.new(StringIO.new(+"test body")) + @socket = Net::BufferedIO.new(StringIO.new("test body")) end describe "when passed no arguments" do diff --git a/spec/ruby/library/net-http/httpresponse/reading_body_spec.rb b/spec/ruby/library/net-http/httpresponse/reading_body_spec.rb index b9ab112c96..637a2806f8 100644 --- a/spec/ruby/library/net-http/httpresponse/reading_body_spec.rb +++ b/spec/ruby/library/net-http/httpresponse/reading_body_spec.rb @@ -5,7 +5,7 @@ require "stringio" describe "Net::HTTPResponse#reading_body" do before :each do @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") - @socket = Net::BufferedIO.new(StringIO.new(+"test body")) + @socket = Net::BufferedIO.new(StringIO.new("test body")) end describe "when body_allowed is true" do diff --git a/spec/ruby/library/net-http/httpresponse/shared/body.rb b/spec/ruby/library/net-http/httpresponse/shared/body.rb index f35ca3200c..618e3936fb 100644 --- a/spec/ruby/library/net-http/httpresponse/shared/body.rb +++ b/spec/ruby/library/net-http/httpresponse/shared/body.rb @@ -3,7 +3,7 @@ require 'stringio' describe :net_httpresponse_body, shared: true do before :each do @res = Net::HTTPUnknownResponse.new("1.0", "???", "test response") - @socket = Net::BufferedIO.new(StringIO.new(+"test body")) + @socket = Net::BufferedIO.new(StringIO.new("test body")) end it "returns the read body" do diff --git a/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb index df44a50afa..ea5e65da5c 100644 --- a/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb +++ b/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb @@ -18,7 +18,37 @@ describe "BasicSocket#read_nonblock" do it "receives data after it's ready" do IO.select([@r], nil, nil, 2) - @r.recv_nonblock(5).should == "aaa" + @r.read_nonblock(5).should == "aaa" + end + + platform_is_not :windows do + it 'returned data is binary encoded regardless of the external encoding' do + IO.select([@r], nil, nil, 2) + @r.read_nonblock(1).encoding.should == Encoding::BINARY + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::ISO_8859_1) + IO.select([@r], nil, nil, 2) + buffer = @r.read_nonblock(3) + buffer.should == "bbb" + buffer.encoding.should == Encoding::BINARY + end + end + + it 'replaces the content of the provided buffer without changing its encoding' do + buffer = "initial data".dup.force_encoding(Encoding::UTF_8) + + IO.select([@r], nil, nil, 2) + @r.read_nonblock(3, buffer) + buffer.should == "aaa" + buffer.encoding.should == Encoding::UTF_8 + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::ISO_8859_1) + IO.select([@r], nil, nil, 2) + @r.read_nonblock(3, buffer) + buffer.should == "bbb" + buffer.encoding.should == Encoding::UTF_8 end platform_is :linux do diff --git a/spec/ruby/library/socket/basicsocket/read_spec.rb b/spec/ruby/library/socket/basicsocket/read_spec.rb new file mode 100644 index 0000000000..ba9de7d5cf --- /dev/null +++ b/spec/ruby/library/socket/basicsocket/read_spec.rb @@ -0,0 +1,47 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "BasicSocket#read" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @r = Socket.new(family, :DGRAM) + @w = Socket.new(family, :DGRAM) + + @r.bind(Socket.pack_sockaddr_in(0, ip_address)) + @w.send("aaa", 0, @r.getsockname) + end + + after :each do + @r.close unless @r.closed? + @w.close unless @w.closed? + end + + it "receives data after it's ready" do + @r.read(3).should == "aaa" + end + + it 'returned data is binary encoded regardless of the external encoding' do + @r.read(3).encoding.should == Encoding::BINARY + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::UTF_8) + buffer = @r.read(3) + buffer.should == "bbb" + buffer.encoding.should == Encoding::BINARY + end + + it 'replaces the content of the provided buffer without changing its encoding' do + buffer = "initial data".dup.force_encoding(Encoding::UTF_8) + + @r.read(3, buffer) + buffer.should == "aaa" + buffer.encoding.should == Encoding::UTF_8 + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::ISO_8859_1) + @r.read(3, buffer) + buffer.should == "bbb" + buffer.encoding.should == Encoding::UTF_8 + end + end +end diff --git a/spec/ruby/library/stringio/fcntl_spec.rb b/spec/ruby/library/stringio/fcntl_spec.rb index f252d5e738..a78004d868 100644 --- a/spec/ruby/library/stringio/fcntl_spec.rb +++ b/spec/ruby/library/stringio/fcntl_spec.rb @@ -3,6 +3,6 @@ require_relative 'fixtures/classes' describe "StringIO#fcntl" do it "raises a NotImplementedError" do - -> { StringIO.new(+"boom").fcntl }.should raise_error(NotImplementedError) + -> { StringIO.new("boom").fcntl }.should raise_error(NotImplementedError) end end diff --git a/spec/ruby/library/stringio/initialize_spec.rb b/spec/ruby/library/stringio/initialize_spec.rb index d1dff590bb..ad067a0be1 100644 --- a/spec/ruby/library/stringio/initialize_spec.rb +++ b/spec/ruby/library/stringio/initialize_spec.rb @@ -172,7 +172,7 @@ end # NOTE: Synchronise with core/io/new_spec.rb (core/io/shared/new.rb) describe "StringIO#initialize when passed keyword arguments" do it "sets the mode based on the passed :mode option" do - io = StringIO.new(+"example", "r") + io = StringIO.new("example", "r") io.closed_read?.should be_false io.closed_write?.should be_true end diff --git a/spec/ruby/library/stringio/read_nonblock_spec.rb b/spec/ruby/library/stringio/read_nonblock_spec.rb index 8b78b1b0e4..74736f7792 100644 --- a/spec/ruby/library/stringio/read_nonblock_spec.rb +++ b/spec/ruby/library/stringio/read_nonblock_spec.rb @@ -33,7 +33,7 @@ end describe "StringIO#read_nonblock" do it "accepts an exception option" do - stringio = StringIO.new(+'foo') + stringio = StringIO.new('foo') stringio.read_nonblock(3, exception: false).should == 'foo' end diff --git a/spec/ruby/library/stringio/reopen_spec.rb b/spec/ruby/library/stringio/reopen_spec.rb index 151bb58c6f..7021ff17e5 100644 --- a/spec/ruby/library/stringio/reopen_spec.rb +++ b/spec/ruby/library/stringio/reopen_spec.rb @@ -3,11 +3,11 @@ require_relative 'fixtures/classes' describe "StringIO#reopen when passed [Object, Integer]" do before :each do - @io = StringIO.new(+"example") + @io = StringIO.new("example") end it "reopens self with the passed Object in the passed mode" do - @io.reopen(+"reopened", IO::RDONLY) + @io.reopen("reopened", IO::RDONLY) @io.closed_read?.should be_false @io.closed_write?.should be_true @io.string.should == "reopened" @@ -51,11 +51,11 @@ end describe "StringIO#reopen when passed [Object, Object]" do before :each do - @io = StringIO.new(+"example") + @io = StringIO.new("example") end it "reopens self with the passed Object in the passed mode" do - @io.reopen(+"reopened", "r") + @io.reopen("reopened", "r") @io.closed_read?.should be_false @io.closed_write?.should be_true @io.string.should == "reopened" @@ -83,7 +83,7 @@ describe "StringIO#reopen when passed [Object, Object]" do it "tries to convert the passed Object to a String using #to_str" do obj = mock("to_str") - obj.should_receive(:to_str).and_return(+"to_str") + obj.should_receive(:to_str).and_return("to_str") @io.reopen(obj, "r") @io.string.should == "to_str" end @@ -107,7 +107,7 @@ describe "StringIO#reopen when passed [Object, Object]" do it "tries to convert the passed mode Object to an Integer using #to_str" do obj = mock("to_str") obj.should_receive(:to_str).and_return("r") - @io.reopen(+"reopened", obj) + @io.reopen("reopened", obj) @io.closed_read?.should be_false @io.closed_write?.should be_true @io.string.should == "reopened" @@ -128,7 +128,7 @@ end describe "StringIO#reopen when passed [String]" do before :each do - @io = StringIO.new(+"example") + @io = StringIO.new("example") end it "reopens self with the passed String in read-write mode" do @@ -157,7 +157,7 @@ end describe "StringIO#reopen when passed [Object]" do before :each do - @io = StringIO.new(+"example") + @io = StringIO.new("example") end it "raises a TypeError when passed an Object that can't be converted to a StringIO" do @@ -180,7 +180,7 @@ end describe "StringIO#reopen when passed no arguments" do before :each do - @io = StringIO.new(+"example\nsecond line") + @io = StringIO.new("example\nsecond line") end it "resets self's mode to read-write" do @@ -208,7 +208,7 @@ end # for details. describe "StringIO#reopen" do before :each do - @io = StringIO.new(+'hello','a') + @io = StringIO.new(+'hello', 'a') end # TODO: find out if this is really a bug |