Skip to content

Commit

Permalink
setindex! fix and test updates (#174)
Browse files Browse the repository at this point in the history
* update tests, fix setindex! on message

* use at-boundscheck macro in getindex and setindex!

* remove broken test

* use @test_broken for repeat close

* cleanups, indenting

* use isa rather than typeof

* finalize test message before closing context
  • Loading branch information
stevengj authored Apr 8, 2018
1 parent 20fab2a commit d05c020
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 84 deletions.
6 changes: 3 additions & 3 deletions src/ZMQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,16 @@ size(zmsg::Message) = (length(zmsg),)
# TODO: change `Any` to `Ref{Message}` when 0.6 support is dropped.
unsafe_convert(::Type{Ptr{UInt8}}, zmsg::Message) = ccall((:zmq_msg_data, libzmq), Ptr{UInt8}, (Any,), zmsg)
function getindex(a::Message, i::Integer)
if i < 1 || i > length(a)
@boundscheck if i < 1 || i > length(a)
throw(BoundsError())
end
unsafe_load(pointer(a), i)
end
function setindex!(a::Message, v, i::Integer)
if i < 1 || i > length(a)
@boundscheck if i < 1 || i > length(a)
throw(BoundsError())
end
unsafe_store(pointer(a), v, i)
unsafe_store!(pointer(a), v, i)
end

# Convert message to string (copies data)
Expand Down
156 changes: 75 additions & 81 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,89 +1,83 @@
using ZMQ, Compat
using Compat.Test

println("Testing with ZMQ version $(ZMQ.version)")
Compat.@info("Testing with ZMQ version $(ZMQ.version)")

ctx=Context()
@testset "ZMQ sockets" begin
ctx=Context()
@test ctx isa Context
ZMQ.close(ctx)

@assert typeof(ctx) == Context
#try to create socket with expired context
@test_throws StateError Socket(ctx, PUB)

ZMQ.close(ctx)

#try to create socket with expired context
try
Socket(ctx, PUB)
@assert false
catch ex
@assert typeof(ex) == StateError
end


ctx2=Context()
s=Socket(ctx2, PUB)
@assert typeof(s) == Socket
ZMQ.close(s)

#trying to close already closed socket
try
ctx2=Context()
s=Socket(ctx2, PUB)
@test s isa Socket
ZMQ.close(s)
catch ex
@assert typeof(ex) == StateError
end


s1=Socket(ctx2, REP)
ZMQ.set_sndhwm(s1, 1000)
ZMQ.set_linger(s1, 1)
ZMQ.set_identity(s1, "abcd")


@assert ZMQ.get_identity(s1)::AbstractString == "abcd"
@assert ZMQ.get_sndhwm(s1)::Integer == 1000
@assert ZMQ.get_linger(s1)::Integer == 1
@assert ZMQ.ismore(s1) == false

s2=Socket(ctx2, REQ)
@assert ZMQ.get_type(s1) == REP
@assert ZMQ.get_type(s2) == REQ

ZMQ.bind(s1, "tcp://*:5555")
ZMQ.connect(s2, "tcp://localhost:5555")

msg = Message("test request")
# Test similar() and copy() fixes in https://github.com/JuliaInterop/ZMQ.jl/pull/165
# Note that we have to send this message to work around
# https://github.com/JuliaInterop/ZMQ.jl/issues/166
@assert similar(msg, UInt8, 12) isa Vector{UInt8}
@assert copy(msg) == convert(Vector{UInt8}, "test request")
ZMQ.send(s2, msg)
@assert (unsafe_string(ZMQ.recv(s1)) == "test request")
ZMQ.send(s1, Message("test response"))
@assert (unsafe_string(ZMQ.recv(s2)) == "test response")

# Test task-blocking behavior
c = Base.Condition()
msg_sent = false
@async begin
global msg_sent
sleep(0.5)
msg_sent = true
ZMQ.send(s2, Message("test request"))
@assert (unsafe_string(ZMQ.recv(s2)) == "test response")
notify(c)
s1=Socket(ctx2, REP)
ZMQ.set_sndhwm(s1, 1000)
ZMQ.set_linger(s1, 1)
ZMQ.set_identity(s1, "abcd")

@test ZMQ.get_identity(s1)::AbstractString == "abcd"
@test ZMQ.get_sndhwm(s1)::Integer == 1000
@test ZMQ.get_linger(s1)::Integer == 1
@test ZMQ.ismore(s1) == false

s2=Socket(ctx2, REQ)
@test ZMQ.get_type(s1) == REP
@test ZMQ.get_type(s2) == REQ

ZMQ.bind(s1, "tcp://*:5555")
ZMQ.connect(s2, "tcp://localhost:5555")

msg = Message("test request")
# Test similar() and copy() fixes in https://github.com/JuliaInterop/ZMQ.jl/pull/165
# Note that we have to send this message to work around
# https://github.com/JuliaInterop/ZMQ.jl/issues/166
@test similar(msg, UInt8, 12) isa Vector{UInt8}
@test copy(msg) == Vector{UInt8}("test request")
ZMQ.send(s2, msg)
@test unsafe_string(ZMQ.recv(s1)) == "test request"
ZMQ.send(s1, Message("test response"))
@test unsafe_string(ZMQ.recv(s2)) == "test response"

# Test task-blocking behavior
c = Base.Condition()
global msg_sent = false
@async begin
global msg_sent
sleep(0.5)
msg_sent = true
ZMQ.send(s2, Message("test request"))
@test (unsafe_string(ZMQ.recv(s2)) == "test response")
notify(c)
end

# This will hang forver if ZMQ blocks the entire process since
# we'll never switch to the other task
@test unsafe_string(ZMQ.recv(s1)) == "test request"
@test msg_sent == true
ZMQ.send(s1, Message("test response"))
wait(c)

ZMQ.send(s2, Message("another test request"))
msg = ZMQ.recv(s1)
o=convert(IOStream, msg)
seek(o, 0)
@test String(take!(o)) == "another test request"

@testset "Message AbstractVector interface" begin
m = Message("1")
@test m[1]==0x31
m[1]=0x32
@test unsafe_string(m)=="2"
finalize(m)
end

ZMQ.close(s1)
ZMQ.close(s2)
ZMQ.close(ctx2)
end

# This will hang forver if ZMQ blocks the entire process since
# we'll never switch to the other task
@assert (unsafe_string(ZMQ.recv(s1)) == "test request")
@assert msg_sent == true
ZMQ.send(s1, Message("test response"))
wait(c)

ZMQ.send(s2, Message("another test request"))
msg = ZMQ.recv(s1)
o=convert(IOStream, msg)
seek(o, 0)
@assert (String(take!(o))=="another test request")

ZMQ.close(s1)
ZMQ.close(s2)
ZMQ.close(ctx2)

0 comments on commit d05c020

Please sign in to comment.