Skip to content

Commit

Permalink
fix: add keepalive test in webworker (#1807)
Browse files Browse the repository at this point in the history
* fix: add keepalive test in webworker

* fix: possible race condition
  • Loading branch information
robertsLando authored Feb 29, 2024
1 parent 8334aae commit 8697b06
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,6 +20,7 @@ export {
DefaultMessageIdProvider,
UniqueMessageIdProvider,
IStore,
PingTimer,
}
export * from './lib/client'
export * from './lib/shared'
Expand Down
6 changes: 3 additions & 3 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
22 changes: 17 additions & 5 deletions test/browser/worker.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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');

})

0 comments on commit 8697b06

Please sign in to comment.