corked_emitter.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict'
  2. const { test } = require('tap')
  3. const CorkedEmitter = require('../lib/corked_emitter')
  4. function gatherEventSequence (expectedNumber) {
  5. const gatheredEvents = []
  6. let callback
  7. const finished = new Promise(function (resolve) {
  8. callback = function (...args) {
  9. gatheredEvents.push(...args)
  10. if (gatheredEvents.length >= expectedNumber) {
  11. // Prevent result mutation after our promise is resolved:
  12. resolve(gatheredEvents.slice())
  13. }
  14. }
  15. })
  16. return {
  17. finished,
  18. callback
  19. }
  20. }
  21. test('normal emit flow', function (t) {
  22. const emitter = new CorkedEmitter()
  23. const expectedSequence = [
  24. ['searchEntry', { data: 'a' }],
  25. ['searchEntry', { data: 'b' }],
  26. ['end']
  27. ]
  28. const gatherer = gatherEventSequence(3)
  29. emitter.on('searchEntry', function (...args) {
  30. gatherer.callback(['searchEntry', ...args])
  31. })
  32. emitter.on('end', function (...args) {
  33. gatherer.callback(['end', ...args])
  34. })
  35. emitter.emit('searchEntry', { data: 'a' })
  36. emitter.emit('searchEntry', { data: 'b' })
  37. emitter.emit('end')
  38. gatherer.finished.then(function (gatheredEvents) {
  39. expectedSequence.forEach(function (expectedEvent, i) {
  40. t.equal(JSON.stringify(expectedEvent), JSON.stringify(gatheredEvents[i]))
  41. })
  42. t.end()
  43. })
  44. })
  45. test('reversed listener registration', function (t) {
  46. const emitter = new CorkedEmitter()
  47. const expectedSequence = [
  48. ['searchEntry', { data: 'a' }],
  49. ['searchEntry', { data: 'b' }],
  50. ['end']
  51. ]
  52. const gatherer = gatherEventSequence(3)
  53. // This time, we swap the event listener registrations.
  54. // The order of emits should remain unchanged.
  55. emitter.on('end', function (...args) {
  56. gatherer.callback(['end', ...args])
  57. })
  58. emitter.on('searchEntry', function (...args) {
  59. gatherer.callback(['searchEntry', ...args])
  60. })
  61. emitter.emit('searchEntry', { data: 'a' })
  62. emitter.emit('searchEntry', { data: 'b' })
  63. emitter.emit('end')
  64. gatherer.finished.then(function (gatheredEvents) {
  65. expectedSequence.forEach(function (expectedEvent, i) {
  66. t.equal(JSON.stringify(expectedEvent), JSON.stringify(gatheredEvents[i]))
  67. })
  68. t.end()
  69. })
  70. })
  71. test('delayed listener registration', function (t) {
  72. const emitter = new CorkedEmitter()
  73. const expectedSequence = [
  74. ['searchEntry', { data: 'a' }],
  75. ['searchEntry', { data: 'b' }],
  76. ['end']
  77. ]
  78. const gatherer = gatherEventSequence(3)
  79. emitter.emit('searchEntry', { data: 'a' })
  80. emitter.emit('searchEntry', { data: 'b' })
  81. emitter.emit('end')
  82. // The listeners only appear after a brief delay - this simulates
  83. // the situation described in https://github.com/ldapjs/node-ldapjs/issues/602
  84. // and in https://github.com/ifroz/node-ldapjs/commit/5239f6c68827f2c25b4589089c199d15bb882412
  85. setTimeout(function () {
  86. emitter.on('end', function (...args) {
  87. gatherer.callback(['end', ...args])
  88. })
  89. emitter.on('searchEntry', function (...args) {
  90. gatherer.callback(['searchEntry', ...args])
  91. })
  92. }, 50)
  93. gatherer.finished.then(function (gatheredEvents) {
  94. expectedSequence.forEach(function (expectedEvent, i) {
  95. t.equal(JSON.stringify(expectedEvent), JSON.stringify(gatheredEvents[i]))
  96. })
  97. t.end()
  98. })
  99. })