PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/simple/test-asynclistener-error.js

http://github.com/joyent/node
JavaScript | 257 lines | 181 code | 46 blank | 30 comment | 3 complexity | 602f14266e8d7f28ac23b4baf8e7c725 MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, ISC, Apache-2.0, MIT, AGPL-3.0
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var common = require('../common');
  22. var assert = require('assert');
  23. var dns = require('dns');
  24. var fs = require('fs');
  25. var net = require('net');
  26. var tracing = require('tracing');
  27. var addListener = tracing.addAsyncListener;
  28. var removeListener = tracing.removeAsyncListener;
  29. var errorMsgs = [];
  30. var currentMsg = '';
  31. var caught = 0;
  32. var expectCaught = 0;
  33. var exitCbRan = false;
  34. var callbacksObj = {
  35. error: function(value, er) {
  36. var idx = errorMsgs.indexOf(er.message);
  37. caught++;
  38. if (-1 < idx)
  39. errorMsgs.splice(idx, 1);
  40. return currentMsg === er.message;
  41. }
  42. };
  43. var listener = tracing.createAsyncListener(callbacksObj);
  44. process.on('exit', function(code) {
  45. removeListener(listener);
  46. // Something else went wrong, no need to further check.
  47. if (code > 0)
  48. return;
  49. // Make sure the exit callback only runs once.
  50. assert.ok(!exitCbRan);
  51. exitCbRan = true;
  52. // Check if any error messages weren't removed from the msg queue.
  53. if (errorMsgs.length > 0)
  54. throw new Error('Errors not fired: ' + errorMsgs);
  55. assert.equal(caught, expectCaught, 'caught all expected errors');
  56. process._rawDebug('ok');
  57. });
  58. // Catch synchronous throws
  59. errorMsgs.push('sync throw');
  60. process.nextTick(function() {
  61. addListener(listener);
  62. expectCaught++;
  63. currentMsg = 'sync throw';
  64. throw new Error(currentMsg);
  65. removeListener(listener);
  66. });
  67. // Simple cases
  68. errorMsgs.push('setTimeout - simple');
  69. errorMsgs.push('setImmediate - simple');
  70. errorMsgs.push('setInterval - simple');
  71. errorMsgs.push('process.nextTick - simple');
  72. process.nextTick(function() {
  73. addListener(listener);
  74. setTimeout(function() {
  75. currentMsg = 'setTimeout - simple';
  76. throw new Error(currentMsg);
  77. });
  78. expectCaught++;
  79. setImmediate(function() {
  80. currentMsg = 'setImmediate - simple';
  81. throw new Error(currentMsg);
  82. });
  83. expectCaught++;
  84. var b = setInterval(function() {
  85. clearInterval(b);
  86. currentMsg = 'setInterval - simple';
  87. throw new Error(currentMsg);
  88. });
  89. expectCaught++;
  90. process.nextTick(function() {
  91. currentMsg = 'process.nextTick - simple';
  92. throw new Error(currentMsg);
  93. });
  94. expectCaught++;
  95. removeListener(listener);
  96. });
  97. // Deeply nested
  98. errorMsgs.push('setInterval - nested');
  99. errorMsgs.push('setImmediate - nested');
  100. errorMsgs.push('process.nextTick - nested');
  101. errorMsgs.push('setTimeout2 - nested');
  102. errorMsgs.push('setTimeout - nested');
  103. process.nextTick(function() {
  104. addListener(listener);
  105. setTimeout(function() {
  106. process.nextTick(function() {
  107. setImmediate(function() {
  108. var b = setInterval(function() {
  109. clearInterval(b);
  110. currentMsg = 'setInterval - nested';
  111. throw new Error(currentMsg);
  112. });
  113. expectCaught++;
  114. currentMsg = 'setImmediate - nested';
  115. throw new Error(currentMsg);
  116. });
  117. expectCaught++;
  118. currentMsg = 'process.nextTick - nested';
  119. throw new Error(currentMsg);
  120. });
  121. expectCaught++;
  122. setTimeout(function() {
  123. currentMsg = 'setTimeout2 - nested';
  124. throw new Error(currentMsg);
  125. });
  126. expectCaught++;
  127. currentMsg = 'setTimeout - nested';
  128. throw new Error(currentMsg);
  129. });
  130. expectCaught++;
  131. removeListener(listener);
  132. });
  133. // FS
  134. errorMsgs.push('fs - file does not exist');
  135. errorMsgs.push('fs - exists');
  136. errorMsgs.push('fs - realpath');
  137. process.nextTick(function() {
  138. addListener(listener);
  139. fs.stat('does not exist', function(err, stats) {
  140. currentMsg = 'fs - file does not exist';
  141. throw new Error(currentMsg);
  142. });
  143. expectCaught++;
  144. fs.exists('hi all', function(exists) {
  145. currentMsg = 'fs - exists';
  146. throw new Error(currentMsg);
  147. });
  148. expectCaught++;
  149. fs.realpath('/some/path', function(err, resolved) {
  150. currentMsg = 'fs - realpath';
  151. throw new Error(currentMsg);
  152. });
  153. expectCaught++;
  154. removeListener(listener);
  155. });
  156. // Nested FS
  157. errorMsgs.push('fs - nested file does not exist');
  158. process.nextTick(function() {
  159. addListener(listener);
  160. setTimeout(function() {
  161. setImmediate(function() {
  162. var b = setInterval(function() {
  163. clearInterval(b);
  164. process.nextTick(function() {
  165. fs.stat('does not exist', function(err, stats) {
  166. currentMsg = 'fs - nested file does not exist';
  167. throw new Error(currentMsg);
  168. });
  169. expectCaught++;
  170. });
  171. });
  172. });
  173. });
  174. removeListener(listener);
  175. });
  176. // Net
  177. errorMsgs.push('net - connection listener');
  178. errorMsgs.push('net - client connect');
  179. errorMsgs.push('net - server listening');
  180. process.nextTick(function() {
  181. addListener(listener);
  182. var server = net.createServer(function(c) {
  183. server.close();
  184. currentMsg = 'net - connection listener';
  185. throw new Error(currentMsg);
  186. });
  187. expectCaught++;
  188. server.listen(common.PORT, function() {
  189. var client = net.connect(common.PORT, function() {
  190. client.end();
  191. currentMsg = 'net - client connect';
  192. throw new Error(currentMsg);
  193. });
  194. expectCaught++;
  195. currentMsg = 'net - server listening';
  196. throw new Error(currentMsg);
  197. });
  198. expectCaught++;
  199. removeListener(listener);
  200. });
  201. // DNS
  202. errorMsgs.push('dns - lookup');
  203. process.nextTick(function() {
  204. addListener(listener);
  205. dns.lookup('localhost', function() {
  206. currentMsg = 'dns - lookup';
  207. throw new Error(currentMsg);
  208. });
  209. expectCaught++;
  210. removeListener(listener);
  211. });