1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
# Copyright (C) 2014-2019 Ruby-GNOME Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
class TestInputStream < Test::Unit::TestCase
include GioTestUtils::SocketClient
def setup
setup_socket_client
@stream = @connection.input_stream
end
def teardown
teardown_socket_client
end
sub_test_case(".open") do
def setup
super
@data = "Hello\n"
client = @server.accept
client.write(@data)
client.flush
client.close
end
def test_no_block
input = Gio::BufferedInputStream.open(@stream)
assert_equal(@data[0, 4], input.read(4))
input.close
end
def test_block
input = nil
read_data = Gio::BufferedInputStream.open(@stream) do |i|
input = i
input.read(4)
end
assert_equal([@data[0, 4], true],
[read_data, input.closed?])
end
def test_block_closed
input = nil
read_data = Gio::BufferedInputStream.open(@stream) do |i|
input = i
data = input.read(4)
input.close
data
end
assert_equal([@data[0, 4], true],
[read_data, input.closed?])
end
end
sub_test_case("#read") do
def test_with_size
data = "Hello\n"
client = @server.accept
client.write(data)
client.flush
assert_equal("Hell", @stream.read(4))
end
def test_without_size
data = "Hello\n"
client = @server.accept
client.write(data)
client.flush
client.close
assert_equal(data, @stream.read)
end
end
def test_read_all
client = @server.accept
client.write("He")
client.flush
thread = Thread.new do
sleep(0.1)
client.write("llo")
client.flush
end
assert_equal("Hell", @stream.read_all(4))
thread.join
end
end
|