Skip to content

Commit

Permalink
test: refactor large event emitter tests
Browse files Browse the repository at this point in the history
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: #6446
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig committed May 2, 2016
1 parent d2eb935 commit 1ab5b4e
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 176 deletions.
52 changes: 29 additions & 23 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
@@ -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]);
}
115 changes: 61 additions & 54 deletions test/parallel/test-event-emitter-remove-all-listeners.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var events = require('events');
const common = require('../common');
const assert = require('assert');
const events = require('events');


function expect(expected) {
var actual = [];
const actual = [];
process.on('exit', function() {
assert.deepStrictEqual(actual.sort(), expected.sort());
});
Expand All @@ -17,56 +17,63 @@ function expect(expected) {

function listener() {}

var e1 = new events.EventEmitter();
e1.on('foo', listener);
e1.on('bar', listener);
e1.on('baz', listener);
e1.on('baz', listener);
var fooListeners = e1.listeners('foo');
var barListeners = e1.listeners('bar');
var bazListeners = e1.listeners('baz');
e1.on('removeListener', expect(['bar', 'baz', 'baz']));
e1.removeAllListeners('bar');
e1.removeAllListeners('baz');
assert.deepStrictEqual(e1.listeners('foo'), [listener]);
assert.deepStrictEqual(e1.listeners('bar'), []);
assert.deepStrictEqual(e1.listeners('baz'), []);
// after calling removeAllListeners,
// the old listeners array should stay unchanged
assert.deepStrictEqual(fooListeners, [listener]);
assert.deepStrictEqual(barListeners, [listener]);
assert.deepStrictEqual(bazListeners, [listener, listener]);
// after calling removeAllListeners,
// new listeners arrays are different from the old
assert.notEqual(e1.listeners('bar'), barListeners);
assert.notEqual(e1.listeners('baz'), bazListeners);
{
const ee = new events.EventEmitter();
ee.on('foo', listener);
ee.on('bar', listener);
ee.on('baz', listener);
ee.on('baz', listener);
const fooListeners = ee.listeners('foo');
const barListeners = ee.listeners('bar');
const bazListeners = ee.listeners('baz');
ee.on('removeListener', expect(['bar', 'baz', 'baz']));
ee.removeAllListeners('bar');
ee.removeAllListeners('baz');
assert.deepStrictEqual(ee.listeners('foo'), [listener]);
assert.deepStrictEqual(ee.listeners('bar'), []);
assert.deepStrictEqual(ee.listeners('baz'), []);
// After calling removeAllListeners(),
// the old listeners array should stay unchanged.
assert.deepStrictEqual(fooListeners, [listener]);
assert.deepStrictEqual(barListeners, [listener]);
assert.deepStrictEqual(bazListeners, [listener, listener]);
// After calling removeAllListeners(),
// new listeners arrays is different from the old.
assert.notEqual(ee.listeners('bar'), barListeners);
assert.notEqual(ee.listeners('baz'), bazListeners);
}

var e2 = new events.EventEmitter();
e2.on('foo', listener);
e2.on('bar', listener);
// expect LIFO order
e2.on('removeListener', expect(['foo', 'bar', 'removeListener']));
e2.on('removeListener', expect(['foo', 'bar']));
e2.removeAllListeners();
console.error(e2);
assert.deepStrictEqual([], e2.listeners('foo'));
assert.deepStrictEqual([], e2.listeners('bar'));
{
const ee = new events.EventEmitter();
ee.on('foo', listener);
ee.on('bar', listener);
// Expect LIFO order
ee.on('removeListener', expect(['foo', 'bar', 'removeListener']));
ee.on('removeListener', expect(['foo', 'bar']));
ee.removeAllListeners();
assert.deepStrictEqual([], ee.listeners('foo'));
assert.deepStrictEqual([], ee.listeners('bar'));
}

var e3 = new events.EventEmitter();
e3.on('removeListener', listener);
// check for regression where removeAllListeners throws when
// there exists a removeListener listener, but there exists
// no listeners for the provided event type
assert.doesNotThrow(e3.removeAllListeners.bind(e3, 'foo'));
{
const ee = new events.EventEmitter();
ee.on('removeListener', listener);
// Check for regression where removeAllListeners() throws when
// there exists a 'removeListener' listener, but there exists
// no listeners for the provided event type.
assert.doesNotThrow(ee.removeAllListeners.bind(ee, 'foo'));
}

var e4 = new events.EventEmitter();
var expectLength = 2;
e4.on('removeListener', function(name, listener) {
assert.equal(expectLength--, this.listeners('baz').length);
});
e4.on('baz', function() {});
e4.on('baz', function() {});
e4.on('baz', function() {});
assert.equal(e4.listeners('baz').length, expectLength + 1);
e4.removeAllListeners('baz');
assert.equal(e4.listeners('baz').length, 0);
{
const ee = new events.EventEmitter();
var expectLength = 2;
ee.on('removeListener', function(name, listener) {
assert.strictEqual(expectLength--, this.listeners('baz').length);
});
ee.on('baz', function() {});
ee.on('baz', function() {});
ee.on('baz', function() {});
assert.strictEqual(ee.listeners('baz').length, expectLength + 1);
ee.removeAllListeners('baz');
assert.strictEqual(ee.listeners('baz').length, 0);
}
Loading

0 comments on commit 1ab5b4e

Please sign in to comment.