strom/tests/rate.spec.ts

68 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-08-15 21:06:54 +00:00
import { Readable } from "stream";
import { performance } from "perf_hooks";
import test from "ava";
import { expect } from "chai";
2019-08-16 13:02:54 +00:00
import { rate } from "../src";
2019-08-15 21:06:54 +00:00
test.cb("rate() sends data at desired rate", t => {
t.plan(9);
const fastRate = 150;
const medRate = 50;
const slowRate = 1;
const sourceFast = new Readable({ objectMode: true });
const sourceMed = new Readable({ objectMode: true });
const sourceSlow = new Readable({ objectMode: true });
const expectedElements = ["a", "b", "c"];
const start = performance.now();
let i = 0;
let j = 0;
let k = 0;
sourceFast
2019-08-16 14:06:23 +00:00
.pipe(rate(fastRate))
2019-08-15 21:06:54 +00:00
.on("data", (element: string[]) => {
const currentRate = (i / (performance.now() - start)) * 1000;
expect(element).to.deep.equal(expectedElements[i]);
expect(currentRate).lessThan(fastRate);
t.pass();
i++;
})
.on("error", t.end);
sourceMed
2019-08-16 14:06:23 +00:00
.pipe(rate(medRate))
2019-08-15 21:06:54 +00:00
.on("data", (element: string[]) => {
const currentRate = (j / (performance.now() - start)) * 1000;
expect(element).to.deep.equal(expectedElements[j]);
expect(currentRate).lessThan(medRate);
t.pass();
j++;
})
.on("error", t.end);
sourceSlow
2019-08-16 14:06:23 +00:00
.pipe(rate(slowRate))
2019-08-15 21:06:54 +00:00
.on("data", (element: string[]) => {
const currentRate = (k / (performance.now() - start)) * 1000;
expect(element).to.deep.equal(expectedElements[k]);
expect(currentRate).lessThan(slowRate);
t.pass();
k++;
})
.on("error", t.end)
.on("end", t.end);
sourceFast.push("a");
sourceFast.push("b");
sourceFast.push("c");
sourceFast.push(null);
sourceMed.push("a");
sourceMed.push("b");
sourceMed.push("c");
sourceMed.push(null);
sourceSlow.push("a");
sourceSlow.push("b");
sourceSlow.push("c");
sourceSlow.push(null);
});