purge-abandoned.test.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict'
  2. const { test } = require('tap')
  3. const { MAX_MSGID } = require('../../../../lib/client/constants')
  4. const purgeAbandoned = require('../../../../lib/client/message-tracker/purge-abandoned')
  5. test('clears queue if only one message present', async t => {
  6. t.plan(3)
  7. const abandoned = new Map()
  8. abandoned.set(1, { age: 2, cb })
  9. purgeAbandoned(2, abandoned)
  10. t.equal(abandoned.size, 0)
  11. function cb (err) {
  12. t.equal(err.name, 'AbandonedError')
  13. t.equal(err.message, 'client request abandoned')
  14. }
  15. })
  16. test('clears queue if multiple messages present', async t => {
  17. t.plan(5)
  18. const abandoned = new Map()
  19. abandoned.set(1, { age: 2, cb })
  20. abandoned.set(2, { age: 3, cb })
  21. purgeAbandoned(4, abandoned)
  22. t.equal(abandoned.size, 0)
  23. function cb (err) {
  24. t.equal(err.name, 'AbandonedError')
  25. t.equal(err.message, 'client request abandoned')
  26. }
  27. })
  28. test('message id has wrappred around', async t => {
  29. t.plan(3)
  30. const abandoned = new Map()
  31. abandoned.set(MAX_MSGID - 1, { age: MAX_MSGID, cb })
  32. // The "abandon" message was sent with an id of "MAX_MSGID". So the message
  33. // that is triggering the purge was the "first" message in the new sequence
  34. // of message identifiers.
  35. purgeAbandoned(1, abandoned)
  36. t.equal(abandoned.size, 0)
  37. function cb (err) {
  38. t.equal(err.name, 'AbandonedError')
  39. t.equal(err.message, 'client request abandoned')
  40. }
  41. })
  42. test('does not clear if window not met', async t => {
  43. t.plan(1)
  44. const abandoned = new Map()
  45. abandoned.set(1, { age: 2, cb })
  46. purgeAbandoned(1, abandoned)
  47. t.equal(abandoned.size, 1)
  48. function cb () {
  49. t.fail('should not be invoked')
  50. }
  51. })