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 5 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
37 changes: 37 additions & 0 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,43 @@

reschedulePing(true)
reschedulePing(false)

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

Check failure on line 2127 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
let client = connect(

Check failure on line 2128 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `⏎↹↹↹↹↹`
{
keepalive: intervalMs / 1000,

Check failure on line 2130 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
reschedulePings,

Check failure on line 2131 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
}

Check failure on line 2132 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `↹}⏎↹↹↹↹` with `}`
)

Check failure on line 2134 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

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

Check failure on line 2136 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

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

Check failure on line 2146 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
client.on('error', (err) => {

Check failure on line 2147 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹↹↹`
client.end(true, () => {
done(err)
})
})

Check failure on line 2152 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

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

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