2019-08-15 21:06:54 +00:00
|
|
|
import { Readable } from "stream";
|
|
|
|
import test from "ava";
|
|
|
|
import { expect } from "chai";
|
2019-12-02 21:02:40 +00:00
|
|
|
import mhysa from "../src";
|
|
|
|
const { batch } = mhysa({ objectMode: true });
|
2019-08-15 21:06:54 +00:00
|
|
|
|
|
|
|
test.cb("batch() batches chunks together", t => {
|
|
|
|
t.plan(3);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
const expectedElements = [["a", "b", "c"], ["d", "e", "f"], ["g"]];
|
|
|
|
let i = 0;
|
|
|
|
source
|
|
|
|
.pipe(batch(3))
|
|
|
|
.on("data", (element: string[]) => {
|
2019-08-16 14:01:55 +00:00
|
|
|
t.deepEqual(element, expectedElements[i]);
|
2019-08-15 21:06:54 +00:00
|
|
|
i++;
|
|
|
|
})
|
|
|
|
.on("error", t.end)
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push("d");
|
|
|
|
source.push("e");
|
|
|
|
source.push("f");
|
|
|
|
source.push("g");
|
|
|
|
source.push(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.cb("batch() yields a batch after the timeout", t => {
|
|
|
|
t.plan(3);
|
|
|
|
const source = new Readable({
|
|
|
|
objectMode: true,
|
2019-12-02 21:02:40 +00:00
|
|
|
read(size: number) {
|
|
|
|
return;
|
|
|
|
},
|
2019-08-15 21:06:54 +00:00
|
|
|
});
|
|
|
|
const expectedElements = [["a", "b"], ["c"], ["d"]];
|
|
|
|
let i = 0;
|
|
|
|
source
|
|
|
|
.pipe(batch(3))
|
|
|
|
.on("data", (element: string[]) => {
|
2019-08-16 14:01:55 +00:00
|
|
|
t.deepEqual(element, expectedElements[i]);
|
2019-08-15 21:06:54 +00:00
|
|
|
i++;
|
|
|
|
})
|
|
|
|
.on("error", t.fail)
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
setTimeout(() => {
|
|
|
|
source.push("c");
|
|
|
|
}, 600);
|
|
|
|
setTimeout(() => {
|
|
|
|
source.push("d");
|
|
|
|
source.push(null);
|
|
|
|
}, 600 * 2);
|
|
|
|
});
|