Skip to content

Commit

Permalink
fix: reschedule pings problem (#1904)
Browse files Browse the repository at this point in the history
* reschedulePingForcely

* reset KeepalivTimer if client receives pingresp

* test for pingresp

* reschedulePing with force arg

* call reschedulePing with true

* npm run lint-fix

---------

Co-authored-by: kksuke <[email protected]>
  • Loading branch information
k-keisuke and kksuke authored Jul 18, 2024
1 parent fbe5294 commit 8e14d3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2109,11 +2109,11 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
/**
* Reschedule the ping interval
*/
public reschedulePing() {
public reschedulePing(force = false) {
if (
this.keepaliveManager &&
this.options.keepalive &&
this.options.reschedulePings
(force || this.options.reschedulePings)
) {
this._reschedulePing()
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const handle: PacketHandler = (client, packet, done) => {
break
case 'pingresp':
client.log('_handlePacket :: received pingresp')
client.reschedulePing()
client.reschedulePing(true)
done()
break
case 'disconnect':
Expand Down
35 changes: 35 additions & 0 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,41 @@ export default function abstractTest(server, config, ports) {

reschedulePing(true)
reschedulePing(false)

const pingresp = (reschedulePings: boolean) => {
it(`should shift ping on pingresp when reschedulePings===${reschedulePings}`, function _test(t, done) {
const intervalMs = 3000

let client = connect({
keepalive: intervalMs / 1000,
reschedulePings,
})

const spy = sinon.spy(client, '_reschedulePing' as any)

client.on('packetreceive', (packet) => {
if (packet.cmd === 'pingresp') {
process.nextTick(() => {
assert.strictEqual(spy.callCount, 1)
client.end(true, done)
client = null
})
}
})

client.on('error', (err) => {
client.end(true, () => {
done(err)
})
})

client.once('connect', () => {
clock.tick(intervalMs)
})
})
}
pingresp(true)
pingresp(false)
})

describe('pinging', () => {
Expand Down

0 comments on commit 8e14d3e

Please sign in to comment.