Skip to content

Commit e4d4663

Browse files
authored
fix: do not shift pings on 'publish' packets (#1866)
* fix: do not shift pings on 'publish' packets Fixes #1863 #1861 * fix: tests
1 parent 6a03d29 commit e4d4663

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/lib/handlers/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const handle: PacketHandler = (client, packet, done) => {
2626
client.pingResp = Date.now()
2727

2828
// do not shift on pingresp otherwise we would skip the pingreq sending
29-
if (packet.cmd !== 'pingresp') {
29+
if (!['pingresp', 'publish'].includes(packet.cmd)) {
3030
client['_shiftPingInterval']()
3131
}
3232

test/abstract_client.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -2004,19 +2004,48 @@ export default function abstractTest(server, config, ports) {
20042004

20052005
it('should not shift ping on publish', function _test(t, done) {
20062006
const intervalMs = 3000
2007+
20072008
const client = connect({ keepalive: intervalMs / 1000 })
20082009

2009-
const spy = sinon.spy()
2010-
client['_checkPing'] = spy
2010+
const spy = sinon.spy(client, '_reschedulePing' as any)
20112011

2012-
client.once('connect', () => {
2013-
client.publish('foo', 'bar')
2014-
clock.tick(intervalMs)
2012+
let serverClient
2013+
2014+
function fakePub() {
20152015
client.publish('foo', 'bar')
2016-
clock.tick(intervalMs)
2016+
serverClient.publish({
2017+
topic: 'foo',
2018+
payload: 'bar',
2019+
})
2020+
clock.tick(1)
2021+
}
20172022

2018-
assert.strictEqual(spy.callCount, 2)
2019-
client.end(true, done)
2023+
server.once('client', (_serverClient) => {
2024+
// send fake packet to client
2025+
serverClient = _serverClient
2026+
2027+
serverClient.on('publish', () => {
2028+
// needed to trigger the setImmediate inside server publish listener and send suback
2029+
clock.tick(1)
2030+
})
2031+
})
2032+
2033+
let received = 0
2034+
2035+
client.on('packetreceive', (packet) => {
2036+
if (packet.cmd === 'publish') {
2037+
clock.tick(intervalMs)
2038+
received++
2039+
assert.strictEqual(spy.callCount, 0)
2040+
if (received === 2) {
2041+
client.end(true, done)
2042+
}
2043+
}
2044+
})
2045+
2046+
client.once('connect', () => {
2047+
fakePub()
2048+
fakePub()
20202049
})
20212050
})
20222051

0 commit comments

Comments
 (0)