test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var BatchProcessor = require('./src/batch-processor.js');
  2. function test(expected, actual) {
  3. if (expected !== actual) {
  4. throw new Error("Expected: " + expected + ", Actual: " + actual);
  5. }
  6. }
  7. describe("BatchProcessor", function () {
  8. describe("Basic usage", function () {
  9. it("should be able to add functions and process them sync", function () {
  10. var bp = BatchProcessor({
  11. auto: false,
  12. async: false
  13. });
  14. var result = "";
  15. function f(v) {
  16. result += v;
  17. }
  18. bp.add(0, f.bind(null, "0"));
  19. bp.add(0, f.bind(null, "0"));
  20. bp.add(1, f.bind(null, "1"));
  21. bp.add(1, f.bind(null, "1"));
  22. bp.add(2, f.bind(null, "2"));
  23. bp.force();
  24. test("00112", result);
  25. });
  26. it("should be able to add functions and process them async", function (done) {
  27. var bp = BatchProcessor({
  28. auto: false,
  29. async: true
  30. });
  31. var result = "";
  32. function f(v) {
  33. result += v;
  34. }
  35. bp.add(0, f.bind(null, "0"));
  36. bp.add(0, f.bind(null, "0"));
  37. bp.add(1, f.bind(null, "1"));
  38. bp.add(1, f.bind(null, "1"));
  39. bp.add(2, f.bind(null, "2"));
  40. bp.force();
  41. test("", result);
  42. setTimeout(function () {
  43. test("00112", result);
  44. done();
  45. }, 10);
  46. });
  47. it("should be able to add functions and process them auto async", function (done) {
  48. var bp = BatchProcessor({
  49. auto: true,
  50. async: true
  51. });
  52. var result = "";
  53. function f(v) {
  54. result += v;
  55. }
  56. bp.add(0, f.bind(null, "0"));
  57. bp.add(0, f.bind(null, "0"));
  58. bp.add(1, f.bind(null, "1"));
  59. bp.add(1, f.bind(null, "1"));
  60. bp.add(2, f.bind(null, "2"));
  61. test("", result);
  62. setTimeout(function () {
  63. test("00112", result);
  64. done();
  65. }, 10);
  66. });
  67. it("should report a warning when auto sync, and default to auto async", function (done) {
  68. var warn = "";
  69. var bp = BatchProcessor({
  70. auto: true,
  71. async: false,
  72. reporter: {
  73. warn: function () {
  74. warn = "called";
  75. }
  76. }
  77. });
  78. test("called", warn);
  79. var result = "";
  80. function f(v) {
  81. result += v;
  82. }
  83. bp.add(0, f.bind(null, "0"));
  84. bp.add(0, f.bind(null, "0"));
  85. bp.add(1, f.bind(null, "1"));
  86. bp.add(1, f.bind(null, "1"));
  87. bp.add(2, f.bind(null, "2"));
  88. test("", result);
  89. setTimeout(function () {
  90. test("00112", result);
  91. done();
  92. }, 10);
  93. });
  94. });
  95. describe("Advanced usage", function () {
  96. it("When processing, incoming functions should be processed directly after that the current batch is finished", function () {
  97. var bp = BatchProcessor({
  98. auto: false,
  99. async: false
  100. });
  101. var result = "";
  102. function f(v) {
  103. result += v;
  104. }
  105. bp.add(0, f.bind(null, "0"));
  106. bp.add(0, f.bind(null, "0"));
  107. bp.add(1, f.bind(null, "1"));
  108. bp.add(1, function () {
  109. f("1");
  110. bp.add(-1, f.bind(null, "a"));
  111. bp.add(0, f.bind(null, "b"));
  112. bp.add(1, f.bind(null, "c"));
  113. bp.add(2, f.bind(null, "d"));
  114. bp.add(3, f.bind(null, "e"));
  115. bp.force();
  116. });
  117. bp.add(2, f.bind(null, "2"));
  118. bp.force();
  119. test("00112abcde", result);
  120. });
  121. });
  122. });