123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- var BatchProcessor = require('./src/batch-processor.js');
- function test(expected, actual) {
- if (expected !== actual) {
- throw new Error("Expected: " + expected + ", Actual: " + actual);
- }
- }
- describe("BatchProcessor", function () {
- describe("Basic usage", function () {
- it("should be able to add functions and process them sync", function () {
- var bp = BatchProcessor({
- auto: false,
- async: false
- });
- var result = "";
- function f(v) {
- result += v;
- }
- bp.add(0, f.bind(null, "0"));
- bp.add(0, f.bind(null, "0"));
- bp.add(1, f.bind(null, "1"));
- bp.add(1, f.bind(null, "1"));
- bp.add(2, f.bind(null, "2"));
- bp.force();
- test("00112", result);
- });
- it("should be able to add functions and process them async", function (done) {
- var bp = BatchProcessor({
- auto: false,
- async: true
- });
- var result = "";
- function f(v) {
- result += v;
- }
- bp.add(0, f.bind(null, "0"));
- bp.add(0, f.bind(null, "0"));
- bp.add(1, f.bind(null, "1"));
- bp.add(1, f.bind(null, "1"));
- bp.add(2, f.bind(null, "2"));
- bp.force();
- test("", result);
- setTimeout(function () {
- test("00112", result);
- done();
- }, 10);
- });
- it("should be able to add functions and process them auto async", function (done) {
- var bp = BatchProcessor({
- auto: true,
- async: true
- });
- var result = "";
- function f(v) {
- result += v;
- }
- bp.add(0, f.bind(null, "0"));
- bp.add(0, f.bind(null, "0"));
- bp.add(1, f.bind(null, "1"));
- bp.add(1, f.bind(null, "1"));
- bp.add(2, f.bind(null, "2"));
- test("", result);
- setTimeout(function () {
- test("00112", result);
- done();
- }, 10);
- });
- it("should report a warning when auto sync, and default to auto async", function (done) {
- var warn = "";
- var bp = BatchProcessor({
- auto: true,
- async: false,
- reporter: {
- warn: function () {
- warn = "called";
- }
- }
- });
- test("called", warn);
- var result = "";
- function f(v) {
- result += v;
- }
- bp.add(0, f.bind(null, "0"));
- bp.add(0, f.bind(null, "0"));
- bp.add(1, f.bind(null, "1"));
- bp.add(1, f.bind(null, "1"));
- bp.add(2, f.bind(null, "2"));
- test("", result);
- setTimeout(function () {
- test("00112", result);
- done();
- }, 10);
- });
- });
- describe("Advanced usage", function () {
- it("When processing, incoming functions should be processed directly after that the current batch is finished", function () {
- var bp = BatchProcessor({
- auto: false,
- async: false
- });
- var result = "";
- function f(v) {
- result += v;
- }
- bp.add(0, f.bind(null, "0"));
- bp.add(0, f.bind(null, "0"));
- bp.add(1, f.bind(null, "1"));
- bp.add(1, function () {
- f("1");
- bp.add(-1, f.bind(null, "a"));
- bp.add(0, f.bind(null, "b"));
- bp.add(1, f.bind(null, "c"));
- bp.add(2, f.bind(null, "d"));
- bp.add(3, f.bind(null, "e"));
- bp.force();
- });
- bp.add(2, f.bind(null, "2"));
- bp.force();
- test("00112abcde", result);
- });
- });
- });
|