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