tests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. function equals(a, b) {
  3. return !!(JSON.stringify(a) === JSON.stringify(b));
  4. }
  5. Tinytest.add('PowerQueue - scope', function(test) {
  6. test.isTrue(typeof PowerQueue !== 'undefined', 'The PowerQueue scope is missing, please add the power-queue package');
  7. });
  8. // We run 5 tasks in serial mode
  9. Tinytest.addAsync('PowerQueue - test serial run', function (test, onComplete) {
  10. var queue = new PowerQueue({
  11. name: 'test queue 1',
  12. autostart: false,
  13. maxProcessing: 1,
  14. debug: true,
  15. // When this task is released we do our tests
  16. onEnded: function() {
  17. console.log('It ended');
  18. // Check that we ran the expected number of tasks
  19. test.equal(counter, 5, 'counter did not match number of tasks');
  20. // Check that the result was correct
  21. test.equal(result, expectedResult, 'result was unexpected');
  22. // We are done testing
  23. onComplete();
  24. }
  25. });
  26. var result = '';
  27. var expectedResult = '12345';
  28. var counter = 0;
  29. var checkCounter = function(id, next) {
  30. console.log('test queue 1 - Run task: ' + id);
  31. // Keep a counter
  32. counter++;
  33. // push id to result
  34. result += id;
  35. // call next task
  36. next();
  37. };
  38. // Add the tasks to the queue
  39. queue.add(function(next) { checkCounter('1', next); });
  40. queue.add(function(next) { checkCounter('2', next); });
  41. queue.add(function(next) { checkCounter('3', next); });
  42. queue.add(function(next) { checkCounter('4', next); });
  43. queue.add(function(next) { checkCounter('5', next); });
  44. // Run the queue
  45. queue.run();
  46. });
  47. // We run 5 tasks in serial mode but pause the queue on 3
  48. Tinytest.addAsync('PowerQueue - test serial pause', function (test, onComplete) {
  49. var queue = new PowerQueue({
  50. name: 'test queue 2',
  51. autostart: false,
  52. maxProcessing: 1,
  53. debug: true,
  54. // When this task is released we do our tests
  55. onPaused: function() {
  56. console.log('Its paused');
  57. // Check that we ran the expected number of tasks
  58. test.equal(counter, 3, 'counter did not match number of tasks');
  59. // Check that the result was correct
  60. test.equal(result, expectedResult, 'result was unexpected');
  61. // We are done testing
  62. onComplete();
  63. }
  64. });
  65. var result = '';
  66. var expectedResult = '123';
  67. var counter = 0;
  68. var checkCounter = function(id, next) {
  69. console.log('test queue 2 - Run task: ' + id);
  70. // Keep a counter
  71. counter++;
  72. // push id to result
  73. result += id;
  74. // call next task
  75. if (id === '3')
  76. next('pause')
  77. else
  78. next();
  79. };
  80. // Add the tasks to the queue
  81. queue.add(function(next) { checkCounter('1', next); });
  82. queue.add(function(next) { checkCounter('2', next); });
  83. queue.add(function(next) { checkCounter('3', next); });
  84. queue.add(function(next) { checkCounter('4', next); });
  85. queue.add(function(next) { checkCounter('5', next); });
  86. // Run the queue
  87. queue.run();
  88. });
  89. // We run 5 tasks in serial mode but pause the queue on 3
  90. Tinytest.addAsync('PowerQueue - test 2 task in parallel', function (test, onComplete) {
  91. var queue = new PowerQueue({
  92. name: 'test queue 3',
  93. autostart: false,
  94. maxProcessing: 2,
  95. debug: true,
  96. // When this task is released we do our tests
  97. onEnded: function() {
  98. console.log('Its paused');
  99. // Check that we ran the expected number of tasks
  100. test.equal(counter, 10, 'counter did not match number of tasks');
  101. // Check that the result was correct
  102. test.equal(result, expectedResult, 'result was unexpected');
  103. // We are done testing
  104. onComplete();
  105. }
  106. });
  107. // start 1-----3-------4-------6------------------------9-----------------------X
  108. // 2-----------------5---------------7--------8-----------10------X
  109. // ms 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
  110. // result 1 2 3 4 5 6 7 8 9 10
  111. // result 1 3 2 4 5 7 6 8 10 9
  112. var wait = {
  113. '1': 10,
  114. '2': 25,
  115. '3': 10,
  116. '4': 10,
  117. '5': 20,
  118. '6': 30,
  119. '7': 10,
  120. '8': 15,
  121. '9': 30,
  122. '10': 10,
  123. };
  124. // 1324
  125. var result = '';
  126. var expectedResult = '13245768109';
  127. var counter = 0;
  128. var checkCounter = function(id, next) {
  129. console.log('test queue 3 - Run task: ' + id);
  130. // Keep a counter
  131. counter++;
  132. // push id to result
  133. Meteor.setTimeout(function() {
  134. result += id;
  135. // call next task
  136. next();
  137. }, wait[id] * 5); // give it a factor 2 to make sure we get the correct result
  138. };
  139. // Add the tasks to the queue
  140. queue.add(function(next) { checkCounter('1', next); });
  141. queue.add(function(next) { checkCounter('2', next); });
  142. queue.add(function(next) { checkCounter('3', next); });
  143. queue.add(function(next) { checkCounter('4', next); });
  144. queue.add(function(next) { checkCounter('5', next); });
  145. queue.add(function(next) { checkCounter('6', next); });
  146. queue.add(function(next) { checkCounter('7', next); });
  147. queue.add(function(next) { checkCounter('8', next); });
  148. queue.add(function(next) { checkCounter('9', next); });
  149. queue.add(function(next) { checkCounter('10', next); });
  150. // Run the queue
  151. queue.run();
  152. });
  153. //Test API:
  154. //test.isFalse(v, msg)
  155. //test.isTrue(v, msg)
  156. //test.equal(actual, expected, message, not)
  157. //test.length(obj, len)
  158. //test.include(s, v)
  159. //test.isNaN(v, msg)
  160. //test.isUndefined(v, msg)
  161. //test.isNotNull
  162. //test.isNull
  163. //test.throws(func)
  164. //test.instanceOf(obj, klass)
  165. //test.notEqual(actual, expected, message)
  166. //test.runId()
  167. //test.exception(exception)
  168. //test.expect_fail()
  169. //test.ok(doc)
  170. //test.fail(doc)
  171. //test.equal(a, b, msg)