Skip to content

Commit

Permalink
fix: correct test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jun 18, 2024
1 parent f06c0d5 commit f1bb034
Showing 1 changed file with 104 additions and 94 deletions.
198 changes: 104 additions & 94 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
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', () => { })

Check failure on line 477 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `·`

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',

Check failure on line 1392 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Insert `↹`
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',

Check failure on line 1646 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Insert `↹`
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', () => { })

Check failure on line 1700 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `·`

serverClient.on('connect', (packet) => {
const connack =
Expand Down Expand Up @@ -2058,64 +2058,63 @@ 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,
})

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

let received = 0

client.on('packetreceive', (packet) => {
if (packet.cmd === 'puback') {
process.nextTick(() => {
clock.tick(intervalMs)
it(`should ${!reschedulePings ? 'not ' : ''

Check failure on line 2061 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Insert `⏎↹↹↹↹`
}reschedule pings if publishing at a higher rate than keepalive and reschedulePings===${reschedulePings}`, function _test(t, done) {

Check failure on line 2062 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
const intervalMs = 3000

Check failure on line 2063 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
const client = connect({

Check failure on line 2064 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
keepalive: intervalMs / 1000,

Check failure on line 2065 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
reschedulePings,

Check failure on line 2066 in test/abstract_client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Delete `↹`
})

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

if (received === 2) {
if (reschedulePings) {
assert.strictEqual(
spyReschedule.callCount,
received,
)
} else {
assert.strictEqual(
spyReschedule.callCount,
0,
)
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)
}
client.end(true, done)
}
})
})

clock.tick(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)
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 })
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 @@ -2445,6 +2444,48 @@ 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 @@ -3133,9 +3174,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 @@ -3279,7 +3320,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 @@ -3593,7 +3634,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 @@ -3702,7 +3743,7 @@ export default function abstractTest(server, config, ports) {
client.publish('topic', 'payload', { qos: 1 })
}
})
client.on('error', () => {})
client.on('error', () => { })
})
})

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

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

Expand All @@ -3826,7 +3867,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 @@ -3892,7 +3933,7 @@ export default function abstractTest(server, config, ports) {
client.publish('topic', 'payload3', { qos: 1 })
}
})
client.on('error', () => {})
client.on('error', () => { })
})
})

Expand Down Expand Up @@ -3975,37 +4016,6 @@ export default function abstractTest(server, config, ports) {
})
})

it('should return an error (via callbacks) for topic it does not have access to', function _test(t, done) {
const client = connect({ ...config })

server.on('client', (serverClient) => {
serverClient.on('connect', () => {
serverClient.connack(connack)
})

serverClient.on('subscribe', (packet) => {
// Send an unauthorized error
serverClient.suback({
granted: [0x87],
messageId: packet.messageId,
})
})
})

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

it('should resubscribe even if disconnect is before suback', function _test(t, done) {
const client = connect({ reconnectPeriod: 100, ...config })
let subscribeCount = 0
Expand Down

0 comments on commit f1bb034

Please sign in to comment.