Skip to content

Commit

Permalink
Fix position for NoopStream in :write mode (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drvi authored Jul 19, 2022
1 parent 2fac971 commit 38b969d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/noop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ Note that this method may return a wrong position when
- the position of the wrapped stream has been changed outside of this package.
"""
function Base.position(stream::NoopStream)
return position(stream.stream) - buffersize(stream.state.buffer1)
mode = stream.state.mode
@checkmode (:idle, :read, :write)
if mode === :idle
return Int64(0)
elseif mode === :write
return position(stream.stream) + buffersize(stream.state.buffer1)
elseif mode === :read
return position(stream.stream) - buffersize(stream.state.buffer1)
end
@assert false "unreachable"
end

function Base.seek(stream::NoopStream, pos::Integer)
Expand Down Expand Up @@ -126,7 +135,7 @@ function stats(stream::NoopStream)
mode = state.mode
@checkmode (:idle, :read, :write)
buffer = state.buffer1
@assert buffer == stream.state.buffer2
@assert buffer === stream.state.buffer2
if mode == :idle
consumed = supplied = 0
elseif mode == :read
Expand Down
25 changes: 25 additions & 0 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,29 @@
@test_throws ArgumentError NoopStream(let s = IOBuffer(); close(s); s; end)
@test_throws ArgumentError TranscodingStream(Noop(), IOBuffer(), bufsize=0)
@test_throws ArgumentError TranscodingStream(Noop(), IOBuffer(), sharedbuf=true)

@testset "position" begin
iob = IOBuffer()
sink = IOBuffer()
stream = NoopStream(sink, bufsize=16)
@test position(stream) == position(iob)
for len in 0:10:100
write(stream, repeat("x", len))
write(iob, repeat("x", len))
@test position(stream) == position(iob)
end
@test position(stream) == position(sink) == position(iob)
close(stream)
close(iob)

mktemp() do path, sink
stream = NoopStream(sink, bufsize=16)
pos = 0
for len in 0:10:100
write(stream, repeat("x", len))
pos += len
@test position(stream) == pos
end
end
end
end
24 changes: 24 additions & 0 deletions test/codecquadruple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,28 @@ end
stream = TranscodingStream(QuadrupleCodec(), IOBuffer("foo"))
@test_throws EOFError unsafe_read(stream, pointer(Vector{UInt8}(undef, 13)), 13)
close(stream)

@testset "position" begin
iob = IOBuffer()
sink = IOBuffer()
stream = TranscodingStream(QuadrupleCodec(), sink, bufsize=16)
@test position(stream) == position(iob)
for len in 0:10:100
write(stream, repeat("x", len))
write(iob, repeat("x", len))
@test position(stream) == position(iob)
end
close(stream)
close(iob)

mktemp() do path, sink
stream = TranscodingStream(QuadrupleCodec(), sink, bufsize=16)
pos = 0
for len in 0:10:100
write(stream, repeat("x", len))
pos += len
@test position(stream) == pos
end
end
end
end

0 comments on commit 38b969d

Please sign in to comment.