diff options
-rw-r--r-- | lib/net/protocol.rb | 2 | ||||
-rw-r--r-- | test/net/protocol/test_protocol.rb | 16 |
2 files changed, 14 insertions, 4 deletions
diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 3610abcacc..60e23f1aa5 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -286,7 +286,7 @@ module Net # :nodoc: # next string end elsif len < 0 - str = str[len, -len] + str = str.byteslice(len, -len) else # len > 0 need_retry = false # next string diff --git a/test/net/protocol/test_protocol.rb b/test/net/protocol/test_protocol.rb index c1837ecf20..2ad3709ac5 100644 --- a/test/net/protocol/test_protocol.rb +++ b/test/net/protocol/test_protocol.rb @@ -27,10 +27,10 @@ class TestProtocol < Test::Unit::TestCase end end - def create_mockio + def create_mockio(capacity: 100) mockio = Object.new mockio.instance_variable_set(:@str, +'') - mockio.instance_variable_set(:@capacity, 100) + mockio.instance_variable_set(:@capacity, capacity) def mockio.string; @str; end def mockio.to_io; self; end def mockio.wait_writable(sec); sleep sec; false; end @@ -46,7 +46,7 @@ class TestProtocol < Test::Unit::TestCase strs.each do |str| len1 = @str.bytesize break if @capacity <= len1 - @str << str[0, @capacity - @str.bytesize] + @str << str.byteslice(0, @capacity - @str.bytesize) len2 = @str.bytesize len += len2 - len1 end @@ -55,6 +55,16 @@ class TestProtocol < Test::Unit::TestCase mockio end + def test_write0_multibyte + mockio = create_mockio(capacity: 1) + def mockio.write_nonblock(str, *strs, **kw) + @str << str.byteslice(0, 1) + 1 + end + io = Net::BufferedIO.new(mockio) + assert_equal(3, io.write("\u3042")) + end + def test_write0_timeout mockio = create_mockio io = Net::BufferedIO.new(mockio) |