Add max option to TestProtocol#create_mockio
* test/net/protocol/test_protocol.rb (TestProtocol#create_mockio): max option to limit maximum writable size at once, and unify mock method in test_write0_multibyte.
This commit is contained in:
parent
a88107c44d
commit
7ba14b029c
@ -27,10 +27,11 @@ class TestProtocol < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_mockio(capacity: 100)
|
def create_mockio(capacity: 100, max: nil)
|
||||||
mockio = Object.new
|
mockio = Object.new
|
||||||
mockio.instance_variable_set(:@str, +'')
|
mockio.instance_variable_set(:@str, +'')
|
||||||
mockio.instance_variable_set(:@capacity, capacity)
|
mockio.instance_variable_set(:@capacity, capacity)
|
||||||
|
mockio.instance_variable_set(:@max, max)
|
||||||
def mockio.string; @str; end
|
def mockio.string; @str; end
|
||||||
def mockio.to_io; self; end
|
def mockio.to_io; self; end
|
||||||
def mockio.wait_writable(sec); sleep sec; false; end
|
def mockio.wait_writable(sec); sleep sec; false; end
|
||||||
@ -43,10 +44,11 @@ class TestProtocol < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
len = 0
|
len = 0
|
||||||
|
max = @max ? [@capacity, @str.bytesize + @max].min : @capacity
|
||||||
strs.each do |str|
|
strs.each do |str|
|
||||||
len1 = @str.bytesize
|
len1 = @str.bytesize
|
||||||
break if @capacity <= len1
|
break if max <= len1
|
||||||
@str << str.byteslice(0, @capacity - @str.bytesize)
|
@str << str.byteslice(0, max - @str.bytesize)
|
||||||
len2 = @str.bytesize
|
len2 = @str.bytesize
|
||||||
len += len2 - len1
|
len += len2 - len1
|
||||||
end
|
end
|
||||||
@ -56,11 +58,7 @@ class TestProtocol < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_write0_multibyte
|
def test_write0_multibyte
|
||||||
mockio = create_mockio(capacity: 1)
|
mockio = create_mockio(max: 1)
|
||||||
def mockio.write_nonblock(str, *strs, **kw)
|
|
||||||
@str << str.byteslice(0, 1)
|
|
||||||
1
|
|
||||||
end
|
|
||||||
io = Net::BufferedIO.new(mockio)
|
io = Net::BufferedIO.new(mockio)
|
||||||
assert_equal(3, io.write("\u3042"))
|
assert_equal(3, io.write("\u3042"))
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user