Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reschedule pings problem #1904

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading