PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms 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
  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
 22var PORT = 12346;
 23
 24if (!process.addAsyncListener) require('../index.js');
 25if (!global.setImmediate) global.setImmediate = setTimeout;
 26
 27var assert = require('assert');
 28var dns = require('dns');
 29var fs = require('fs');
 30var net = require('net');
 31var addListener = process.addAsyncListener;
 32var removeListener = process.removeAsyncListener;
 33
 34var caught = 0;
 35var expectCaught = 0;
 36
 37function asyncL() { }
 38
 39var callbacksObj = {
 40  error: function(domain, er) {
 41    caught++;
 42
 43    switch (er.message) {
 44      case 'sync throw':
 45      case 'setTimeout - simple':
 46      case 'setImmediate - simple':
 47      case 'setInterval - simple':
 48      case 'process.nextTick - simple':
 49      case 'setTimeout - nested':
 50      case 'process.nextTick - nested':
 51      case 'setImmediate - nested':
 52      case 'setTimeout2 - nested':
 53      case 'setInterval - nested':
 54      case 'fs - file does not exist':
 55      case 'fs - nested file does not exist':
 56      case 'fs - exists':
 57      case 'fs - realpath':
 58      case 'net - connection listener':
 59      case 'net - server listening':
 60      case 'net - client connect':
 61      case 'dns - lookup':
 62        return true;
 63
 64      default:
 65        return false;
 66    }
 67  }
 68};
 69
 70process.on('exit', function() {
 71  console.log('caught:', caught);
 72  console.log('expected:', expectCaught);
 73  assert.equal(caught, expectCaught, 'caught all expected errors');
 74  console.log('ok');
 75});
 76
 77var listener = process.createAsyncListener(asyncL, callbacksObj);
 78
 79
 80// Catch synchronous throws
 81process.nextTick(function() {
 82  addListener(listener);
 83
 84  expectCaught++;
 85  throw new Error('sync throw');
 86
 87  removeListener(listener);
 88});
 89
 90
 91// Simple cases
 92process.nextTick(function() {
 93  addListener(listener);
 94
 95  setTimeout(function() {
 96    throw new Error('setTimeout - simple');
 97  });
 98  expectCaught++;
 99
100  setImmediate(function() {
101    throw new Error('setImmediate - simple');
102  });
103  expectCaught++;
104
105  var b = setInterval(function() {
106    clearInterval(b);
107    throw new Error('setInterval - simple');
108  });
109  expectCaught++;
110
111  process.nextTick(function() {
112    throw new Error('process.nextTick - simple');
113  });
114  expectCaught++;
115
116  removeListener(listener);
117});
118
119
120// Deeply nested
121process.nextTick(function() {
122  addListener(listener);
123
124  setTimeout(function() {
125    process.nextTick(function() {
126      setImmediate(function() {
127        var b = setInterval(function() {
128          clearInterval(b);
129          throw new Error('setInterval - nested');
130        });
131        expectCaught++;
132        throw new Error('setImmediate - nested');
133      });
134      expectCaught++;
135      throw new Error('process.nextTick - nested');
136    });
137    expectCaught++;
138    setTimeout(function() {
139      throw new Error('setTimeout2 - nested');
140    });
141    expectCaught++;
142    throw new Error('setTimeout - nested');
143  });
144  expectCaught++;
145
146  removeListener(listener);
147});
148
149
150// FS
151process.nextTick(function() {
152  addListener(listener);
153
154  fs.stat('does not exist', function() {
155    throw new Error('fs - file does not exist');
156  });
157  expectCaught++;
158
159  fs.exists('hi all', function() {
160    throw new Error('fs - exists');
161  });
162  expectCaught++;
163
164  fs.realpath('/some/path', function() {
165    throw new Error('fs - realpath');
166  });
167  expectCaught++;
168
169  removeListener(listener);
170});
171
172
173// Nested FS
174process.nextTick(function() {
175  addListener(listener);
176
177  setTimeout(function() {
178    setImmediate(function() {
179      var b = setInterval(function() {
180        clearInterval(b);
181        process.nextTick(function() {
182          fs.stat('does not exist', function() {
183            throw new Error('fs - nested file does not exist');
184          });
185          expectCaught++;
186        });
187      });
188    });
189  });
190
191  removeListener(listener);
192});
193
194
195// Net
196process.nextTick(function() {
197  addListener(listener);
198
199  var server = net.createServer(function() {
200    server.close();
201    throw new Error('net - connection listener');
202  });
203  expectCaught++;
204
205  server.listen(PORT, function() {
206    var client = net.connect(PORT, function() {
207      client.end();
208      throw new Error('net - client connect');
209    });
210    expectCaught++;
211    throw new Error('net - server listening');
212  });
213  expectCaught++;
214
215  removeListener(listener);
216});
217
218
219// DNS
220process.nextTick(function() {
221  addListener(listener);
222
223  dns.lookup('localhost', function() {
224    throw new Error('dns - lookup');
225  });
226  expectCaught++;
227
228  removeListener(listener);
229});