-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setindex! fix and test updates (#174)
* 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
Showing
2 changed files
with
78 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |