Skip to content

Commit

Permalink
fix: do not shift pings on 'publish' packets (#1866)
Browse files Browse the repository at this point in the history
* fix: do not shift pings on 'publish' packets

Fixes #1863 #1861

* fix: tests
  • Loading branch information
robertsLando committed May 13, 2024
1 parent 6a03d29 commit e4d4663
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lib/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const handle: PacketHandler = (client, packet, done) => {
client.pingResp = Date.now()

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

Expand Down
45 changes: 37 additions & 8 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2004,19 +2004,48 @@ export default function abstractTest(server, config, ports) {

it('should not shift ping on publish', function _test(t, done) {
const intervalMs = 3000

const client = connect({ keepalive: intervalMs / 1000 })

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

client.once('connect', () => {
client.publish('foo', 'bar')
clock.tick(intervalMs)
let serverClient

function fakePub() {
client.publish('foo', 'bar')
clock.tick(intervalMs)
serverClient.publish({
topic: 'foo',
payload: 'bar',
})
clock.tick(1)
}

assert.strictEqual(spy.callCount, 2)
client.end(true, done)
server.once('client', (_serverClient) => {
// send fake packet to client
serverClient = _serverClient

serverClient.on('publish', () => {
// needed to trigger the setImmediate inside server publish listener and send suback
clock.tick(1)
})
})

let received = 0

client.on('packetreceive', (packet) => {
if (packet.cmd === 'publish') {
clock.tick(intervalMs)
received++
assert.strictEqual(spy.callCount, 0)
if (received === 2) {
client.end(true, done)
}
}
})

client.once('connect', () => {
fakePub()
fakePub()
})
})

Expand Down

0 comments on commit e4d4663

Please sign in to comment.