Skip to content

Commit

Permalink
more tests for readall and readbytes
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Jan 20, 2016
1 parent 29e5455 commit 490cee3
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,32 @@ close(s)
push!(l, ("IOBuffer", io))


# TCPSocket
function run_test_server(srv, text)
push!(tasks, @async begin
try
sock = accept(srv)
try
write(sock,text)
catch e
if typeof(e) != Base.UVError
rethrow(e)
end
finally
close(sock)
end
finally
close(srv)
end
end)
yield()
end

# PR#14627
Base.connect!(sock::TCPSocket, addr::Base.InetAddr) = Base.connect!(sock, addr.host, addr.port)

addr = Base.InetAddr(ip"127.0.0.1", 4444)
# TCPSocket
io = (text) -> begin
c = Condition()
tsk = @async begin
srv = listen(addr)
notify(c)
sock = accept(srv)
write(sock,text)
close(sock)
close(srv)
end
push!(tasks, tsk)
wait(c)
connect(addr)
port, srv = listenany(rand(2000:4000))
run_test_server(srv, text)
connect(port)
end
s = io(text)
@test isa(s, IO)
Expand All @@ -70,22 +77,13 @@ close(s)
push!(l, ("TCPSocket", io))


@windows ? nothing : begin

# PipeEndpoint
socketname = joinpath(dir, "socket")
io = (text)-> begin
c = Condition()
tsk = @async begin
con = listen(socketname)
notify(c)
sock = accept(con)
try write(sock,text) end
close(sock)
close(con)
end
push!(tasks, tsk)
wait(c)
a = "\\\\.\\pipe\\uv-test-$(randstring(6))"
b = joinpath(dir, "socket-$(randstring(6))")
socketname = @windows ? a : b
srv = listen(socketname)
run_test_server(srv, text)
connect(socketname)
end
s = io(text)
Expand All @@ -95,6 +93,8 @@ close(s)
push!(l, ("PipeEndpoint", io))


@windows ? nothing : begin

# Pipe
io = (text) -> open(`echo -n $text`)[1]
s = io(text)
Expand All @@ -121,13 +121,7 @@ for (name, f) in l

io = ()->(s=f(text); push!(open_streams, s); s)

verbose && println("$name readall...")
@test readall(io()) == text
@test readall(io()) == readall(filename)

verbose && println("$name read...")
@test readbytes(io()) == Vector{UInt8}(text)
@test readbytes(io()) == open(readbytes,filename)
@test read(io(), UInt8) == read(IOBuffer(text), UInt8)
@test read(io(), UInt8) == open(io->read(io, UInt8), filename)
@test read(io(), Int) == read(IOBuffer(text), Int)
Expand All @@ -145,11 +139,6 @@ for (name, f) in l
close(s1)
close(s2)

verbose && println("$name readuntil...")
@test readuntil(io(), '\n') == open(io->readuntil(io,'\n'),filename)
@test readuntil(io(), "\n") == open(io->readuntil(io,"\n"),filename)
@test readuntil(io(), ',') == open(io->readuntil(io,','),filename)

verbose && println("$name eof...")
n = length(text) - 1
@test read!(io(), Vector{UInt8}(n)) ==
Expand All @@ -170,10 +159,15 @@ for (name, f) in l
UTF8String(['A' + i % 52 for i in 1:(div(Base.SZ_UNBUFFERED_IO,2))]),
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO -1)]),
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO )]),
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO +1)]),
UTF8String(['A' + i % 52 for i in 1:(7 + Base.SZ_UNBUFFERED_IO *3)])
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO +1)])
]

verbose && println("$name readall...")
@test readall(io()) == text

verbose && println("$name readbytes...")
@test readbytes(io()) == Vector{UInt8}(text)

verbose && println("$name readbytes!...")
l = length(text)
for n = [1, 2, l-2, l-1, l, l+1, l+2]
Expand Down Expand Up @@ -202,6 +196,11 @@ for (name, f) in l

cleanup()

verbose && println("$name readuntil...")
@test readuntil(io(), '\n') == readuntil(IOBuffer(text),'\n')
@test readuntil(io(), "\n") == readuntil(IOBuffer(text),"\n")
@test readuntil(io(), ',') == readuntil(IOBuffer(text),',')

verbose && println("$name readline...")
@test readline(io()) == readline(IOBuffer(text))

Expand Down

0 comments on commit 490cee3

Please sign in to comment.