Skip to content

Commit

Permalink
Implement isreadable and iswritable (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 authored Mar 24, 2024
1 parent 3c187f4 commit 39409a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions ext/TestExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function TranscodingStreams.test_chunked_read(Encoder, Decoder)
stream = TranscodingStream(Decoder(), buffer, stop_on_end=true)
ok &= hash(read(stream)) == hash(chunk)
ok &= eof(stream)
ok &= isreadable(stream)
close(stream)
end
Test.@test ok
Expand Down
12 changes: 11 additions & 1 deletion src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ julia> using TranscodingStreams
julia> file = open(joinpath(dirname(dirname(pathof(TranscodingStreams))), "README.md"));
julia> stream = NoopStream(file);
julia> stream = TranscodingStream(Noop(), file);
julia> readline(file)
"TranscodingStreams.jl"
Expand Down Expand Up @@ -182,6 +182,16 @@ function Base.isopen(stream::TranscodingStream)
return stream.state.mode != :close && stream.state.mode != :panic
end

function Base.isreadable(stream::TranscodingStream)::Bool
mode = stream.state.mode
(mode === :idle || mode === :read || mode === :stop) && isreadable(stream.stream)
end

function Base.iswritable(stream::TranscodingStream)::Bool
mode = stream.state.mode
(mode === :idle || mode === :write) && iswritable(stream.stream)
end

function Base.close(stream::TranscodingStream)
mode = stream.state.mode
if mode != :panic
Expand Down
28 changes: 18 additions & 10 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,26 @@
TranscodingStreams.test_roundtrip_write(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_lines(NoopStream, NoopStream)

# switch write => read
stream = NoopStream(IOBuffer(b"foobar", read=true, write=true))
@test_throws ArgumentError begin
write(stream, b"xyz")
read(stream, 3)
@testset "switch write => read" begin
stream = NoopStream(IOBuffer(collect(b"foobar"), read=true, write=true))
@test isreadable(stream)
@test iswritable(stream)
@test_throws ArgumentError begin
write(stream, b"xyz")
read(stream, 3)
end
@test !isreadable(stream)
@test iswritable(stream)
end

# switch read => write
stream = NoopStream(IOBuffer(b"foobar", read=true, write=true))
@test_throws ArgumentError begin
read(stream, 3)
write(stream, b"xyz")
@testset "switch read => write" begin
stream = NoopStream(IOBuffer(collect(b"foobar"), read=true, write=true))
@test_throws ArgumentError begin
read(stream, 3)
write(stream, b"xyz")
end
@test isreadable(stream)
@test !iswritable(stream)
end

stream = NoopStream(IOBuffer(""))
Expand Down

0 comments on commit 39409a5

Please sign in to comment.