Skip to content

Commit

Permalink
Add moar tests and fix isfreed() (#245)
Browse files Browse the repository at this point in the history
* Fix definition of isfreed()

* Add some more Message tests

Also changed the `SubString` test to actually use substrings with `@view`.

* fixup! Add some more Message tests
  • Loading branch information
JamesWrigley authored Jul 10, 2024
1 parent 1a3d67c commit d3cf3cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/src/_changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Changelog](https://keepachangelog.com).
- Support for creating [`Message`](@ref)'s from the new `Memory` type in Julia
1.11 ([#244]).

### Fixed
- Fixed [`isfreed()`](@ref), which would previously return the wrong values
([#245]).

## [v1.2.6] - 2024-06-13

### Added
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Message(::String)
Message(::SubString{String})
Message(::Array)
Message(::IOBuffer)
isfreed(::Message)
```

## Context
Expand Down
10 changes: 7 additions & 3 deletions src/message.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ mutable struct Message <: AbstractArray{UInt8,1}
end
end

# check whether zeromq has called our free-function, i.e. whether
# we are save to reclaim ownership of any buffer object
isfreed(m::Message) = haskey(gc_protect, getfield(m, :handle))
"""
isfreed(m::Message)
Check whether zeromq has called our free-function, i.e. whether we are safe to
reclaim ownership of any buffer object the [`Message`](@ref) was created with.
"""
isfreed(m::Message) = !haskey(gc_protect, getfield(m, :handle))

# AbstractArray behaviors:
Base.similar(a::Message, ::Type{T}, dims::Dims) where {T} = Array{T}(undef, dims) # ?
Expand Down
17 changes: 15 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ end
@test !isopen(s2)
end

# Test all the send constructors
@testset "Message" begin
# Test all the send constructors
s1 = Socket(PUB)
s2 = Socket(SUB)
ZMQ.subscribe(s2, "")
Expand Down Expand Up @@ -170,7 +170,7 @@ end
@test String(ZMQ.recv(s2)) == str_msg

# Message(::SubString)
m4 = Message(str_msg[1:3])
m4 = Message(SubString(str_msg, 1:3))
ZMQ.send(s1, m4)
@test String(ZMQ.recv(s2)) == str_msg[1:3]

Expand All @@ -187,8 +187,21 @@ end
ZMQ.send(s1, m6)
@test ZMQ.recv(s2) == buffer3

close(iobuf)
@test_throws ErrorException Message(iobuf)

close(s1)
close(s2)

# Test indexing
m = Message(10)
@test_throws BoundsError m[0]
@test_throws BoundsError m[11]

# Smoke tests
@test !Bool(m.more)
@test_throws ErrorException m.more = true
@test ZMQ.isfreed(m)
end

@testset "ZMQ resource management" begin
Expand Down

0 comments on commit d3cf3cb

Please sign in to comment.