mirror of https://github.com/lukechilds/node.git
Browse Source
This commit splits several large event emitter tests into smaller block scoped tests. This keeps the tests more isolated. This commit also replaces var with const, assert.equal() with assert.stictEqual(), and other small enhancements. PR-URL: https://github.com/nodejs/node/pull/6446 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>process-exit-stdio-flushing
cjihrig
9 years ago
3 changed files with 191 additions and 176 deletions
@ -1,32 +1,38 @@ |
|||
'use strict'; |
|||
|
|||
require('../common'); |
|||
var assert = require('assert'); |
|||
var events = require('events'); |
|||
const assert = require('assert'); |
|||
const events = require('events'); |
|||
|
|||
function listener() {} |
|||
function listener2() {} |
|||
|
|||
var e1 = new events.EventEmitter(); |
|||
e1.on('foo', listener); |
|||
var fooListeners = e1.listeners('foo'); |
|||
assert.deepStrictEqual(e1.listeners('foo'), [listener]); |
|||
e1.removeAllListeners('foo'); |
|||
assert.deepStrictEqual(e1.listeners('foo'), []); |
|||
assert.deepStrictEqual(fooListeners, [listener]); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('foo', listener); |
|||
const fooListeners = ee.listeners('foo'); |
|||
assert.deepStrictEqual(ee.listeners('foo'), [listener]); |
|||
ee.removeAllListeners('foo'); |
|||
assert.deepStrictEqual(ee.listeners('foo'), []); |
|||
assert.deepStrictEqual(fooListeners, [listener]); |
|||
} |
|||
|
|||
var e2 = new events.EventEmitter(); |
|||
e2.on('foo', listener); |
|||
var e2ListenersCopy = e2.listeners('foo'); |
|||
assert.deepStrictEqual(e2ListenersCopy, [listener]); |
|||
assert.deepStrictEqual(e2.listeners('foo'), [listener]); |
|||
e2ListenersCopy.push(listener2); |
|||
assert.deepStrictEqual(e2.listeners('foo'), [listener]); |
|||
assert.deepStrictEqual(e2ListenersCopy, [listener, listener2]); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('foo', listener); |
|||
const eeListenersCopy = ee.listeners('foo'); |
|||
assert.deepStrictEqual(eeListenersCopy, [listener]); |
|||
assert.deepStrictEqual(ee.listeners('foo'), [listener]); |
|||
eeListenersCopy.push(listener2); |
|||
assert.deepStrictEqual(ee.listeners('foo'), [listener]); |
|||
assert.deepStrictEqual(eeListenersCopy, [listener, listener2]); |
|||
} |
|||
|
|||
var e3 = new events.EventEmitter(); |
|||
e3.on('foo', listener); |
|||
var e3ListenersCopy = e3.listeners('foo'); |
|||
e3.on('foo', listener2); |
|||
assert.deepStrictEqual(e3.listeners('foo'), [listener, listener2]); |
|||
assert.deepStrictEqual(e3ListenersCopy, [listener]); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('foo', listener); |
|||
const eeListenersCopy = ee.listeners('foo'); |
|||
ee.on('foo', listener2); |
|||
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]); |
|||
assert.deepStrictEqual(eeListenersCopy, [listener]); |
|||
} |
|||
|
@ -1,115 +1,117 @@ |
|||
'use strict'; |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
var events = require('events'); |
|||
|
|||
var count = 0; |
|||
|
|||
function listener1() { |
|||
console.log('listener1'); |
|||
count++; |
|||
} |
|||
|
|||
function listener2() { |
|||
console.log('listener2'); |
|||
count++; |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const events = require('events'); |
|||
|
|||
function listener1() {} |
|||
function listener2() {} |
|||
|
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('hello', listener1); |
|||
ee.on('removeListener', common.mustCall((name, cb) => { |
|||
assert.strictEqual(name, 'hello'); |
|||
assert.strictEqual(cb, listener1); |
|||
})); |
|||
ee.removeListener('hello', listener1); |
|||
assert.deepStrictEqual([], ee.listeners('hello')); |
|||
} |
|||
|
|||
function remove1() { |
|||
assert(0); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('hello', listener1); |
|||
ee.on('removeListener', common.fail); |
|||
ee.removeListener('hello', listener2); |
|||
assert.deepStrictEqual([listener1], ee.listeners('hello')); |
|||
} |
|||
|
|||
function remove2() { |
|||
assert(0); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('hello', listener1); |
|||
ee.on('hello', listener2); |
|||
ee.once('removeListener', common.mustCall((name, cb) => { |
|||
assert.strictEqual(name, 'hello'); |
|||
assert.strictEqual(cb, listener1); |
|||
assert.deepStrictEqual([listener2], ee.listeners('hello')); |
|||
})); |
|||
ee.removeListener('hello', listener1); |
|||
assert.deepStrictEqual([listener2], ee.listeners('hello')); |
|||
ee.once('removeListener', common.mustCall((name, cb) => { |
|||
assert.strictEqual(name, 'hello'); |
|||
assert.strictEqual(cb, listener2); |
|||
assert.deepStrictEqual([], ee.listeners('hello')); |
|||
})); |
|||
ee.removeListener('hello', listener2); |
|||
assert.deepStrictEqual([], ee.listeners('hello')); |
|||
} |
|||
|
|||
var e1 = new events.EventEmitter(); |
|||
e1.on('hello', listener1); |
|||
e1.on('removeListener', common.mustCall(function(name, cb) { |
|||
assert.equal(name, 'hello'); |
|||
assert.equal(cb, listener1); |
|||
})); |
|||
e1.removeListener('hello', listener1); |
|||
assert.deepStrictEqual([], e1.listeners('hello')); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
|
|||
var e2 = new events.EventEmitter(); |
|||
e2.on('hello', listener1); |
|||
e2.on('removeListener', common.fail); |
|||
e2.removeListener('hello', listener2); |
|||
assert.deepStrictEqual([listener1], e2.listeners('hello')); |
|||
function remove1() { |
|||
common.fail('remove1 should not have been called'); |
|||
} |
|||
|
|||
var e3 = new events.EventEmitter(); |
|||
e3.on('hello', listener1); |
|||
e3.on('hello', listener2); |
|||
e3.once('removeListener', common.mustCall(function(name, cb) { |
|||
assert.equal(name, 'hello'); |
|||
assert.equal(cb, listener1); |
|||
assert.deepStrictEqual([listener2], e3.listeners('hello')); |
|||
})); |
|||
e3.removeListener('hello', listener1); |
|||
assert.deepStrictEqual([listener2], e3.listeners('hello')); |
|||
e3.once('removeListener', common.mustCall(function(name, cb) { |
|||
assert.equal(name, 'hello'); |
|||
assert.equal(cb, listener2); |
|||
assert.deepStrictEqual([], e3.listeners('hello')); |
|||
})); |
|||
e3.removeListener('hello', listener2); |
|||
assert.deepStrictEqual([], e3.listeners('hello')); |
|||
function remove2() { |
|||
common.fail('remove2 should not have been called'); |
|||
} |
|||
|
|||
var e4 = new events.EventEmitter(); |
|||
e4.on('removeListener', common.mustCall(function(name, cb) { |
|||
ee.on('removeListener', common.mustCall(function(name, cb) { |
|||
if (cb !== remove1) return; |
|||
this.removeListener('quux', remove2); |
|||
this.emit('quux'); |
|||
}, 2)); |
|||
e4.on('quux', remove1); |
|||
e4.on('quux', remove2); |
|||
e4.removeListener('quux', remove1); |
|||
}, 2)); |
|||
ee.on('quux', remove1); |
|||
ee.on('quux', remove2); |
|||
ee.removeListener('quux', remove1); |
|||
} |
|||
|
|||
var e5 = new events.EventEmitter(); |
|||
e5.on('hello', listener1); |
|||
e5.on('hello', listener2); |
|||
e5.once('removeListener', common.mustCall(function(name, cb) { |
|||
assert.equal(name, 'hello'); |
|||
assert.equal(cb, listener1); |
|||
assert.deepStrictEqual([listener2], e5.listeners('hello')); |
|||
e5.once('removeListener', common.mustCall(function(name, cb) { |
|||
assert.equal(name, 'hello'); |
|||
assert.equal(cb, listener2); |
|||
assert.deepStrictEqual([], e5.listeners('hello')); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
ee.on('hello', listener1); |
|||
ee.on('hello', listener2); |
|||
ee.once('removeListener', common.mustCall((name, cb) => { |
|||
assert.strictEqual(name, 'hello'); |
|||
assert.strictEqual(cb, listener1); |
|||
assert.deepStrictEqual([listener2], ee.listeners('hello')); |
|||
ee.once('removeListener', common.mustCall((name, cb) => { |
|||
assert.strictEqual(name, 'hello'); |
|||
assert.strictEqual(cb, listener2); |
|||
assert.deepStrictEqual([], ee.listeners('hello')); |
|||
})); |
|||
e5.removeListener('hello', listener2); |
|||
assert.deepStrictEqual([], e5.listeners('hello')); |
|||
})); |
|||
e5.removeListener('hello', listener1); |
|||
assert.deepStrictEqual([], e5.listeners('hello')); |
|||
|
|||
const e6 = new events.EventEmitter(); |
|||
|
|||
const listener3 = common.mustCall(() => { |
|||
e6.removeListener('hello', listener4); |
|||
}, 2); |
|||
|
|||
const listener4 = common.mustCall(() => {}, 1); |
|||
ee.removeListener('hello', listener2); |
|||
assert.deepStrictEqual([], ee.listeners('hello')); |
|||
})); |
|||
ee.removeListener('hello', listener1); |
|||
assert.deepStrictEqual([], ee.listeners('hello')); |
|||
} |
|||
|
|||
e6.on('hello', listener3); |
|||
e6.on('hello', listener4); |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
const listener3 = common.mustCall(() => { |
|||
ee.removeListener('hello', listener4); |
|||
}, 2); |
|||
const listener4 = common.mustCall(() => {}); |
|||
|
|||
// listener4 will still be called although it is removed by listener 3.
|
|||
e6.emit('hello'); |
|||
// This is so because the interal listener array at time of emit
|
|||
// was [listener3,listener4]
|
|||
ee.on('hello', listener3); |
|||
ee.on('hello', listener4); |
|||
|
|||
// Interal listener array [listener3]
|
|||
e6.emit('hello'); |
|||
// listener4 will still be called although it is removed by listener 3.
|
|||
ee.emit('hello'); |
|||
// This is so because the interal listener array at time of emit
|
|||
// was [listener3,listener4]
|
|||
|
|||
const e7 = new events.EventEmitter(); |
|||
// Interal listener array [listener3]
|
|||
ee.emit('hello'); |
|||
} |
|||
|
|||
const listener5 = () => {}; |
|||
{ |
|||
const ee = new events.EventEmitter(); |
|||
|
|||
e7.once('hello', listener5); |
|||
e7.on('removeListener', common.mustCall((eventName, listener) => { |
|||
ee.once('hello', listener1); |
|||
ee.on('removeListener', common.mustCall((eventName, listener) => { |
|||
assert.strictEqual(eventName, 'hello'); |
|||
assert.strictEqual(listener, listener5); |
|||
})); |
|||
e7.emit('hello'); |
|||
assert.strictEqual(listener, listener1); |
|||
})); |
|||
ee.emit('hello'); |
|||
} |
|||
|
Loading…
Reference in new issue