Test readable length in first

This commit is contained in:
Jerry Kurian 2019-09-07 17:14:08 -04:00
parent ae7c9d6b09
commit 2cbeae38e7

View File

@ -73,6 +73,7 @@ test.cb(
"compose() should emit drain event after 1 second when first stream is bottleneck", "compose() should emit drain event after 1 second when first stream is bottleneck",
t => { t => {
t.plan(6); t.plan(6);
let passedBottleneck = 0;
interface Chunk { interface Chunk {
index: number; index: number;
mapped: string[]; mapped: string[];
@ -80,6 +81,7 @@ test.cb(
const first = map( const first = map(
async (chunk: Chunk) => { async (chunk: Chunk) => {
await sleep(200); await sleep(200);
passedBottleneck++;
chunk.mapped.push("first"); chunk.mapped.push("first");
return chunk; return chunk;
}, },
@ -98,18 +100,23 @@ test.cb(
const composed = compose( const composed = compose(
[first, second], [first, second],
{ objectMode: true, highWaterMark: 2 }, { objectMode: true, highWaterMark: 5 },
); );
composed.on("error", err => { composed.on("error", err => {
t.end(err); t.end(err);
}); });
composed.on("drain", err => { composed.on("drain", err => {
expect(composed._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.greaterThan(1000); expect(performance.now() - start).to.be.greaterThan(1000);
t.pass(); t.pass();
}); });
composed.on("data", (chunk: Chunk) => { composed.on("data", (chunk: Chunk) => {
// Since first is bottleneck, composed accumulates until cb is executed in first. Therefore buffer should contain 4, 3, 2, 1 then 0 elements
expect(composed._writableState.length).to.be.equal(
input.length - passedBottleneck,
);
expect(chunk.mapped.length).to.equal(2); expect(chunk.mapped.length).to.equal(2);
expect(chunk.mapped).to.deep.equal(["first", "second"]); expect(chunk.mapped).to.deep.equal(["first", "second"]);
t.pass(); t.pass();
@ -119,11 +126,11 @@ test.cb(
}); });
const input = [ const input = [
{ data: 1 }, { index: 1, mapped: [] },
{ data: 2 }, { index: 2, mapped: [] },
{ data: 3 }, { index: 3, mapped: [] },
{ data: 4 }, { index: 4, mapped: [] },
{ data: 5 }, { index: 5, mapped: [] },
]; ];
input.forEach(item => { input.forEach(item => {
@ -153,27 +160,32 @@ test.cb(
const second = map( const second = map(
async (chunk: Chunk) => { async (chunk: Chunk) => {
pendingReads--;
await sleep(500); await sleep(500);
expect(first._readableState.length).to.equal(pendingReads);
chunk.mapped.push("second"); chunk.mapped.push("second");
return chunk; return chunk;
}, },
{ objectMode: true }, { objectMode: true, highWaterMark: 1 },
); );
const composed = compose( const composed = compose(
[first, second], [first, second],
{ objectMode: true, highWaterMark: 2 }, { objectMode: true, highWaterMark: 5 },
); );
composed.on("error", err => { composed.on("error", err => {
t.end(err); t.end(err);
}); });
composed.on("drain", err => { composed.on("drain", err => {
expect(composed._writableState.length).to.be.equal(0);
expect(performance.now() - start).to.be.lessThan(100); expect(performance.now() - start).to.be.lessThan(100);
t.pass(); t.pass();
}); });
composed.on("data", (chunk: Chunk) => { composed.on("data", (chunk: Chunk) => {
// Since second is bottleneck, composed will write into first immediately. Buffer should be empty.
expect(composed._writableState.length).to.be.equal(0);
expect(chunk.mapped.length).to.equal(2); expect(chunk.mapped.length).to.equal(2);
expect(chunk.mapped).to.deep.equal(["first", "second"]); expect(chunk.mapped).to.deep.equal(["first", "second"]);
t.pass(); t.pass();
@ -189,6 +201,7 @@ test.cb(
{ index: 4, mapped: [] }, { index: 4, mapped: [] },
{ index: 5, mapped: [] }, { index: 5, mapped: [] },
]; ];
let pendingReads = input.length;
input.forEach(item => { input.forEach(item => {
composed.write(item); composed.write(item);
@ -229,7 +242,7 @@ test.cb(
const composed = compose( const composed = compose(
[first, second], [first, second],
{ objectMode: true, highWaterMark: 3 }, { objectMode: true, highWaterMark: 5 },
); );
composed.on("error", err => { composed.on("error", err => {
t.end(err); t.end(err);
@ -245,6 +258,7 @@ test.cb(
}); });
composed.on("drain", () => { composed.on("drain", () => {
expect(composed._writableState.length).to.be.equal(0);
t.pass(); t.pass();
}); });