keep-alive.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. const { test } = require('tap')
  3. const eos = require('end-of-stream')
  4. const Faketimers = require('@sinonjs/fake-timers')
  5. const { setup, connect, noError } = require('./helper')
  6. const aedes = require('../')
  7. test('supports pingreq/pingresp', function (t) {
  8. t.plan(1)
  9. const s = noError(connect(setup()))
  10. t.teardown(s.broker.close.bind(s.broker))
  11. s.broker.on('keepaliveTimeout', function (client) {
  12. t.fail('keep alive should not timeout')
  13. })
  14. s.inStream.write({
  15. cmd: 'pingreq'
  16. })
  17. s.outStream.on('data', function (packet) {
  18. t.equal(packet.cmd, 'pingresp', 'the response is a pingresp')
  19. })
  20. })
  21. test('supports keep alive disconnections', function (t) {
  22. t.plan(2)
  23. const clock = Faketimers.install()
  24. const s = connect(setup(), { keepalive: 1 })
  25. t.teardown(s.broker.close.bind(s.broker))
  26. s.broker.on('keepaliveTimeout', function (client) {
  27. t.pass('keep alive timeout')
  28. })
  29. eos(s.conn, function () {
  30. t.pass('waits 1 and a half the keepalive timeout')
  31. })
  32. setTimeout(() => {
  33. clock.uninstall()
  34. }, 1.5)
  35. clock.tick(1.5)
  36. })
  37. test('supports keep alive disconnections after a pingreq', function (t) {
  38. t.plan(3)
  39. const clock = Faketimers.install()
  40. const s = connect(setup(), { keepalive: 1 })
  41. t.teardown(s.broker.close.bind(s.broker))
  42. eos(s.conn, function () {
  43. t.pass('waits 1 and a half the keepalive timeout')
  44. })
  45. s.broker.on('keepaliveTimeout', function (client) {
  46. t.pass('keep alive timeout')
  47. })
  48. s.outStream.on('data', function (packet) {
  49. t.equal(packet.cmd, 'pingresp', 'the response is a pingresp')
  50. })
  51. setTimeout(() => {
  52. s.inStream.write({
  53. cmd: 'pingreq'
  54. })
  55. clock.uninstall()
  56. }, 1)
  57. clock.tick(3)
  58. })
  59. test('disconnect if a connect does not arrive in time', function (t) {
  60. t.plan(2)
  61. const clock = Faketimers.install()
  62. const s = setup(aedes({
  63. connectTimeout: 500
  64. }))
  65. t.teardown(s.broker.close.bind(s.broker))
  66. s.client.on('error', function (err) {
  67. t.equal(err.message, 'connect did not arrive in time')
  68. })
  69. eos(s.conn, function () {
  70. t.pass('waits waitConnectTimeout before ending')
  71. })
  72. setTimeout(() => {
  73. clock.uninstall()
  74. }, 1000)
  75. clock.tick(1000)
  76. })