strom/tests/demux.spec.ts

551 lines
16 KiB
TypeScript
Raw Normal View History

2019-08-28 21:01:51 +00:00
import test from "ava";
import { expect } from "chai";
2019-09-09 15:53:21 +00:00
const { demux, map } = require("../src");
2019-08-29 12:50:11 +00:00
import { Writable } from "stream";
2019-09-09 15:53:21 +00:00
const sinon = require("sinon");
const { sleep } = require("../src/helpers");
import { performance } from "perf_hooks";
2019-08-28 21:01:51 +00:00
interface Test {
key: string;
2019-09-09 15:53:21 +00:00
visited: number[];
2019-08-28 21:01:51 +00:00
}
2019-09-09 18:43:18 +00:00
2019-09-09 15:53:21 +00:00
test.cb("demux() constructor should be called once per key", t => {
t.plan(1);
2019-08-28 21:01:51 +00:00
const input = [
2019-09-09 15:53:21 +00:00
{ key: "a", visited: [] },
{ key: "b", visited: [] },
{ key: "a", visited: [] },
{ key: "c", visited: [] },
{ key: "a", visited: [] },
{ key: "b", visited: [] },
2019-08-28 21:01:51 +00:00
];
2019-09-09 15:53:21 +00:00
const construct = sinon.spy((destKey: string) => {
2019-08-29 12:50:11 +00:00
const dest = map((chunk: Test) => {
2019-09-09 15:53:21 +00:00
chunk.visited.push(1);
return chunk;
2019-08-29 12:50:11 +00:00
});
2019-08-28 21:01:51 +00:00
return dest;
2019-09-09 15:53:21 +00:00
});
2019-08-28 21:01:51 +00:00
const demuxed = demux(construct, { key: "key" }, { objectMode: true });
2019-09-09 15:53:21 +00:00
2019-08-28 21:01:51 +00:00
demuxed.on("finish", () => {
2019-09-09 15:53:21 +00:00
expect(construct.withArgs("a").callCount).to.equal(1);
expect(construct.withArgs("b").callCount).to.equal(1);
expect(construct.withArgs("c").callCount).to.equal(1);
2019-08-28 21:01:51 +00:00
t.pass();
t.end();
2019-08-28 21:01:51 +00:00
});
input.forEach(event => demuxed.write(event));
demuxed.end();
});
2019-08-28 21:04:31 +00:00
test.cb("demux() should send input through correct pipeline", t => {
t.plan(6);
const input = [
{ key: "a", visited: [] },
{ key: "b", visited: [] },
{ key: "a", visited: [] },
{ key: "c", visited: [] },
{ key: "a", visited: [] },
{ key: "b", visited: [] },
];
const pipelineSpies = {};
const construct = (destKey: string) => {
const mapper = sinon.spy((chunk: Test) => {
return { ...chunk, visited: [1] };
});
const dest = map(mapper);
pipelineSpies[destKey] = mapper;
return dest;
};
const demuxed = demux(construct, { key: "key" }, { objectMode: true });
demuxed.on("finish", () => {
pipelineSpies["a"].getCalls().forEach(call => {
expect(call.args[0].key).to.equal("a");
t.pass();
});
pipelineSpies["b"].getCalls().forEach(call => {
expect(call.args[0].key).to.equal("b");
t.pass();
});
pipelineSpies["c"].getCalls().forEach(call => {
expect(call.args[0].key).to.equal("c");
t.pass();
});
t.end();
});
input.forEach(event => demuxed.write(event));
demuxed.end();
});
2019-09-09 15:53:21 +00:00
test.cb("demux() constructor should be called once per key using keyBy", t => {
t.plan(1);
2019-08-28 21:04:31 +00:00
const input = [
2019-09-09 15:53:21 +00:00
{ key: "a", visited: [] },
{ key: "b", visited: [] },
{ key: "a", visited: [] },
{ key: "c", visited: [] },
{ key: "a", visited: [] },
{ key: "b", visited: [] },
2019-08-28 21:04:31 +00:00
];
2019-09-09 15:53:21 +00:00
const construct = sinon.spy((destKey: string) => {
2019-08-29 12:50:11 +00:00
const dest = map((chunk: Test) => {
2019-09-09 15:53:21 +00:00
chunk.visited.push(1);
return chunk;
2019-08-29 12:50:11 +00:00
});
2019-08-28 21:04:31 +00:00
return dest;
2019-09-09 15:53:21 +00:00
});
2019-08-28 21:04:31 +00:00
const demuxed = demux(
construct,
2019-09-09 15:53:21 +00:00
{ keyBy: item => item.key },
2019-08-28 21:04:31 +00:00
{ objectMode: true },
);
2019-09-09 15:53:21 +00:00
2019-08-28 21:04:31 +00:00
demuxed.on("finish", () => {
2019-09-09 15:53:21 +00:00
expect(construct.withArgs("a").callCount).to.equal(1);
expect(construct.withArgs("b").callCount).to.equal(1);
expect(construct.withArgs("c").callCount).to.equal(1);
2019-08-28 21:04:31 +00:00
t.pass();
t.end();
2019-08-28 21:04:31 +00:00
});
input.forEach(event => demuxed.write(event));
demuxed.end();
});
2019-08-30 13:33:29 +00:00
test.cb("demux() should send input through correct pipeline using keyBy", t => {
t.plan(6);
const input = [
{ key: "a", visited: [] },
{ key: "b", visited: [] },
{ key: "a", visited: [] },
{ key: "c", visited: [] },
{ key: "a", visited: [] },
{ key: "b", visited: [] },
];
const pipelineSpies = {};
const construct = (destKey: string) => {
const mapper = sinon.spy((chunk: Test) => {
return { ...chunk, visited: [1] };
});
const dest = map(mapper);
pipelineSpies[destKey] = mapper;
return dest;
};
const demuxed = demux(
construct,
{ keyBy: item => item.key },
{ objectMode: true },
);
demuxed.on("finish", () => {
pipelineSpies["a"].getCalls().forEach(call => {
expect(call.args[0].key).to.equal("a");
t.pass();
});
pipelineSpies["b"].getCalls().forEach(call => {
expect(call.args[0].key).to.equal("b");
t.pass();
});
pipelineSpies["c"].getCalls().forEach(call => {
expect(call.args[0].key).to.equal("c");
t.pass();
});
t.end();
});
input.forEach(event => demuxed.write(event));
demuxed.end();
});
test("demux() when write returns false, drain event should be emitted after at least slowProcessorSpeed * highWaterMark", t => {
2019-09-09 15:53:21 +00:00
return new Promise(async (resolve, reject) => {
t.plan(7);
interface Chunk {
key: string;
mapped: number[];
}
const input: Chunk[] = [
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
];
let pendingReads = input.length;
const highWaterMark = 5;
const slowProcessorSpeed = 25;
2019-09-09 15:53:21 +00:00
const sink = new Writable({
objectMode: true,
write(chunk, encoding, cb) {
cb();
t.pass();
pendingReads--;
if (pendingReads === 0) {
resolve();
}
},
});
const construct = (destKey: string) => {
2019-09-11 18:29:20 +00:00
const first = map(
async (chunk: Chunk) => {
await sleep(slowProcessorSpeed);
chunk.mapped.push(1);
return chunk;
},
{ highWaterMark: 1, objectMode: true },
);
2019-09-09 15:53:21 +00:00
const second = map(async (chunk: Chunk) => {
chunk.mapped.push(2);
return chunk;
});
first.pipe(second).pipe(sink);
return first;
};
const _demux = demux(
construct,
{ key: "key" },
{
objectMode: true,
highWaterMark,
},
);
_demux.on("error", err => {
reject();
});
let start = null;
2019-09-09 15:53:21 +00:00
for (const item of input) {
const res = _demux.write(item);
expect(_demux._writableState.length).to.be.at.most(highWaterMark);
if (!res) {
start = performance.now();
await new Promise((resolv, rej) => {
_demux.once("drain", () => {
2019-09-11 18:29:20 +00:00
expect(_demux._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.greaterThan(
slowProcessorSpeed * highWaterMark,
);
t.pass();
resolv();
});
});
2019-09-09 15:53:21 +00:00
}
}
});
});
test("demux() should emit one drain event when writing 6 items with highWaterMark of 5", t => {
return new Promise(async (resolve, reject) => {
t.plan(7);
2019-09-09 15:53:21 +00:00
interface Chunk {
key: string;
mapped: number[];
}
const highWaterMark = 5;
const input = [
2019-09-11 18:29:20 +00:00
{ key: "a", val: 1, mapped: [] },
{ key: "a", val: 2, mapped: [] },
{ key: "a", val: 3, mapped: [] },
{ key: "a", val: 4, mapped: [] },
{ key: "a", val: 5, mapped: [] },
{ key: "a", val: 6, mapped: [] },
];
let pendingReads = input.length;
2019-09-09 15:53:21 +00:00
const sink = new Writable({
objectMode: true,
write(chunk, encoding, cb) {
cb();
pendingReads--;
2019-09-11 18:29:20 +00:00
t.pass();
if (pendingReads === 0) {
2019-09-09 15:53:21 +00:00
resolve();
}
},
});
const construct = (destKey: string) => {
2019-09-11 18:29:20 +00:00
const pipeline = map(
async (chunk: Chunk) => {
await sleep(50);
chunk.mapped.push(2);
return chunk;
},
{ highWaterMark: 1, objectMode: true },
);
2019-09-09 15:53:21 +00:00
2019-09-11 18:29:20 +00:00
pipeline.pipe(sink);
return pipeline;
2019-09-09 15:53:21 +00:00
};
const _demux = demux(
construct,
{ key: "key" },
{
objectMode: true,
2019-09-11 18:29:20 +00:00
highWaterMark: 5,
2019-09-09 15:53:21 +00:00
},
);
2019-09-11 18:29:20 +00:00
2019-09-09 15:53:21 +00:00
_demux.on("error", err => {
reject();
});
for (const item of input) {
const res = _demux.write(item);
expect(_demux._writableState.length).to.be.at.most(highWaterMark);
if (!res) {
2019-09-11 18:29:20 +00:00
await new Promise(_resolve => {
_demux.once("drain", () => {
_resolve();
2019-09-11 18:29:20 +00:00
expect(_demux._writableState.length).to.be.equal(0);
t.pass();
});
});
2019-09-09 15:53:21 +00:00
}
}
});
});
test.cb(
"demux() should emit drain event immediately when second stream is bottleneck",
t => {
t.plan(6);
2019-09-11 18:29:20 +00:00
const slowProcessorSpeed = 100;
const highWaterMark = 3;
2019-09-09 15:53:21 +00:00
interface Chunk {
key: string;
mapped: number[];
}
const sink = new Writable({
objectMode: true,
write(chunk, encoding, cb) {
2019-09-11 18:29:20 +00:00
expect(chunk.mapped).to.deep.equal([1, 2]);
2019-09-09 15:53:21 +00:00
t.pass();
2019-09-11 18:29:20 +00:00
pendingReads--;
2019-09-09 15:53:21 +00:00
if (pendingReads === 0) {
t.end();
}
2019-09-11 18:29:20 +00:00
cb();
2019-09-09 15:53:21 +00:00
},
});
const construct = (destKey: string) => {
const first = map(
(chunk: Chunk) => {
chunk.mapped.push(1);
return chunk;
},
2019-09-11 18:29:20 +00:00
{ objectMode: true, highWaterMark: 1 },
2019-09-09 15:53:21 +00:00
);
const second = map(
async (chunk: Chunk) => {
await sleep(slowProcessorSpeed);
2019-09-09 15:53:21 +00:00
chunk.mapped.push(2);
return chunk;
},
{ objectMode: true, highWaterMark: 1 },
);
first.pipe(second).pipe(sink);
return first;
};
const _demux = demux(
construct,
{ key: "key" },
{
objectMode: true,
highWaterMark,
},
);
_demux.on("error", err => {
t.end(err);
});
_demux.on("drain", () => {
expect(_demux._writableState.length).to.be.equal(0);
2019-09-11 18:29:20 +00:00
// Should take longer than the amount of items needed to be processed until we are under highWaterMark
expect(performance.now() - start).to.be.greaterThan(
slowProcessorSpeed * (input.length - highWaterMark - 1),
);
2019-09-09 15:53:21 +00:00
t.pass();
});
const input = [
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
];
let pendingReads = input.length;
2019-09-11 18:29:20 +00:00
const start = performance.now();
2019-09-09 15:53:21 +00:00
input.forEach(item => {
_demux.write(item);
});
},
);
2019-09-11 18:29:20 +00:00
test("demux() should be blocked by slowest pipeline", t => {
t.plan(1);
2019-09-11 18:29:20 +00:00
const slowProcessorSpeed = 100;
interface Chunk {
key: string;
mapped: number[];
}
return new Promise(async (resolve, reject) => {
const sink = new Writable({
objectMode: true,
write(chunk, encoding, cb) {
cb();
pendingReads--;
if (chunk.key === "b") {
2019-09-11 18:29:20 +00:00
expect(performance.now() - start).to.be.greaterThan(
slowProcessorSpeed * totalItems,
);
t.pass();
2019-09-11 18:29:20 +00:00
expect(pendingReads).to.equal(0);
resolve();
}
},
});
const construct = (destKey: string) => {
const first = map(
async (chunk: Chunk) => {
2019-09-11 18:29:20 +00:00
await sleep(slowProcessorSpeed);
chunk.mapped.push(1);
return chunk;
},
{ objectMode: true, highWaterMark: 1 },
);
2019-09-11 18:29:20 +00:00
first.pipe(sink);
return first;
};
const _demux = demux(
construct,
{ key: "key" },
{
objectMode: true,
2019-09-11 18:29:20 +00:00
highWaterMark: 1,
},
);
_demux.on("error", err => {
reject(err);
});
const input = [
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "c", mapped: [] },
{ key: "c", mapped: [] },
2019-09-11 18:29:20 +00:00
{ key: "c", mapped: [] },
{ key: "b", mapped: [] },
];
let pendingReads = input.length;
2019-09-11 18:29:20 +00:00
const totalItems = input.length;
2019-09-09 18:43:18 +00:00
const start = performance.now();
for (const item of input) {
2019-09-11 18:29:20 +00:00
if (!_demux.write(item)) {
await new Promise(_resolve => {
_demux.once("drain", () => {
_resolve();
});
});
}
}
});
});
2019-09-09 18:43:18 +00:00
2019-09-09 15:53:21 +00:00
test("demux() should emit drain event and first should contain up to highWaterMark items in readable state when second is bottleneck", t => {
t.plan(6);
const highWaterMark = 5;
return new Promise(async (resolve, reject) => {
interface Chunk {
key: string;
mapped: number[];
}
const sink = new Writable({
objectMode: true,
write(chunk, encoding, cb) {
2019-09-11 18:29:20 +00:00
expect(chunk.mapped).to.deep.equal([1, 2]);
2019-09-09 15:53:21 +00:00
t.pass();
cb();
if (pendingReads === 0) {
resolve();
}
},
});
const construct = (destKey: string) => {
const first = map(
(chunk: Chunk) => {
expect(first._readableState.length).to.be.at.most(2);
chunk.mapped.push(1);
return chunk;
},
2019-09-11 18:29:20 +00:00
{ objectMode: true, highWaterMark: 1 },
2019-09-09 15:53:21 +00:00
);
const second = map(
async (chunk: Chunk) => {
chunk.mapped.push(2);
expect(second._writableState.length).to.be.equal(1);
await sleep(100);
pendingReads--;
return chunk;
},
2019-09-11 18:29:20 +00:00
{ objectMode: true, highWaterMark: 1 },
2019-09-09 15:53:21 +00:00
);
first.pipe(second).pipe(sink);
return first;
};
const _demux = demux(
construct,
{ key: "key" },
{
objectMode: true,
highWaterMark,
},
);
_demux.on("error", err => {
reject();
});
_demux.on("drain", () => {
expect(_demux._writableState.length).to.be.equal(0);
t.pass();
});
const input = [
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
{ key: "a", mapped: [] },
];
let pendingReads = input.length;
input.forEach(item => {
_demux.write(item);
});
});
});