-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: keepalive causes a reconnect loop when connection is lost (#1779)
* fix: keepalive causes a reconnect loop when connection is lost Fixes #1778 * fix: add test
- Loading branch information
1 parent
44a2f2f
commit 3da5e84
Showing
4 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
import mqtt from '.' | ||
|
||
const client = mqtt.connect('mqtt://test.mosquitto.org') | ||
const client = mqtt.connect('mqtt://test.mosquitto.org', { | ||
keepalive: 10, | ||
reconnectPeriod: 15000, | ||
}) | ||
|
||
const testTopic = 'presence' | ||
|
||
client.subscribe(testTopic, (err) => { | ||
if (!err) { | ||
console.log('subscribed to', testTopic) | ||
client.publish(testTopic, 'Hello mqtt', (err2) => { | ||
function publish() { | ||
client.publish( | ||
testTopic, | ||
`Hello mqtt ${new Date().toISOString()}`, | ||
(err2) => { | ||
if (!err2) { | ||
console.log('message published') | ||
} else { | ||
console.error(err2) | ||
} | ||
}) | ||
}, | ||
) | ||
} | ||
|
||
client.subscribe(testTopic, (err) => { | ||
if (!err) { | ||
console.log('subscribed to', testTopic) | ||
} else { | ||
console.error(err) | ||
} | ||
}) | ||
|
||
client.on('message', (topic, message) => { | ||
console.log('received message "%s" from topic "%s"', message, topic) | ||
client.end() | ||
setTimeout(() => { | ||
publish() | ||
}, 2000) | ||
}) | ||
|
||
client.on('error', (err) => { | ||
console.error(err) | ||
}) | ||
|
||
client.on('connect', () => { | ||
console.log('connected') | ||
publish() | ||
}) | ||
|
||
client.on('disconnect', () => { | ||
console.log('disconnected') | ||
}) | ||
|
||
client.on('offline', () => { | ||
console.log('offline') | ||
}) | ||
|
||
client.on('reconnect', () => { | ||
console.log('reconnect') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { afterEach, beforeEach, describe, it } from 'node:test' | ||
import PingTimer from '../src/lib/PingTimer' | ||
import { assert } from 'chai' | ||
import { useFakeTimers, spy } from 'sinon' | ||
|
||
describe('PingTimer', () => { | ||
let clock: sinon.SinonFakeTimers | ||
beforeEach(() => { | ||
clock = useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
clock.restore() | ||
}) | ||
|
||
it('should schedule and clear', () => { | ||
const keepalive = 10 // seconds | ||
const cb = spy() | ||
const pingTimer = new PingTimer(keepalive, cb) | ||
|
||
assert.ok(pingTimer['timer'], 'timer should be created automatically') | ||
|
||
clock.tick(keepalive * 1000 + 1) | ||
assert.equal( | ||
cb.callCount, | ||
1, | ||
'should trigger the callback after keepalive seconds', | ||
) | ||
clock.tick(keepalive * 1000 + 1) | ||
assert.equal(cb.callCount, 2, 'should reschedule automatically') | ||
pingTimer.clear() | ||
assert.ok(!pingTimer['timer'], 'timer should not exists after clear()') | ||
}) | ||
|
||
it('should not re-schedule if timer has been cleared in check ping', () => { | ||
const keepalive = 10 // seconds | ||
const cb = spy() | ||
const pingTimer = new PingTimer(keepalive, () => { | ||
pingTimer.clear() | ||
cb() | ||
}) | ||
|
||
clock.tick(keepalive * 1000 + 1) | ||
assert.equal( | ||
cb.callCount, | ||
1, | ||
'should trigger the callback after keepalive seconds', | ||
) | ||
clock.tick(keepalive * 1000 + 1) | ||
assert.equal(cb.callCount, 1, 'should not re-schedule') | ||
assert.ok(!pingTimer['timer'], 'timer should not exists') | ||
}) | ||
}) |