Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush buffer to avoid deleting data when seeking NoopStream #172

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/noop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,30 @@ function Base.position(stream::NoopStream)
end

function Base.seek(stream::NoopStream, pos::Integer)
mode = stream.state.mode
if mode === :write
flushbuffer(stream)
end
seek(stream.stream, pos)
initbuffer!(stream.buffer1)
return stream
end

function Base.seekstart(stream::NoopStream)
mode = stream.state.mode
if mode === :write
flushbuffer(stream)
end
seekstart(stream.stream)
initbuffer!(stream.buffer1)
return stream
end

function Base.seekend(stream::NoopStream)
mode = stream.state.mode
if mode === :write
flushbuffer(stream)
end
seekend(stream.stream)
initbuffer!(stream.buffer1)
return stream
Expand Down
28 changes: 19 additions & 9 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,19 +335,29 @@
end
end

@testset "seekstart doesn't delete data" begin
@testset "seek doesn't delete data" begin
sink = IOBuffer()
stream = NoopStream(sink, bufsize=16)
write(stream, "x")
# seekstart must not delete user data even if it errors.
try
seekstart(stream)
catch e
e isa ArgumentError || rethrow()
end
write(stream, TranscodingStreams.TOKEN_END)
seekstart(stream)
flush(stream)
@test_broken take!(sink) == b"x"
@test take!(sink) == b"x"
close(stream)

op_expected = [
(seekstart, b"dbc"),
(seekend, b"abcd"),
(Base.Fix2(seek, 1), b"adc"),
]
@testset "$op" for (op, expected) in op_expected
sink = IOBuffer()
stream = NoopStream(sink, bufsize=16)
write(stream, "abc")
@test op(stream) === stream
write(stream, "d")
flush(stream)
@test take!(sink) == expected
close(stream)
end
end
end
25 changes: 16 additions & 9 deletions test/codecquadruple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,22 @@ end
end

@testset "seekstart doesn't delete data" begin
sink = IOBuffer()
stream = TranscodingStream(QuadrupleCodec(), sink, bufsize=16)
write(stream, "x")
# seekstart must not delete user data even if it errors.
@test_throws ArgumentError seekstart(stream)
write(stream, TranscodingStreams.TOKEN_END)
flush(stream)
@test take!(sink) == b"xxxx"
close(stream)
for n in 0:3
sink = IOBuffer()
# wrap stream in NoopStream n times.
stream = foldl(
(s,_) -> NoopStream(s),
1:n;
init=TranscodingStream(QuadrupleCodec(), sink, bufsize=16)
)
write(stream, "x")
# seekstart must not delete user data even if it errors.
@test_throws ArgumentError seekstart(stream)
write(stream, TranscodingStreams.TOKEN_END)
flush(stream)
@test take!(sink) == b"xxxx"
close(stream)
end
end

@testset "eof is true after write" begin
Expand Down
Loading