Skip to content

Commit

Permalink
style: fix lint issues and move test to client_mqtt5.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jun 18, 2024
1 parent f1bb034 commit e883a3d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 105 deletions.
169 changes: 64 additions & 105 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Testing dependencies
*/
import { assert } from 'chai'
import sinon, { SinonSpy } from 'sinon'
import sinon from 'sinon'
import fs from 'fs'
import levelStore from 'mqtt-level-store'
import Store from '../src/lib/store'
Expand Down Expand Up @@ -474,7 +474,7 @@ export default function abstractTest(server, config, ports) {
// fake a port
const client = connect({ reconnectPeriod: 20, port: 4557 })

client.on('error', () => { })
client.on('error', () => {})

client.on('offline', () => {
client.end(true, done)
Expand Down Expand Up @@ -1389,7 +1389,7 @@ export default function abstractTest(server, config, ports) {

it(
'should silently ignore errors thrown by `handleMessage` and return when no callback is passed ' +
'into `handlePublish` method',
'into `handlePublish` method',
function _test(t, done) {
const client = connect()

Expand Down Expand Up @@ -1643,7 +1643,7 @@ export default function abstractTest(server, config, ports) {

it(
'should silently ignore errors thrown by `handleMessage` and return when no callback is passed ' +
'into `handlePubrel` method',
'into `handlePubrel` method',
function _test(t, done) {
const store = new Store()
const client = connect({ incomingStore: store })
Expand Down Expand Up @@ -1697,7 +1697,7 @@ export default function abstractTest(server, config, ports) {
const server2 = serverBuilder(config.protocol, (serverClient) => {
// errors are not interesting for this test
// but they might happen on some platforms
serverClient.on('error', () => { })
serverClient.on('error', () => {})

serverClient.on('connect', (packet) => {
const connack =
Expand Down Expand Up @@ -2058,63 +2058,64 @@ export default function abstractTest(server, config, ports) {
})

const reschedulePing = (reschedulePings: boolean) => {
it(`should ${!reschedulePings ? 'not ' : ''
}reschedule pings if publishing at a higher rate than keepalive and reschedulePings===${reschedulePings}`, function _test(t, done) {
const intervalMs = 3000
const client = connect({
keepalive: intervalMs / 1000,
reschedulePings,
})
it(`should ${
!reschedulePings ? 'not ' : ''
}reschedule pings if publishing at a higher rate than keepalive and reschedulePings===${reschedulePings}`, function _test(t, done) {
const intervalMs = 3000
const client = connect({
keepalive: intervalMs / 1000,
reschedulePings,
})

const spyReschedule = sinon.spy(
client,
'_reschedulePing' as any,
)
const spyReschedule = sinon.spy(
client,
'_reschedulePing' as any,
)

let received = 0

client.on('packetreceive', (packet) => {
if (packet.cmd === 'puback') {
process.nextTick(() => {
clock.tick(intervalMs)

received++

if (received === 2) {
if (reschedulePings) {
assert.strictEqual(
spyReschedule.callCount,
received,
)
} else {
assert.strictEqual(
spyReschedule.callCount,
0,
)
}
client.end(true, done)
}
})
let received = 0

clock.tick(1)
}
})
client.on('packetreceive', (packet) => {
if (packet.cmd === 'puback') {
process.nextTick(() => {
clock.tick(intervalMs)

received++

server.once('client', (serverClient) => {
serverClient.on('publish', () => {
// needed to trigger the setImmediate inside server publish listener and send suback
clock.tick(1)
if (received === 2) {
if (reschedulePings) {
assert.strictEqual(
spyReschedule.callCount,
received,
)
} else {
assert.strictEqual(
spyReschedule.callCount,
0,
)
}
client.end(true, done)
}
})
})

client.once('connect', () => {
// reset call count (it's called also on connack)
spyReschedule.resetHistory()
// use qos1 so the puback is received (to reschedule ping)
client.publish('foo', 'bar', { qos: 1 })
client.publish('foo', 'bar', { qos: 1 })
clock.tick(1)
}
})

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

client.once('connect', () => {
// reset call count (it's called also on connack)
spyReschedule.resetHistory()
// use qos1 so the puback is received (to reschedule ping)
client.publish('foo', 'bar', { qos: 1 })
client.publish('foo', 'bar', { qos: 1 })
})
})
}

reschedulePing(true)
Expand Down Expand Up @@ -2444,48 +2445,6 @@ export default function abstractTest(server, config, ports) {
})
})

if (version === 5) {
it('should fire a callback with error for topic it does not have access to', function _test(t, done) {

const server2 = serverBuilder(config.protocol, (serverClient) => {
serverClient.on('connect', (packet) => {
const connack =
version === 5 ? { reasonCode: 0 } : { returnCode: 0 }
serverClient.connack(connack)
})
serverClient.on('subscribe', (packet) => {
serverClient.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map((e) => 135),
})
})
})

server2.listen(ports.PORTAND49, () => {
const client = connect({
...config,
port: ports.PORTAND49,
host: 'localhost',
})

client.subscribe('$SYS/#', (subErr) => {
client.end(true, (endErr) => {
server2.close((err2) => {
if (subErr) {
assert.strictEqual(
subErr.message,
'Subscribe error: Not authorized',
)
return done(err2 || endErr)
}
done(new Error('Suback errors do NOT work'))
})
})
})
})
})
}

it('should fire a callback with error if disconnected (options provided)', function _test(t, done) {
const client = connect()
const topic = 'test'
Expand Down Expand Up @@ -3174,9 +3133,9 @@ export default function abstractTest(server, config, ports) {
const reconnectPeriodDuringTest = end - start
if (
reconnectPeriodDuringTest >=
test.period - reconnectSlushTime &&
test.period - reconnectSlushTime &&
reconnectPeriodDuringTest <=
test.period + reconnectSlushTime
test.period + reconnectSlushTime
) {
// give the connection a 200 ms slush window
done()
Expand Down Expand Up @@ -3320,7 +3279,7 @@ export default function abstractTest(server, config, ports) {

server.once('client', (serverClient) => {
// ignore errors
serverClient.on('error', () => { })
serverClient.on('error', () => {})
serverClient.on('publish', () => {
setImmediate(() => {
serverClient.stream.destroy()
Expand Down Expand Up @@ -3634,7 +3593,7 @@ export default function abstractTest(server, config, ports) {

client.on('connect', () => {
if (!reconnect) {
client.subscribe('test', { qos: 2 }, () => { })
client.subscribe('test', { qos: 2 }, () => {})
reconnect = true
}
})
Expand Down Expand Up @@ -3743,7 +3702,7 @@ export default function abstractTest(server, config, ports) {
client.publish('topic', 'payload', { qos: 1 })
}
})
client.on('error', () => { })
client.on('error', () => {})
})
})

Expand Down Expand Up @@ -3791,7 +3750,7 @@ export default function abstractTest(server, config, ports) {
client.publish('topic', 'payload', { qos: 2 })
}
})
client.on('error', () => { })
client.on('error', () => {})
})
})

Expand Down Expand Up @@ -3853,7 +3812,7 @@ export default function abstractTest(server, config, ports) {
)
}
})
client.on('error', () => { })
client.on('error', () => {})
})
})

Expand All @@ -3867,7 +3826,7 @@ export default function abstractTest(server, config, ports) {
const server2 = serverBuilder(config.protocol, (serverClient) => {
// errors are not interesting for this test
// but they might happen on some platforms
serverClient.on('error', () => { })
serverClient.on('error', () => {})

serverClient.on('connect', (packet) => {
const connack =
Expand Down Expand Up @@ -3933,7 +3892,7 @@ export default function abstractTest(server, config, ports) {
client.publish('topic', 'payload3', { qos: 1 })
}
})
client.on('error', () => { })
client.on('error', () => {})
})
})

Expand Down
34 changes: 34 additions & 0 deletions test/client_mqtt5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,40 @@ describe('MQTT 5.0', () => {
},
)

it('suback handling error codes', function _test(t, done) {
serverThatSendsErrors.listen(ports.PORTAND117)

serverThatSendsErrors.once('client', (serverClient) => {
serverClient.on('subscribe', (packet) => {
serverClient.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map((e) => 135),
})
})
})

const client = mqtt.connect({
protocolVersion: 5,
port: ports.PORTAND117,
host: 'localhost',
})

client.subscribe('$SYS/#', (subErr) => {
client.end(true, (endErr) => {
serverThatSendsErrors.close((err2) => {
if (subErr) {
assert.strictEqual(
subErr.message,
'Subscribe error: Not authorized',
)
return done(err2 || endErr)
}
done(new Error('Suback errors do NOT work'))
})
})
})
})

it(
'server side disconnect',
{
Expand Down

0 comments on commit e883a3d

Please sign in to comment.