|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | const tape = require('tape')
|
| 3 | +const EventEmitter = require('events').EventEmitter |
3 | 4 | const PauseWrapper = require('./PauseWrapper.js')
|
4 | 5 |
|
5 | 6 | tape('Pause order events', function (t) {
|
@@ -110,3 +111,119 @@ tape('._onResumeCb', function (t) {
|
110 | 111 | stack.push('resume()')
|
111 | 112 | wrapper.resume()
|
112 | 113 | })
|
| 114 | + |
| 115 | +tape('events are not emitted on pause', function (t) { |
| 116 | + const wrapper = new PauseWrapper(new EventEmitter()) |
| 117 | + const stack = [] |
| 118 | + const somedata = {} |
| 119 | + wrapper.on('some-event', function (data) { |
| 120 | + stack.push('event[some-event]') |
| 121 | + t.equals(data, somedata) |
| 122 | + }) |
| 123 | + stack.push('pause()') |
| 124 | + wrapper.pause() |
| 125 | + wrapper.emit('some-event', somedata) |
| 126 | + wrapper._onResume(function a () { |
| 127 | + stack.push('a()') |
| 128 | + }) |
| 129 | + stack.push('resume()') |
| 130 | + wrapper.resume() |
| 131 | + t.deepEquals(stack, [ |
| 132 | + 'pause()', |
| 133 | + 'resume()', |
| 134 | + 'event[some-event]', |
| 135 | + 'a()' |
| 136 | + ]) |
| 137 | + t.end() |
| 138 | +}) |
| 139 | + |
| 140 | +tape('Closing the wrapper stops all events', function (t) { |
| 141 | + const wrapper = new PauseWrapper(new EventEmitter()) |
| 142 | + const stack = [] |
| 143 | + |
| 144 | + t.equals(wrapper._closed, false, 'even though private "_closed" should be predictable') |
| 145 | + |
| 146 | + stackEvent('some-event') |
| 147 | + stackEvent('close') |
| 148 | + stackEvent('destroy') |
| 149 | + wrapper.emit('some-event') |
| 150 | + wrapper._onResumeCb(function cb () { |
| 151 | + t.fail('This is called even though it should be closed by now') |
| 152 | + }, function a (cb2) { |
| 153 | + stack.push('a()') |
| 154 | + setImmediate(cb2) |
| 155 | + }) |
| 156 | + stack.push('._closed = true') |
| 157 | + wrapper._closed = true |
| 158 | + wrapper.emit('some-event') |
| 159 | + wrapper.emit('close') |
| 160 | + wrapper.emit('destroy') |
| 161 | + wrapper._onResume(function () { |
| 162 | + t.fail('This is called even though it was closed before') |
| 163 | + }) |
| 164 | + |
| 165 | + // Give it some time, to make sure that the callback has run |
| 166 | + // properly. |
| 167 | + setTimeout(function () { |
| 168 | + t.deepEqual(stack, [ |
| 169 | + 'event[some-event]', |
| 170 | + 'a()', |
| 171 | + '._closed = true', |
| 172 | + 'event[close]', |
| 173 | + 'event[destroy]' |
| 174 | + ]) |
| 175 | + t.end() |
| 176 | + }, 10) // 10ms should be enough. |
| 177 | + |
| 178 | + function stackEvent (eventName) { |
| 179 | + wrapper.on(eventName, function () { stack.push('event[' + eventName + ']') }) |
| 180 | + } |
| 181 | +}) |
| 182 | + |
| 183 | +tape('pause on resume', function (t) { |
| 184 | + const wrapper = new PauseWrapper(new EventEmitter()) |
| 185 | + const stack = [] |
| 186 | + |
| 187 | + stackEvent('pause') |
| 188 | + stackEvent('resume') |
| 189 | + |
| 190 | + stack.push('pause()') |
| 191 | + wrapper.pause() |
| 192 | + wrapper._onResume(function firstResume () { |
| 193 | + stack.push('firstResume()') |
| 194 | + stack.push('pause()') |
| 195 | + wrapper.pause() |
| 196 | + wrapper._onResume(function thirdResume () { |
| 197 | + stack.push('thirdResume()') |
| 198 | + t.deepEquals(stack, [ |
| 199 | + 'pause()', |
| 200 | + 'event[pause]', |
| 201 | + 'resume()', |
| 202 | + 'event[resume]', |
| 203 | + 'firstResume()', |
| 204 | + 'pause()', |
| 205 | + 'event[pause]', |
| 206 | + 'timeout()', |
| 207 | + 'resume()', |
| 208 | + 'event[resume]', |
| 209 | + 'secondResume()', |
| 210 | + 'thirdResume()' |
| 211 | + ]) |
| 212 | + t.end() |
| 213 | + }) |
| 214 | + setTimeout(function timeout () { |
| 215 | + stack.push('timeout()') |
| 216 | + stack.push('resume()') |
| 217 | + wrapper.resume() |
| 218 | + }) |
| 219 | + }) |
| 220 | + wrapper._onResume(function secondResume () { |
| 221 | + stack.push('secondResume()') |
| 222 | + }) |
| 223 | + stack.push('resume()') |
| 224 | + wrapper.resume() |
| 225 | + |
| 226 | + function stackEvent (eventName) { |
| 227 | + wrapper.on(eventName, function () { stack.push('event[' + eventName + ']') }) |
| 228 | + } |
| 229 | +}) |
0 commit comments