test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var assert = require('assert');
  2. var Pend = require('./');
  3. var tests = [
  4. {
  5. name: "basic",
  6. fn: testBasic,
  7. },
  8. {
  9. name: "max",
  10. fn: testWithMax,
  11. },
  12. {
  13. name: "callback twice",
  14. fn: testCallbackTwice,
  15. },
  16. {
  17. name: "calling wait twice",
  18. fn: testCallingWaitTwice,
  19. },
  20. {
  21. name: "hold()",
  22. fn: testHoldFn,
  23. },
  24. ];
  25. var testCount = tests.length;
  26. doOneTest();
  27. function doOneTest() {
  28. var test = tests.shift();
  29. if (!test) {
  30. console.log(testCount + " tests passed.");
  31. return;
  32. }
  33. process.stdout.write(test.name + "...");
  34. test.fn(function() {
  35. process.stdout.write("OK\n");
  36. doOneTest();
  37. });
  38. }
  39. function testBasic(cb) {
  40. var pend = new Pend();
  41. var results = [];
  42. pend.go(function(cb) {
  43. results.push(1);
  44. setTimeout(function() {
  45. results.push(3);
  46. cb();
  47. }, 500);
  48. });
  49. pend.go(function(cb) {
  50. results.push(2);
  51. setTimeout(function() {
  52. results.push(4);
  53. cb();
  54. }, 1000);
  55. });
  56. pend.wait(function(err) {
  57. assert.deepEqual(results, [1,2,3,4]);
  58. cb();
  59. });
  60. assert.deepEqual(results, [1, 2]);
  61. }
  62. function testWithMax(cb) {
  63. var pend = new Pend();
  64. var results = [];
  65. pend.max = 2;
  66. pend.go(function(cb) {
  67. results.push('a');
  68. setTimeout(function() {
  69. results.push(1);
  70. cb();
  71. }, 500);
  72. });
  73. pend.go(function(cb) {
  74. results.push('b');
  75. setTimeout(function() {
  76. results.push(1);
  77. cb();
  78. }, 500);
  79. });
  80. pend.go(function(cb) {
  81. results.push('c');
  82. setTimeout(function() {
  83. results.push(2);
  84. cb();
  85. }, 100);
  86. });
  87. pend.wait(function(err) {
  88. assert.deepEqual(results, ['a', 'b', 1, 'c', 1, 2]);
  89. cb();
  90. });
  91. assert.deepEqual(results, ['a', 'b']);
  92. }
  93. function testCallbackTwice(cb) {
  94. var pend = new Pend();
  95. pend.go(function(cb) {
  96. setTimeout(cb, 100);
  97. });
  98. pend.go(function(cb) {
  99. cb();
  100. assert.throws(cb, /callback called twice/);
  101. });
  102. pend.wait(cb);
  103. }
  104. function testCallingWaitTwice(cb) {
  105. var pend = new Pend();
  106. pend.go(function(cb) {
  107. setTimeout(cb, 100);
  108. });
  109. pend.wait(function() {
  110. pend.go(function(cb) {
  111. setTimeout(cb, 50);
  112. });
  113. pend.go(function(cb) {
  114. setTimeout(cb, 10);
  115. });
  116. pend.go(function(cb) {
  117. setTimeout(cb, 20);
  118. });
  119. pend.wait(cb);
  120. });
  121. }
  122. function testHoldFn(cb) {
  123. var pend = new Pend();
  124. setTimeout(pend.hold(), 100);
  125. pend.go(function(cb) {
  126. cb();
  127. });
  128. pend.wait(cb);
  129. }