PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): MIT, Apache-2.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 Stream = require('../../').Stream;
  24. (function testErrorListenerCatches() {
  25. var source = new Stream();
  26. var dest = new Stream();
  27. source.pipe(dest);
  28. var gotErr = null;
  29. source.on('error', function(err) {
  30. gotErr = err;
  31. });
  32. var err = new Error('This stream turned into bacon.');
  33. source.emit('error', err);
  34. assert.strictEqual(gotErr, err);
  35. })();
  36. (function testErrorWithoutListenerThrows() {
  37. var source = new Stream();
  38. var dest = new Stream();
  39. source.pipe(dest);
  40. var err = new Error('This stream turned into bacon.');
  41. var gotErr = null;
  42. try {
  43. source.emit('error', err);
  44. } catch (e) {
  45. gotErr = e;
  46. }
  47. assert.strictEqual(gotErr, err);
  48. })();
  49. (function testErrorWithRemovedListenerThrows() {
  50. var EE = require('events').EventEmitter;
  51. var R = Stream.Readable;
  52. var W = Stream.Writable;
  53. var r = new R;
  54. var w = new W;
  55. var removed = false;
  56. var didTest = false;
  57. process.on('exit', function() {
  58. assert(didTest);
  59. console.log('ok');
  60. });
  61. r._read = function() {
  62. setTimeout(function() {
  63. assert(removed);
  64. assert.throws(function() {
  65. w.emit('error', new Error('fail'));
  66. });
  67. didTest = true;
  68. });
  69. };
  70. w.on('error', myOnError);
  71. r.pipe(w);
  72. w.removeListener('error', myOnError);
  73. removed = true;
  74. function myOnError(er) {
  75. throw new Error('this should not happen');
  76. }
  77. })();
  78. (function testErrorWithRemovedListenerThrows() {
  79. var EE = require('events').EventEmitter;
  80. var R = Stream.Readable;
  81. var W = Stream.Writable;
  82. var r = new R;
  83. var w = new W;
  84. var removed = false;
  85. var didTest = false;
  86. var caught = false;
  87. process.on('exit', function() {
  88. assert(didTest);
  89. console.log('ok');
  90. });
  91. r._read = function() {
  92. setTimeout(function() {
  93. assert(removed);
  94. w.emit('error', new Error('fail'));
  95. didTest = true;
  96. });
  97. };
  98. w.on('error', myOnError);
  99. w._write = function() {};
  100. r.pipe(w);
  101. // Removing some OTHER random listener should not do anything
  102. w.removeListener('error', function() {});
  103. removed = true;
  104. function myOnError(er) {
  105. assert(!caught);
  106. caught = true;
  107. }
  108. })();