Skip to content

Instantly share code, notes, and snippets.

@ithinkihaveacat
Created February 9, 2010 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ithinkihaveacat/299779 to your computer and use it in GitHub Desktop.
Save ithinkihaveacat/299779 to your computer and use it in GitHub Desktop.
From 9c2cdf7f751ce4b463e02f8426df3af6fd05444b Mon Sep 17 00:00:00 2001
From: Michael Stillwell <mjs@beebo.org>
Date: Tue, 9 Feb 2010 22:32:52 +0000
Subject: [PATCH] Add test: modifying event listeners during callback.
---
test/mjsunit/test-event-emitter-modify-in-emit.js | 32 +++++++++++++++++++++
1 files changed, 32 insertions(+), 0 deletions(-)
create mode 100644 test/mjsunit/test-event-emitter-modify-in-emit.js
diff --git a/test/mjsunit/test-event-emitter-modify-in-emit.js b/test/mjsunit/test-event-emitter-modify-in-emit.js
new file mode 100644
index 0000000..7bb2eb3
--- /dev/null
+++ b/test/mjsunit/test-event-emitter-modify-in-emit.js
@@ -0,0 +1,32 @@
+process.mixin(require("./common"));
+var events = require('events');
+
+var callbacks_called = [ ];
+
+var e = new events.EventEmitter();
+
+function callback1() {
+ callbacks_called.push("callback1");
+ e.addListener("foo", callback2);
+ e.removeListener("foo", callback1);
+}
+
+function callback2() {
+ callbacks_called.push("callback2");
+ e.removeListener("foo", callback2);
+}
+
+e.addListener("foo", callback1);
+assert.equal(1, e.listeners("foo").length);
+
+e.emit("foo");
+assert.equal(1, e.listeners("foo").length);
+assert.deepEqual(["callback1"], callbacks_called);
+
+e.emit("foo");
+assert.equal(0, e.listeners("foo").length);
+assert.deepEqual(["callback1", "callback2"], callbacks_called);
+
+e.emit("foo");
+assert.equal(0, e.listeners("foo").length);
+assert.deepEqual(["callback1", "callback2"], callbacks_called);
--
1.6.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment