Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

waterfall.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Tests the "waterfall" primitive.
  3. */
  4. var mod_tap = require('tap');
  5. var mod_vasync = require('..');
  6. var count = 0;
  7. var st;
  8. mod_tap.test('empty waterfall', function (test) {
  9. st = mod_vasync.waterfall([], function (err) {
  10. test.ok(err === null);
  11. test.ok(st.ndone === 0);
  12. test.ok(st.nerrors === 0);
  13. test.ok(st.operations.length === 0);
  14. test.ok(st.successes.length === 0);
  15. test.equal(count, 1);
  16. test.end();
  17. });
  18. count++;
  19. test.ok(st.ndone === 0);
  20. test.ok(st.nerrors === 0);
  21. test.ok(st.operations.length === 0);
  22. test.ok(st.successes.length === 0);
  23. });
  24. mod_tap.test('normal 4-stage waterfall', function (test) {
  25. count = 0;
  26. st = mod_vasync.waterfall([
  27. function func1(cb) {
  28. test.ok(count === 0, 'func1: count === 0');
  29. test.ok(st.ndone === 0);
  30. count++;
  31. setTimeout(cb, 20, null, { 'hello': 'world' });
  32. },
  33. function func2(extra, cb) {
  34. test.equal(extra.hello, 'world', 'func2: extra arg');
  35. test.ok(count == 1, 'func2: count == 1');
  36. test.ok(st.ndone === 1);
  37. test.ok(st.operations[0].status == 'ok');
  38. test.ok(st.operations[1].status == 'pending');
  39. test.ok(st.operations[2].status == 'waiting');
  40. count++;
  41. setTimeout(cb, 20, null, 5, 6, 7);
  42. },
  43. function (five, six, seven, cb) {
  44. test.equal(five, 5, 'func3: extra arg');
  45. test.equal(six, 6, 'func3: extra arg');
  46. test.equal(seven, 7, 'func3: extra arg');
  47. test.ok(count == 2, 'func3: count == 2');
  48. test.ok(st.ndone === 2);
  49. count++;
  50. setTimeout(cb, 20);
  51. },
  52. function func4(cb) {
  53. test.ok(count == 3, 'func4: count == 2');
  54. test.ok(st.ndone === 3);
  55. count++;
  56. setTimeout(cb, 20, null, 8, 9);
  57. }
  58. ], function (err, eight, nine) {
  59. test.ok(count == 4, 'final: count == 4');
  60. test.ok(err === null, 'no error');
  61. test.ok(eight == 8);
  62. test.ok(nine == 9);
  63. test.ok(st.ndone === 4);
  64. test.ok(st.nerrors === 0);
  65. test.ok(st.operations.length === 4);
  66. test.ok(st.successes.length === 4);
  67. test.ok(st.operations[0].status == 'ok');
  68. test.ok(st.operations[1].status == 'ok');
  69. test.ok(st.operations[2].status == 'ok');
  70. test.ok(st.operations[3].status == 'ok');
  71. test.end();
  72. });
  73. test.ok(st.ndone === 0);
  74. test.ok(st.nerrors === 0);
  75. test.ok(st.operations.length === 4);
  76. test.ok(st.operations[0].funcname == 'func1', 'func1 name');
  77. test.ok(st.operations[0].status == 'pending');
  78. test.ok(st.operations[1].funcname == 'func2', 'func2 name');
  79. test.ok(st.operations[1].status == 'waiting');
  80. test.ok(st.operations[2].funcname == '(anon)', 'anon name');
  81. test.ok(st.operations[2].status == 'waiting');
  82. test.ok(st.operations[3].funcname == 'func4', 'func4 name');
  83. test.ok(st.operations[3].status == 'waiting');
  84. test.ok(st.successes.length === 0);
  85. });
  86. mod_tap.test('bailing out early', function (test) {
  87. count = 0;
  88. st = mod_vasync.waterfall([
  89. function func1(cb) {
  90. test.ok(count === 0, 'func1: count === 0');
  91. count++;
  92. setTimeout(cb, 20);
  93. },
  94. function func2(cb) {
  95. test.ok(count == 1, 'func2: count == 1');
  96. count++;
  97. setTimeout(cb, 20, new Error('boom!'));
  98. },
  99. function func3(cb) {
  100. test.ok(count == 2, 'func3: count == 2');
  101. count++;
  102. setTimeout(cb, 20);
  103. }
  104. ], function (err) {
  105. test.ok(count == 2, 'final: count == 3');
  106. test.equal(err.message, 'boom!');
  107. test.ok(st.ndone == 2);
  108. test.ok(st.nerrors == 1);
  109. test.ok(st.operations[0].status == 'ok');
  110. test.ok(st.operations[1].status == 'fail');
  111. test.ok(st.operations[2].status == 'waiting');
  112. test.ok(st.successes.length == 1);
  113. test.end();
  114. });
  115. });
  116. mod_tap.test('bad function', function (test) {
  117. count = 0;
  118. st = mod_vasync.waterfall([
  119. function func1(cb) {
  120. count++;
  121. cb();
  122. setTimeout(function () {
  123. test.throws(
  124. function () { cb(); process.abort(); },
  125. 'vasync.waterfall: ' +
  126. 'function 0 ("func1") invoked its ' +
  127. 'callback twice');
  128. test.equal(count, 2);
  129. test.end();
  130. }, 100);
  131. },
  132. function func2(cb) {
  133. count++;
  134. /* do nothing -- we'll throw an exception first */
  135. }
  136. ], function (err) {
  137. /* not reached */
  138. console.error('didn\'t expect to finish');
  139. process.abort();
  140. });
  141. });
  142. mod_tap.test('badargs', function (test) {
  143. test.throws(function () { mod_vasync.waterfall(); });
  144. test.throws(function () { mod_vasync.waterfall([], 'foo'); });
  145. test.throws(function () { mod_vasync.waterfall('foo', 'bar'); });
  146. test.end();
  147. });
  148. mod_tap.test('normal waterfall, no callback', function (test) {
  149. count = 0;
  150. st = mod_vasync.waterfall([
  151. function func1(cb) {
  152. test.ok(count === 0);
  153. count++;
  154. setImmediate(cb);
  155. },
  156. function func2(cb) {
  157. test.ok(count == 1);
  158. count++;
  159. setImmediate(cb);
  160. setTimeout(function () {
  161. test.ok(count == 2);
  162. test.end();
  163. }, 100);
  164. }
  165. ]);
  166. });
  167. mod_tap.test('empty waterfall, no callback', function (test) {
  168. st = mod_vasync.waterfall([]);
  169. setTimeout(function () { test.end(); }, 100);
  170. });