PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/newrelic/node_modules/continuation-local-storage/node_modules/async-listener/test/core-asynclistener-error.simple.js

https://github.com/markba/bashhistory.io
JavaScript | 229 lines | 158 code | 44 blank | 27 comment | 3 complexity | eb9d12df7bc82506bebc992696b7d673 MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, MIT
  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 PORT = 12346;
  22. if (!process.addAsyncListener) require('../index.js');
  23. if (!global.setImmediate) global.setImmediate = setTimeout;
  24. var assert = require('assert');
  25. var dns = require('dns');
  26. var fs = require('fs');
  27. var net = require('net');
  28. var addListener = process.addAsyncListener;
  29. var removeListener = process.removeAsyncListener;
  30. var caught = 0;
  31. var expectCaught = 0;
  32. function asyncL() { }
  33. var callbacksObj = {
  34. error: function(domain, er) {
  35. caught++;
  36. switch (er.message) {
  37. case 'sync throw':
  38. case 'setTimeout - simple':
  39. case 'setImmediate - simple':
  40. case 'setInterval - simple':
  41. case 'process.nextTick - simple':
  42. case 'setTimeout - nested':
  43. case 'process.nextTick - nested':
  44. case 'setImmediate - nested':
  45. case 'setTimeout2 - nested':
  46. case 'setInterval - nested':
  47. case 'fs - file does not exist':
  48. case 'fs - nested file does not exist':
  49. case 'fs - exists':
  50. case 'fs - realpath':
  51. case 'net - connection listener':
  52. case 'net - server listening':
  53. case 'net - client connect':
  54. case 'dns - lookup':
  55. return true;
  56. default:
  57. return false;
  58. }
  59. }
  60. };
  61. process.on('exit', function() {
  62. console.log('caught:', caught);
  63. console.log('expected:', expectCaught);
  64. assert.equal(caught, expectCaught, 'caught all expected errors');
  65. console.log('ok');
  66. });
  67. var listener = process.createAsyncListener(asyncL, callbacksObj);
  68. // Catch synchronous throws
  69. process.nextTick(function() {
  70. addListener(listener);
  71. expectCaught++;
  72. throw new Error('sync throw');
  73. removeListener(listener);
  74. });
  75. // Simple cases
  76. process.nextTick(function() {
  77. addListener(listener);
  78. setTimeout(function() {
  79. throw new Error('setTimeout - simple');
  80. });
  81. expectCaught++;
  82. setImmediate(function() {
  83. throw new Error('setImmediate - simple');
  84. });
  85. expectCaught++;
  86. var b = setInterval(function() {
  87. clearInterval(b);
  88. throw new Error('setInterval - simple');
  89. });
  90. expectCaught++;
  91. process.nextTick(function() {
  92. throw new Error('process.nextTick - simple');
  93. });
  94. expectCaught++;
  95. removeListener(listener);
  96. });
  97. // Deeply nested
  98. process.nextTick(function() {
  99. addListener(listener);
  100. setTimeout(function() {
  101. process.nextTick(function() {
  102. setImmediate(function() {
  103. var b = setInterval(function() {
  104. clearInterval(b);
  105. throw new Error('setInterval - nested');
  106. });
  107. expectCaught++;
  108. throw new Error('setImmediate - nested');
  109. });
  110. expectCaught++;
  111. throw new Error('process.nextTick - nested');
  112. });
  113. expectCaught++;
  114. setTimeout(function() {
  115. throw new Error('setTimeout2 - nested');
  116. });
  117. expectCaught++;
  118. throw new Error('setTimeout - nested');
  119. });
  120. expectCaught++;
  121. removeListener(listener);
  122. });
  123. // FS
  124. process.nextTick(function() {
  125. addListener(listener);
  126. fs.stat('does not exist', function() {
  127. throw new Error('fs - file does not exist');
  128. });
  129. expectCaught++;
  130. fs.exists('hi all', function() {
  131. throw new Error('fs - exists');
  132. });
  133. expectCaught++;
  134. fs.realpath('/some/path', function() {
  135. throw new Error('fs - realpath');
  136. });
  137. expectCaught++;
  138. removeListener(listener);
  139. });
  140. // Nested FS
  141. process.nextTick(function() {
  142. addListener(listener);
  143. setTimeout(function() {
  144. setImmediate(function() {
  145. var b = setInterval(function() {
  146. clearInterval(b);
  147. process.nextTick(function() {
  148. fs.stat('does not exist', function() {
  149. throw new Error('fs - nested file does not exist');
  150. });
  151. expectCaught++;
  152. });
  153. });
  154. });
  155. });
  156. removeListener(listener);
  157. });
  158. // Net
  159. process.nextTick(function() {
  160. addListener(listener);
  161. var server = net.createServer(function() {
  162. server.close();
  163. throw new Error('net - connection listener');
  164. });
  165. expectCaught++;
  166. server.listen(PORT, function() {
  167. var client = net.connect(PORT, function() {
  168. client.end();
  169. throw new Error('net - client connect');
  170. });
  171. expectCaught++;
  172. throw new Error('net - server listening');
  173. });
  174. expectCaught++;
  175. removeListener(listener);
  176. });
  177. // DNS
  178. process.nextTick(function() {
  179. addListener(listener);
  180. dns.lookup('localhost', function() {
  181. throw new Error('dns - lookup');
  182. });
  183. expectCaught++;
  184. removeListener(listener);
  185. });