PageRenderTime 209ms CodeModel.GetById 149ms app.highlight 8ms RepoModel.GetById 50ms app.codeStats 0ms

/node_modules/express/node_modules/connect/node_modules/multiparty/node_modules/readable-stream/test/simple/test-stream-pipe-error-handling.js

https://github.com/nexcafe/N-blog
JavaScript | 131 lines | 87 code | 23 blank | 21 comment | 0 complexity | d45096a27b95657ff25370797116f7c9 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 common = require('../common');
 23var assert = require('assert');
 24var Stream = require('../../').Stream;
 25
 26(function testErrorListenerCatches() {
 27  var source = new Stream();
 28  var dest = new Stream();
 29
 30  source.pipe(dest);
 31
 32  var gotErr = null;
 33  source.on('error', function(err) {
 34    gotErr = err;
 35  });
 36
 37  var err = new Error('This stream turned into bacon.');
 38  source.emit('error', err);
 39  assert.strictEqual(gotErr, err);
 40})();
 41
 42(function testErrorWithoutListenerThrows() {
 43  var source = new Stream();
 44  var dest = new Stream();
 45
 46  source.pipe(dest);
 47
 48  var err = new Error('This stream turned into bacon.');
 49
 50  var gotErr = null;
 51  try {
 52    source.emit('error', err);
 53  } catch (e) {
 54    gotErr = e;
 55  }
 56
 57  assert.strictEqual(gotErr, err);
 58})();
 59
 60(function testErrorWithRemovedListenerThrows() {
 61  var EE = require('events').EventEmitter;
 62  var R = Stream.Readable;
 63  var W = Stream.Writable;
 64
 65  var r = new R;
 66  var w = new W;
 67  var removed = false;
 68  var didTest = false;
 69
 70  process.on('exit', function() {
 71    assert(didTest);
 72    console.log('ok');
 73  });
 74
 75  r._read = function() {
 76    setTimeout(function() {
 77      assert(removed);
 78      assert.throws(function() {
 79        w.emit('error', new Error('fail'));
 80      });
 81      didTest = true;
 82    });
 83  };
 84
 85  w.on('error', myOnError);
 86  r.pipe(w);
 87  w.removeListener('error', myOnError);
 88  removed = true;
 89
 90  function myOnError(er) {
 91    throw new Error('this should not happen');
 92  }
 93})();
 94
 95(function testErrorWithRemovedListenerThrows() {
 96  var EE = require('events').EventEmitter;
 97  var R = Stream.Readable;
 98  var W = Stream.Writable;
 99
100  var r = new R;
101  var w = new W;
102  var removed = false;
103  var didTest = false;
104  var caught = false;
105
106  process.on('exit', function() {
107    assert(didTest);
108    console.log('ok');
109  });
110
111  r._read = function() {
112    setTimeout(function() {
113      assert(removed);
114      w.emit('error', new Error('fail'));
115      didTest = true;
116    });
117  };
118
119  w.on('error', myOnError);
120  w._write = function() {};
121
122  r.pipe(w);
123  // Removing some OTHER random listener should not do anything
124  w.removeListener('error', function() {});
125  removed = true;
126
127  function myOnError(er) {
128    assert(!caught);
129    caught = true;
130  }
131})();