bind-emitter.tap.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. 'use strict';
  2. var test = require('tap').test
  3. , EventEmitter = require('events').EventEmitter
  4. , cls = require('../context.js')
  5. ;
  6. test("event emitters bound to CLS context", function (t) {
  7. t.plan(13);
  8. t.test("handler registered in context, emit out of context", function (t) {
  9. t.plan(1);
  10. var n = cls.createNamespace('in')
  11. , ee = new EventEmitter()
  12. ;
  13. n.run(function () {
  14. n.set('value', 'hello');
  15. n.bindEmitter(ee);
  16. ee.on('event', function () {
  17. t.equal(n.get('value'), 'hello', "value still set in EE.");
  18. cls.destroyNamespace('in');
  19. });
  20. });
  21. ee.emit('event');
  22. });
  23. t.test("once handler registered in context", function (t) {
  24. t.plan(1);
  25. var n = cls.createNamespace('inOnce')
  26. , ee = new EventEmitter()
  27. ;
  28. n.run(function () {
  29. n.set('value', 'hello');
  30. n.bindEmitter(ee);
  31. ee.once('event', function () {
  32. t.equal(n.get('value'), 'hello', "value still set in EE.");
  33. cls.destroyNamespace('inOnce');
  34. });
  35. });
  36. ee.emit('event');
  37. });
  38. t.test("handler registered out of context, emit in context", function (t) {
  39. t.plan(1);
  40. var n = cls.createNamespace('out')
  41. , ee = new EventEmitter()
  42. ;
  43. ee.on('event', function () {
  44. t.equal(n.get('value'), 'hello', "value still set in EE.");
  45. cls.destroyNamespace('out');
  46. });
  47. n.run(function () {
  48. n.set('value', 'hello');
  49. n.bindEmitter(ee);
  50. ee.emit('event');
  51. });
  52. });
  53. t.test("once handler registered out of context", function (t) {
  54. t.plan(1);
  55. var n = cls.createNamespace('outOnce')
  56. , ee = new EventEmitter()
  57. ;
  58. ee.once('event', function () {
  59. t.equal(n.get('value'), 'hello', "value still set in EE.");
  60. cls.destroyNamespace('outOnce');
  61. });
  62. n.run(function () {
  63. n.set('value', 'hello');
  64. n.bindEmitter(ee);
  65. ee.emit('event');
  66. });
  67. });
  68. t.test("handler registered out of context, emit out of context", function (t) {
  69. t.plan(1);
  70. var n = cls.createNamespace('out')
  71. , ee = new EventEmitter()
  72. ;
  73. ee.on('event', function () {
  74. t.equal(n.get('value'), undefined, "no context.");
  75. cls.destroyNamespace('out');
  76. });
  77. n.run(function () {
  78. n.set('value', 'hello');
  79. n.bindEmitter(ee);
  80. });
  81. ee.emit('event');
  82. });
  83. t.test("once handler registered out of context on Readable", function (t) {
  84. var Readable = require('stream').Readable;
  85. if (Readable) {
  86. t.plan(12);
  87. var n = cls.createNamespace('outOnceReadable')
  88. , re = new Readable()
  89. ;
  90. re._read = function () {};
  91. t.ok(n.name, "namespace has a name");
  92. t.equal(n.name, 'outOnceReadable', "namespace has a name");
  93. re.once('data', function (data) {
  94. t.equal(n.get('value'), 'hello', "value still set in EE");
  95. t.equal(data, 'blah', "emit still works");
  96. cls.destroyNamespace('outOnceReadable');
  97. });
  98. n.run(function () {
  99. n.set('value', 'hello');
  100. t.notOk(re.emit.__wrapped, "emit is not wrapped");
  101. t.notOk(re.on.__wrapped, "on is not wrapped");
  102. t.notOk(re.addListener.__wrapped, "addListener is not wrapped");
  103. n.bindEmitter(re);
  104. t.ok(re.emit.__wrapped, "emit is wrapped");
  105. t.ok(re.on.__wrapped, "on is wrapped");
  106. t.ok(re.addListener.__wrapped, "addListener is wrapped");
  107. t.equal(typeof re._events.data, 'function', 'only the one data listener');
  108. t.notOk(re._events.data['context@outOnceReadable'], "context isn't on listener");
  109. re.emit('data', 'blah');
  110. });
  111. }
  112. else {
  113. t.comment("this test requires node 0.10+");
  114. t.end();
  115. }
  116. });
  117. t.test("emitter with newListener that removes handler", function (t) {
  118. t.plan(3);
  119. var n = cls.createNamespace('newListener')
  120. , ee = new EventEmitter()
  121. ;
  122. // add monkeypatching to ee
  123. n.bindEmitter(ee);
  124. function listen() {
  125. ee.on('data', function (chunk) {
  126. t.equal(chunk, 'chunk', 'listener still works');
  127. });
  128. }
  129. ee.on('newListener', function handler(event) {
  130. if (event !== 'data') return;
  131. this.removeListener('newListener', handler);
  132. t.notOk(this.listeners('newListener').length, 'newListener was removed');
  133. process.nextTick(listen);
  134. });
  135. ee.on('drain', function (chunk) {
  136. process.nextTick(function () {
  137. ee.emit('data', chunk);
  138. });
  139. });
  140. ee.on('data', function (chunk) {
  141. t.equal(chunk, 'chunk', 'got data event');
  142. cls.destroyNamespace('newListener');
  143. });
  144. ee.emit('drain', 'chunk');
  145. });
  146. t.test("handler registered in context on Readable", function (t) {
  147. var Readable = require('stream').Readable;
  148. if (Readable) {
  149. t.plan(12);
  150. var n = cls.createNamespace('outOnReadable')
  151. , re = new Readable()
  152. ;
  153. re._read = function () {};
  154. t.ok(n.name, "namespace has a name");
  155. t.equal(n.name, 'outOnReadable', "namespace has a name");
  156. n.run(function () {
  157. n.set('value', 'hello');
  158. n.bindEmitter(re);
  159. t.ok(re.emit.__wrapped, "emit is wrapped");
  160. t.ok(re.on.__wrapped, "on is wrapped");
  161. t.ok(re.addListener.__wrapped, "addListener is wrapped");
  162. re.on('data', function (data) {
  163. t.equal(n.get('value'), 'hello', "value still set in EE");
  164. t.equal(data, 'blah', "emit still works");
  165. cls.destroyNamespace('outOnReadable');
  166. });
  167. });
  168. t.ok(re.emit.__wrapped, "emit is still wrapped");
  169. t.ok(re.on.__wrapped, "on is still wrapped");
  170. t.ok(re.addListener.__wrapped, "addListener is still wrapped");
  171. t.equal(typeof re._events.data, 'function', 'only the one data listener');
  172. t.ok(re._events.data['cls@contexts']['context@outOnReadable'],
  173. "context is bound to listener");
  174. re.emit('data', 'blah');
  175. }
  176. else {
  177. t.comment("this test requires node 0.10+");
  178. t.end();
  179. }
  180. });
  181. t.test("handler added but used entirely out of context", function (t) {
  182. t.plan(2);
  183. var n = cls.createNamespace('none')
  184. , ee = new EventEmitter()
  185. ;
  186. n.run(function () {
  187. n.set('value', 'hello');
  188. n.bindEmitter(ee);
  189. });
  190. ee.on('event', function () {
  191. t.ok(n, "n is set");
  192. t.notOk(n.get('value'), "value shouldn't be visible");
  193. cls.destroyNamespace('none');
  194. });
  195. ee.emit('event');
  196. });
  197. t.test("handler added but no listeners registered", function (t) {
  198. t.plan(3);
  199. var http = require('http')
  200. , n = cls.createNamespace('no_listener')
  201. ;
  202. // only fails on Node < 0.10
  203. var server = http.createServer(function (req, res) {
  204. n.bindEmitter(req);
  205. t.doesNotThrow(function () {
  206. req.emit('event');
  207. });
  208. res.writeHead(200, {"Content-Length" : 4});
  209. res.end('WORD');
  210. });
  211. server.listen(8080);
  212. http.get('http://localhost:8080/', function (res) {
  213. t.equal(res.statusCode, 200, "request came back OK");
  214. res.setEncoding('ascii');
  215. res.on('data', function (body) {
  216. t.equal(body, 'WORD');
  217. server.close();
  218. cls.destroyNamespace('no_listener');
  219. });
  220. });
  221. });
  222. t.test("listener with parameters added but not bound to context", function (t) {
  223. t.plan(2);
  224. var ee = new EventEmitter()
  225. , n = cls.createNamespace('param_list')
  226. ;
  227. function sent(value) {
  228. t.equal(value, 3, "sent value is correct");
  229. cls.destroyNamespace('param_list');
  230. }
  231. ee.on('send', sent);
  232. n.bindEmitter(ee);
  233. t.doesNotThrow(function () {
  234. ee.emit('send', 3);
  235. });
  236. });
  237. t.test("listener that throws doesn't leave removeListener wrapped", function (t) {
  238. t.plan(4);
  239. var ee = new EventEmitter()
  240. , n = cls.createNamespace('kaboom')
  241. ;
  242. n.bindEmitter(ee);
  243. function kaboom() {
  244. throw new Error('whoops');
  245. }
  246. n.run(function () {
  247. ee.on('bad', kaboom);
  248. t.throws(function () { ee.emit('bad'); });
  249. t.equal(typeof ee.removeListener, 'function', 'removeListener is still there');
  250. t.notOk(ee.removeListener.__wrapped, "removeListener got unwrapped");
  251. t.equal(ee._events.bad, kaboom, "listener isn't still bound");
  252. cls.destroyNamespace('kaboom');
  253. });
  254. });
  255. t.test("emitter bound to multiple namespaces handles them correctly", function (t) {
  256. t.plan(8);
  257. var ee = new EventEmitter()
  258. , ns1 = cls.createNamespace('1')
  259. , ns2 = cls.createNamespace('2')
  260. ;
  261. // emulate an incoming data emitter
  262. setTimeout(function () {
  263. ee.emit('data', 'hi');
  264. }, 10);
  265. t.doesNotThrow(function () { ns1.bindEmitter(ee); });
  266. t.doesNotThrow(function () { ns2.bindEmitter(ee); });
  267. ns1.run(function () {
  268. ns2.run(function () {
  269. ns1.set('name', 'tom1');
  270. ns2.set('name', 'tom2');
  271. t.doesNotThrow(function () { ns1.bindEmitter(ee); });
  272. t.doesNotThrow(function () { ns2.bindEmitter(ee); });
  273. ns1.run(function () {
  274. process.nextTick(function () {
  275. t.equal(ns1.get('name'), 'tom1', "ns1 value correct");
  276. t.equal(ns2.get('name'), 'tom2', "ns2 value correct");
  277. ns1.set('name', 'bob');
  278. ns2.set('name', 'alice');
  279. ee.on('data', function () {
  280. t.equal(ns1.get('name'), 'bob', "ns1 value bound onto emitter");
  281. t.equal(ns2.get('name'), 'alice', "ns2 value bound onto emitter");
  282. });
  283. });
  284. });
  285. });
  286. });
  287. });
  288. });