Skip to content

Commit

Permalink
Another fix for connection interrupted/resumed. Add test. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Octogonapus authored Aug 9, 2024
1 parent 9d41512 commit a3f33e8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AWSCRT"
uuid = "df31ea59-17a4-4ebd-9d69-4f45266dc2c7"
version = "0.4.0"
version = "0.4.1"

[deps]
CountDownLatches = "621fb831-fdad-4fff-93ac-1af7b7ed19e3"
Expand Down
9 changes: 7 additions & 2 deletions src/AWSMQTT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function _c_on_connection_interrupted(

data = Base.unsafe_pointer_to_objref(userdata)::_OnConnectionInterruptedUserData
try
put!(data.ch, _OnConnectionInterruptedEvent(data.callback, error_code))
put!(data.ch, _OnConnectionInterruptedEvent(data.callback, data.conn, error_code))
catch ex
if ex isa InvalidStateException && ex.state == :closed
else
Expand Down Expand Up @@ -265,7 +265,12 @@ function _c_on_connection_resumed(
try
put!(
data.ch,
_OnConnectionResumedEvent(data.callback, aws_mqtt_connect_return_code(return_code), session_present != 0),
_OnConnectionResumedEvent(
data.callback,
data.conn,
aws_mqtt_connect_return_code(return_code),
session_present != 0,
),
)
catch ex
if ex isa InvalidStateException && ex.state == :closed
Expand Down
38 changes: 38 additions & 0 deletions test/interrupt_connection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@testset "interrupt and resume connection" begin
topic1 = "test-topic-$(Random.randstring(6))"
payload1 = Random.randstring(48)
client_id1 = random_client_id()
@show topic1 payload1 client_id1

client = MQTTClient(new_tls_ctx())
connection = MQTTConnection(client)

interruptions = Threads.Atomic{Int}(0)
resumes = Threads.Atomic{Int}(0)

task = connect(
connection,
ENV["ENDPOINT"],
8883,
client_id1;
will = Will(topic1, AWS_MQTT_QOS_AT_LEAST_ONCE, "The client has gone offline!", false),
on_connection_interrupted = (conn, error_code) -> begin
Threads.atomic_add!(interruptions, 1)
@warn "connection interrupted" error_code
end,
on_connection_resumed = (conn, return_code, session_present) -> begin
Threads.atomic_add!(resumes, 1)
@info "connection resumed" return_code session_present
end,
)
@test fetch(task) == Dict(:session_present => false)

# kill the MQTT connection
run(`sudo ss -K dport = 8883`)

wait_for(() -> resumes[] > 0, Timer(30))
@test interruptions[] == 1
@test resumes[] == 1

disconnect(connection)
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ end)
Aqua.test_all(AWSCRT, ambiguities = false)
Aqua.test_ambiguities(AWSCRT)
end
@testset "interrupt_connection.jl" begin
# running this in parallel will kill the connections used by other tests so don't do that
@info "Starting interrupt_connection.jl"
include("interrupt_connection.jl")
end
end
@testset "mqtt_test.jl" begin
@info "Starting mqtt_test.jl"
Expand Down

2 comments on commit a3f33e8

@Octogonapus
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112794

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.1 -m "<description of version>" a3f33e8af0f0016bd37d70fc9c9ab427269f9a1a
git push origin v0.4.1

Please sign in to comment.