Skip to content

Commit

Permalink
fix(client) first_connect was not properly updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Nov 4, 2021
1 parent 01a48b6 commit 2723220
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion mqtt/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ function client_mt:__init(opts)
self._to_remove_handlers = {}

-- state
self.first_connect = true -- contains true to perform one network connection attemt after client creation
self.send_time = 0 -- time of the last network send from client side
self.first_connect = true -- contains true to perform one network connection attempt after client creation
-- Note: remains true, during the connect process. False after succes or failure.

-- packet creation/parse functions according version
if not a.version then
Expand Down Expand Up @@ -697,12 +698,14 @@ function client_mt:start_connecting()
-- open network connection
local ok, err = self:open_connection()
if not ok then
self.first_connect = false
return false, err
end

-- send CONNECT packet
ok, err = self:send_connect()
if not ok then
self.first_connect = false
return false, err
end

Expand Down Expand Up @@ -972,6 +975,7 @@ function client_mt:handle_received_packet(packet)
log:error("client '%s' %s", self.opts.id, err)
self:handle("error", err, self)
self:close_connection("error")
self.first_connect = false
return false, err
end

Expand All @@ -985,13 +989,15 @@ function client_mt:handle_received_packet(packet)
self:handle("error", err, self, packet)
self:handle("connect", packet, self)
self:close_connection("connection failed")
self.first_connect = false
return false, err
end

log:info("client '%s' connected successfully to '%s'", self.opts.id, conn.uri)

-- fire connect event
self:handle("connect", packet, self)
self.first_connect = false
else
-- connection authorized, so process usual packets

Expand Down
2 changes: 1 addition & 1 deletion mqtt/ioloop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function ioloop_mt:iteration()
-- an error from a client was returned
if not client.opts.reconnect then
-- error and not reconnecting, remove the client
log:fatal("client '%s' failed with '%s', will not re-connect", client.opts.id, err)
log:info("client '%s' returned '%s', no re-connect set, removing client", client.opts.id, err)
self:remove(client)
t = opts.sleep_max
else
Expand Down
10 changes: 10 additions & 0 deletions tests/spec/06-mqtt-client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ describe("last will message", function()

local function send_self_destroy()
if not client1_ready or not client2_ready then
log:warn("not self destroying, clients not ready")
return
end
log:warn("client1 publishing 'self-destructing-message' to '.../stop' topic")
assert(client1:publish{
topic = prefix.."/stop",
payload = "self-destructing-message",
Expand All @@ -367,13 +369,17 @@ describe("last will message", function()
client1:on{
connect = function()
-- subscribe, then send self-destructing message
log:warn("client1 is now connected")
log:warn("client1 subscribing to '.../stop' topic")
assert(client1:subscribe{topic=prefix.."/stop", callback=function()
client1_ready = true
log:warn("client1 subscription to '.../stop' topic confirmed, client 1 is ready for self destruction")
send_self_destroy()
end})
end,
message = function()
-- break connection with broker on any message
log:warn("client1 received a message and is now closing its connection")
client1:close_connection("self-destructed")
end,
}
Expand All @@ -383,13 +389,17 @@ describe("last will message", function()
client2:on{
connect = function()
-- subscribe to will-message topic
log:warn("client2 is now connected")
log:warn("client2 subscribing to will-topic: '.../willtest' topic")
assert(client2:subscribe{topic=will_topic, callback=function()
client2_ready = true
log:warn("client2 subscription to will-topic '.../willtest' confirmed, client 2 is ready for self destruction")
send_self_destroy()
end})
end,
message = function(msg)
will_received = msg.topic == will_topic
log:warn("client2 received a message, topic is: '%s', client 2 is now closing its connection",tostring(msg.topic))
client2:disconnect()
end,
}
Expand Down

0 comments on commit 2723220

Please sign in to comment.