Skip to content

Commit

Permalink
Add spec for Socket#read_nonblock with a provided buffer
Browse files Browse the repository at this point in the history
When provided a buffer MRI preserves the original encoding
but TruffleRuby sets the encoding to `Encoding::BINARY`
  • Loading branch information
byroot committed Mar 23, 2024
1 parent e2f39fe commit c78a7e9
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion library/socket/basicsocket/read_nonblock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,34 @@

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

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("aaa", 0, @r.getsockname)
@r.set_encoding(Encoding::UTF_8)
IO.select([@r], nil, nil, 2)
@r.read_nonblock(1).encoding.should == Encoding::BINARY
end

it 'replaces the content of the provided buffer without changing its encoding' do
buffer = +"initial data"
initial_encoding = buffer.encoding

IO.select([@r], nil, nil, 2)
@r.read_nonblock(5, buffer)
buffer.should == "aaa"
buffer.encoding.should == initial_encoding

@w.send("aaa", 0, @r.getsockname)
@r.set_encoding(Encoding::ISO_8859_1)
IO.select([@r], nil, nil, 2)
@r.read_nonblock(5, buffer)
buffer.should == "aaa"
buffer.encoding.should == initial_encoding
end

platform_is :linux do
Expand Down

0 comments on commit c78a7e9

Please sign in to comment.