Skip to content

Commit

Permalink
fix eof definitions and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Jan 14, 2016
1 parent 99e292a commit a0b5414
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
10 changes: 5 additions & 5 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ nb_available(s::LibuvStream) = nb_available(s.buffer)

function eof(s::LibuvStream)
if isopen(s) # fast path
nb_available(s) > 0 && return true
nb_available(s) > 0 && return false
else
return nb_available(s) <= 0
end
Expand Down Expand Up @@ -1232,17 +1232,17 @@ write(s::BufferStream, c::Char) = write(s, string(c))
function write{T}(s::BufferStream, a::Array{T})
rv=write(s.buffer, a)
!(s.buffer_writes) && notify(s.r_c; all=true);
rv
return rv
end
function write(s::BufferStream, p::Ptr, nb::Integer)
rv=write(s.buffer, p, nb)
!(s.buffer_writes) && notify(s.r_c; all=true);
rv
return rv
end

function eof(s::LibuvStream)
function eof(s::BufferStream)
wait_readnb(s,1)
!isopen(s) && nb_available(s)<=0
return !isopen(s) && nb_available(s)<=0
end

# If buffer_writes is called, it will delay notifying waiters till a flush is called.
Expand Down
6 changes: 6 additions & 0 deletions test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,22 @@ let bstream = BufferStream()
@test isopen(bstream)
@test isreadable(bstream)
@test iswritable(bstream)
@test nb_available(bstream) == 0
@test sprint(io -> show(io,bstream)) == "BufferStream() bytes waiting:$(nb_available(bstream.buffer)), isopen:true"
a = rand(UInt8,10)
write(bstream,a)
@test !eof(bstream)
flush(bstream)
b = read(bstream,UInt8)
@test a[1] == b
b = read(bstream,UInt8)
@test a[2] == b
c = zeros(UInt8,8)
@test nb_available(bstream) == 8
@test !eof(bstream)
read!(bstream,c)
@test c == a[3:10]
close(bstream)
@test eof(bstream)
@test nb_available(bstream) == 0
end
21 changes: 21 additions & 0 deletions test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,24 @@ let
end
[close(s) for s in [a, b, c]]
end

let P = Pipe()
Base.link_pipe(P)
write(P, "hello")
@test nb_available(P) == 0
@test !eof(P)
@test read(P, Char) === 'h'
@test !eof(P)
@test read(P, Char) === 'e'
@test isopen(P)
close(P.in)
@test isopen(P)
@test !eof(P)
@test readuntil(P, 'o') == "llo"
@test isopen(P)
@test eof(P)
@test !isopen(P)
close(P)
@test !isopen(P)
@test eof(P)
end

1 comment on commit a0b5414

@tkelman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. x-ref #14667, should be backported at same time @JuliaBackports

Please sign in to comment.