Skip to content

Commit f290e2e

Browse files
committed
more tests for readall and readbytes
1 parent 1a3b7ec commit f290e2e

File tree

1 file changed

+68
-45
lines changed

1 file changed

+68
-45
lines changed

test/read.jl

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,32 @@ close(s)
4343
push!(l, ("IOBuffer", io))
4444

4545

46-
# TCPSocket
46+
function run_test_server(srv, text)
47+
push!(tasks, @async begin
48+
try
49+
sock = accept(srv)
50+
try
51+
write(sock,text)
52+
catch e
53+
if typeof(e) != Base.UVError
54+
rethrow(e)
55+
end
56+
finally
57+
close(sock)
58+
end
59+
finally
60+
close(srv)
61+
end
62+
end)
63+
yield()
64+
end
4765

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

51-
addr = Base.InetAddr(ip"127.0.0.1", 4444)
67+
# TCPSocket
5268
io = (text) -> begin
53-
c = Condition()
54-
tsk = @async begin
55-
srv = listen(addr)
56-
notify(c)
57-
sock = accept(srv)
58-
write(sock,text)
59-
close(sock)
60-
close(srv)
61-
end
62-
push!(tasks, tsk)
63-
wait(c)
64-
connect(addr)
69+
port, srv = listenany(rand(2000:4000))
70+
run_test_server(srv, text)
71+
connect(port)
6572
end
6673
s = io(text)
6774
@test isa(s, IO)
@@ -70,22 +77,13 @@ close(s)
7077
push!(l, ("TCPSocket", io))
7178

7279

73-
@windows ? nothing : begin
74-
7580
# PipeEndpoint
76-
socketname = joinpath(dir, "socket")
77-
io = (text)-> begin
78-
c = Condition()
79-
tsk = @async begin
80-
con = listen(socketname)
81-
notify(c)
82-
sock = accept(con)
83-
try write(sock,text) end
84-
close(sock)
85-
close(con)
86-
end
87-
push!(tasks, tsk)
88-
wait(c)
81+
io = (text) -> begin
82+
a = "\\\\.\\pipe\\uv-test-$(randstring(6))"
83+
b = joinpath(dir, "socket-$(randstring(6))")
84+
socketname = @windows ? a : b
85+
srv = listen(socketname)
86+
run_test_server(srv, text)
8987
connect(socketname)
9088
end
9189
s = io(text)
@@ -95,8 +93,18 @@ close(s)
9593
push!(l, ("PipeEndpoint", io))
9694

9795

96+
@windows ? nothing : begin
97+
98+
# See "could not spawn `type 'C:\Users\appveyor\AppData\Local\Temp\1\jul3516.tmp\file.txt'`"
99+
#https://ci.appveyor.com/project/StefanKarpinski/julia/build/1.0.12733/job/hpwjs4hmf03vs5ag#L1244
100+
98101
# Pipe
99-
io = (text) -> open(`echo -n $text`)[1]
102+
io = (text) -> begin
103+
open(io->write(io, text), filename, "w")
104+
open(`$(@windows ? "type" : "cat") $filename`)[1]
105+
# Was open(`echo -n $text`)[1]
106+
# See https://github.com/JuliaLang/julia/issues/14747
107+
end
100108
s = io(text)
101109
@test isa(s, IO)
102110
@test isa(s, Pipe)
@@ -105,33 +113,33 @@ push!(l, ("Pipe", io))
105113

106114
end
107115

116+
108117
open_streams = []
109118
function cleanup()
110119
for s in open_streams
111120
try close(s) end
112121
end
122+
empty!(open_streams)
113123
for tsk in tasks
114124
wait(tsk)
115125
end
126+
empty!(tasks)
116127
end
117128

118129
verbose = false
119130

131+
cleanup()
132+
120133
for (name, f) in l
121134

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

124-
verbose && println("$name readall...")
125-
@test readall(io()) == text
126-
@test readall(io()) == readall(filename)
127-
128137
verbose && println("$name read...")
129-
@test readbytes(io()) == Vector{UInt8}(text)
130-
@test readbytes(io()) == open(readbytes,filename)
131138
@test read(io(), UInt8) == read(IOBuffer(text), UInt8)
132139
@test read(io(), UInt8) == open(io->read(io, UInt8), filename)
133140
@test read(io(), Int) == read(IOBuffer(text), Int)
134141
@test read(io(), Int) == open(io->read(io,Int),filename)
142+
cleanup()
135143
s1 = io()
136144
s2 = IOBuffer(text)
137145
@test read(s1, UInt32, 2) == read(s2, UInt32, 2)
@@ -145,11 +153,6 @@ for (name, f) in l
145153
close(s1)
146154
close(s2)
147155

148-
verbose && println("$name readuntil...")
149-
@test readuntil(io(), '\n') == open(io->readuntil(io,'\n'),filename)
150-
@test readuntil(io(), "\n") == open(io->readuntil(io,"\n"),filename)
151-
@test readuntil(io(), ',') == open(io->readuntil(io,','),filename)
152-
153156
verbose && println("$name eof...")
154157
n = length(text) - 1
155158
@test read!(io(), Vector{UInt8}(n)) ==
@@ -170,10 +173,17 @@ for (name, f) in l
170173
UTF8String(['A' + i % 52 for i in 1:(div(Base.SZ_UNBUFFERED_IO,2))]),
171174
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO -1)]),
172175
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO )]),
173-
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO +1)]),
174-
UTF8String(['A' + i % 52 for i in 1:(7 + Base.SZ_UNBUFFERED_IO *3)])
176+
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO +1)])
175177
]
176178

179+
verbose && println("$name readall...")
180+
@test readall(io()) == text
181+
cleanup()
182+
183+
verbose && println("$name readbytes...")
184+
@test readbytes(io()) == Vector{UInt8}(text)
185+
cleanup()
186+
177187
verbose && println("$name readbytes!...")
178188
l = length(text)
179189
for n = [1, 2, l-2, l-1, l, l+1, l+2]
@@ -202,18 +212,31 @@ for (name, f) in l
202212

203213
cleanup()
204214

215+
verbose && println("$name readuntil...")
216+
@test readuntil(io(), '\n') == readuntil(IOBuffer(text),'\n')
217+
cleanup()
218+
@test readuntil(io(), "\n") == readuntil(IOBuffer(text),"\n")
219+
cleanup()
220+
@test readuntil(io(), ',') == readuntil(IOBuffer(text),',')
221+
cleanup()
222+
205223
verbose && println("$name readline...")
206224
@test readline(io()) == readline(IOBuffer(text))
225+
cleanup()
207226

208227
verbose && println("$name readlines...")
209228
@test readlines(io()) == readlines(IOBuffer(text))
229+
cleanup()
210230
@test collect(eachline(io())) == collect(eachline(IOBuffer(text)))
231+
cleanup()
211232

212233
verbose && println("$name countlines...")
213234
@test countlines(io()) == countlines(IOBuffer(text))
235+
cleanup()
214236

215237
verbose && println("$name readcsv...")
216238
@test readcsv(io()) == readcsv(IOBuffer(text))
239+
cleanup()
217240
end
218241

219242
text = old_text

0 commit comments

Comments
 (0)