Skip to content

Commit 852d98a

Browse files
committed
fix: reset ping timers on connect
1 parent 3895873 commit 852d98a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/mqtt/client.rb

+4
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ def connect(clientid = nil)
266266
# Receive response
267267
receive_connack
268268

269+
# Reset ping stopwatch
270+
@last_ping_request = Time.now
271+
@last_ping_response = Time.now
272+
269273
# Start packet reading thread
270274
@read_thread = Thread.new(Thread.current) do |parent|
271275
Thread.current[:parent] = parent

spec/mqtt_client_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@
284284
client.connect('myclient')
285285
end
286286

287+
it "should reset @last_ping_request and @last_ping_response" do
288+
frozen_time = Time.now
289+
expect(Time).to receive(:now).exactly(2).and_return(frozen_time)
290+
client.instance_variable_set('@last_ping_request', Time.at(0))
291+
client.instance_variable_set('@last_ping_response', Time.at(0))
292+
client.connect('myclient')
293+
expect(client.instance_variable_get('@last_ping_request')).to eq(frozen_time)
294+
expect(client.instance_variable_get('@last_ping_response')).to eq(frozen_time)
295+
end
296+
287297
context "protocol version 3.1.0" do
288298
it "should write a valid CONNECT packet to the socket if not connected" do
289299
client.version = '3.1.0'

0 commit comments

Comments
 (0)