From 8697b06cae3265422620c38b76126381502a9c17 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Thu, 29 Feb 2024 17:17:38 +0100 Subject: [PATCH] fix: add keepalive test in webworker (#1807) * fix: add keepalive test in webworker * fix: possible race condition --- src/mqtt.ts | 2 ++ test/abstract_client.ts | 6 +++--- test/browser/test.js | 8 +++++++- test/browser/worker.js | 22 +++++++++++++++++----- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/mqtt.ts b/src/mqtt.ts index eb84b8ed2..60bca03a7 100644 --- a/src/mqtt.ts +++ b/src/mqtt.ts @@ -9,6 +9,7 @@ import DefaultMessageIdProvider from './lib/default-message-id-provider' import UniqueMessageIdProvider from './lib/unique-message-id-provider' import Store, { IStore } from './lib/store' import connect, { connectAsync } from './lib/connect' +import PingTimer from './lib/PingTimer' export const Client = MqttClient export { @@ -19,6 +20,7 @@ export { DefaultMessageIdProvider, UniqueMessageIdProvider, IStore, + PingTimer, } export * from './lib/client' export * from './lib/shared' diff --git a/test/abstract_client.ts b/test/abstract_client.ts index 78983bc04..64e19d0e2 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -3112,7 +3112,7 @@ export default function abstractTest(server, config, ports) { client.end(true) } }) - let client = connect({ reconnectPeriod: 100 }) + let client = connect({ reconnectPeriod: 200 }) let serverPublished = false let clientCalledBack = false @@ -3125,7 +3125,7 @@ export default function abstractTest(server, config, ports) { }) }) - // after 100ms the client should reconnect + // after 200ms the client should reconnect server.once('client', (serverClientNew) => { serverClientNew.on('publish', () => { serverPublished = true @@ -3195,7 +3195,7 @@ export default function abstractTest(server, config, ports) { } }) - let client = connect({ reconnectPeriod: 100 }) + let client = connect({ reconnectPeriod: 200 }) let serverPublished = false let clientCalledBack = false diff --git a/test/browser/test.js b/test/browser/test.js index c81595ceb..3365f0407 100644 --- a/test/browser/test.js +++ b/test/browser/test.js @@ -94,10 +94,16 @@ describe('MQTT.js browser tests', () => { it('should work in a Web Worker', (done) => { const worker = new Worker('test/browser/worker.js') + let ready = false worker.onmessage = (e) => { if (e.data === 'worker ready') { + ready = true + } else if(e.data === 'keepalive'){ + worker.onerror = null + // worker.terminate() + expect(ready).to.be.true done() - } else { + }else { done(Error(e.data)) } } diff --git a/test/browser/worker.js b/test/browser/worker.js index e3cdcee3b..ef271487e 100644 --- a/test/browser/worker.js +++ b/test/browser/worker.js @@ -1,10 +1,14 @@ importScripts('/dist/mqtt.js'); -/** @type { import('../../src').MqttClient }*/ +/** @type { import('../../src') }*/ const MQTT = mqtt; +console.log('worker start'); +console.log('worker MQTT', MQTT); + const client = MQTT.connect(`ws://localhost:4000`, { clientId: `testClient-worker_` + Math.random().toString(16).substr(2, 8), + keepalive: 2, }); client.on('offline', () => { @@ -19,10 +23,18 @@ client.on('error', (err) => { console.log('worker client error', err); }) +client.on('packetsend', (packet) => { + if (packet.cmd === 'pingreq') { + postMessage('keepalive'); + client.end(() => { + console.log('worker client end'); + + }); + } +}) + client.on('connect', () => { console.log('worker client connect'); - client.end(() => { - console.log('worker client end'); - postMessage('worker ready'); - }); + postMessage('worker ready'); + }) \ No newline at end of file