Skip to content

Commit

Permalink
handle exceptions when writing frames
Browse files Browse the repository at this point in the history
  • Loading branch information
erikedin committed Mar 4, 2018
1 parent 1305a08 commit c010b85
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/Proxy/writer_proxy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ struct WriterProxy <: IO
end

function run_writerproxy(w::WriterProxy)
for writerequest in w.channel
if writerequest == CloseSocket()
close(w.writer)
break
try
for writerequest in w.channel
if writerequest == CloseSocket()
break
end
write(w.writer, writerequest.frame)
end
write(w.writer, writerequest.frame)
catch ex
finally
close(w.writer)
end
end

Expand Down
23 changes: 21 additions & 2 deletions test/writer_proxy_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import DandelionWebSockets.Proxy: write, close
struct MockWriter <: IO
channel::Channel{Frame}
closechannel::Channel{Bool}
exception::Nullable{Exception}

MockWriter() = new(Channel{Frame}(0), Channel{Bool}(0))
MockWriter() = new(Channel{Frame}(0), Channel{Bool}(0), Nullable{Exception}())
MockWriter(ex::Exception) = new(Channel{Frame}(0), Channel{Bool}(0), Nullable{Exception}(ex))
end

function write(m::MockWriter, frame::Frame)
if isnull(m.exception)
put!(m.channel, frame)
else
throw(get(m.exception))
end
end

write(m::MockWriter, frame::Frame) = put!(m.channel, frame)
takeframe!(m::MockWriter) = take!(m.channel)
close(m::MockWriter) = put!(m.closechannel, true)
isclosed!(m::MockWriter) = take!(m.closechannel)
Expand Down Expand Up @@ -43,4 +52,14 @@ isclosed!(m::MockWriter) = take!(m.closechannel)

@test isclosed!(mockwriter)
end

@testset "Streams throws exception on write; Socket is closed" begin
writer = MockWriter(EOFError())
proxywriter = WriterProxy(writer)

frame = Frame(true, 0, 0, 0, OPCODE_TEXT, true, 1, 0, Vector{UInt8}(b"\x01\x02\x03\x04"), b"1")
write(proxywriter, frame)

@test isclosed!(writer)
end
end

0 comments on commit c010b85

Please sign in to comment.