Skip to content

Commit 8697b06

Browse files
authored
fix: add keepalive test in webworker (#1807)
* fix: add keepalive test in webworker * fix: possible race condition
1 parent 8334aae commit 8697b06

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/mqtt.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import DefaultMessageIdProvider from './lib/default-message-id-provider'
99
import UniqueMessageIdProvider from './lib/unique-message-id-provider'
1010
import Store, { IStore } from './lib/store'
1111
import connect, { connectAsync } from './lib/connect'
12+
import PingTimer from './lib/PingTimer'
1213

1314
export const Client = MqttClient
1415
export {
@@ -19,6 +20,7 @@ export {
1920
DefaultMessageIdProvider,
2021
UniqueMessageIdProvider,
2122
IStore,
23+
PingTimer,
2224
}
2325
export * from './lib/client'
2426
export * from './lib/shared'

test/abstract_client.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3112,7 +3112,7 @@ export default function abstractTest(server, config, ports) {
31123112
client.end(true)
31133113
}
31143114
})
3115-
let client = connect({ reconnectPeriod: 100 })
3115+
let client = connect({ reconnectPeriod: 200 })
31163116
let serverPublished = false
31173117
let clientCalledBack = false
31183118

@@ -3125,7 +3125,7 @@ export default function abstractTest(server, config, ports) {
31253125
})
31263126
})
31273127

3128-
// after 100ms the client should reconnect
3128+
// after 200ms the client should reconnect
31293129
server.once('client', (serverClientNew) => {
31303130
serverClientNew.on('publish', () => {
31313131
serverPublished = true
@@ -3195,7 +3195,7 @@ export default function abstractTest(server, config, ports) {
31953195
}
31963196
})
31973197

3198-
let client = connect({ reconnectPeriod: 100 })
3198+
let client = connect({ reconnectPeriod: 200 })
31993199
let serverPublished = false
32003200
let clientCalledBack = false
32013201

test/browser/test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ describe('MQTT.js browser tests', () => {
9494

9595
it('should work in a Web Worker', (done) => {
9696
const worker = new Worker('test/browser/worker.js')
97+
let ready = false
9798
worker.onmessage = (e) => {
9899
if (e.data === 'worker ready') {
100+
ready = true
101+
} else if(e.data === 'keepalive'){
102+
worker.onerror = null
103+
// worker.terminate()
104+
expect(ready).to.be.true
99105
done()
100-
} else {
106+
}else {
101107
done(Error(e.data))
102108
}
103109
}

test/browser/worker.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
importScripts('/dist/mqtt.js');
22

3-
/** @type { import('../../src').MqttClient }*/
3+
/** @type { import('../../src') }*/
44
const MQTT = mqtt;
55

6+
console.log('worker start');
7+
console.log('worker MQTT', MQTT);
8+
69
const client = MQTT.connect(`ws://localhost:4000`, {
710
clientId: `testClient-worker_` + Math.random().toString(16).substr(2, 8),
11+
keepalive: 2,
812
});
913

1014
client.on('offline', () => {
@@ -19,10 +23,18 @@ client.on('error', (err) => {
1923
console.log('worker client error', err);
2024
})
2125

26+
client.on('packetsend', (packet) => {
27+
if (packet.cmd === 'pingreq') {
28+
postMessage('keepalive');
29+
client.end(() => {
30+
console.log('worker client end');
31+
32+
});
33+
}
34+
})
35+
2236
client.on('connect', () => {
2337
console.log('worker client connect');
24-
client.end(() => {
25-
console.log('worker client end');
26-
postMessage('worker ready');
27-
});
38+
postMessage('worker ready');
39+
2840
})

0 commit comments

Comments
 (0)