Split rate tests
This commit is contained in:
parent
f6e3a03eb7
commit
b8bd69eb01
@ -4,64 +4,86 @@ import test from "ava";
|
|||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import { rate } from "../src";
|
import { rate } from "../src";
|
||||||
|
|
||||||
test.cb("rate() sends data at desired rate", t => {
|
test.cb("rate() sends data at a rate of 150", t => {
|
||||||
t.plan(9);
|
t.plan(5);
|
||||||
const fastRate = 150;
|
const targetRate = 150;
|
||||||
const medRate = 50;
|
const source = new Readable({ objectMode: true });
|
||||||
const slowRate = 1;
|
const expectedElements = ["a", "b", "c", "d", "e"];
|
||||||
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();
|
const start = performance.now();
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let j = 0;
|
|
||||||
let k = 0;
|
|
||||||
|
|
||||||
sourceFast
|
source
|
||||||
.pipe(rate(fastRate))
|
.pipe(rate(targetRate))
|
||||||
.on("data", (element: string[]) => {
|
.on("data", (element: string[]) => {
|
||||||
const currentRate = (i / (performance.now() - start)) * 1000;
|
const currentRate = (i / (performance.now() - start)) * 1000;
|
||||||
expect(element).to.deep.equal(expectedElements[i]);
|
expect(element).to.deep.equal(expectedElements[i]);
|
||||||
expect(currentRate).lessThan(fastRate);
|
expect(currentRate).lessThan(targetRate);
|
||||||
t.pass();
|
t.pass();
|
||||||
i++;
|
i++;
|
||||||
})
|
})
|
||||||
.on("error", t.end);
|
|
||||||
|
|
||||||
sourceMed
|
|
||||||
.pipe(rate(medRate))
|
|
||||||
.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
|
|
||||||
.pipe(rate(slowRate))
|
|
||||||
.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("error", t.end)
|
||||||
.on("end", t.end);
|
.on("end", t.end);
|
||||||
|
|
||||||
sourceFast.push("a");
|
source.push("a");
|
||||||
sourceFast.push("b");
|
source.push("b");
|
||||||
sourceFast.push("c");
|
source.push("c");
|
||||||
sourceFast.push(null);
|
source.push("d");
|
||||||
sourceMed.push("a");
|
source.push("e");
|
||||||
sourceMed.push("b");
|
source.push(null);
|
||||||
sourceMed.push("c");
|
});
|
||||||
sourceMed.push(null);
|
|
||||||
sourceSlow.push("a");
|
test.cb("rate() sends data at a rate of 50", t => {
|
||||||
sourceSlow.push("b");
|
t.plan(5);
|
||||||
sourceSlow.push("c");
|
const targetRate = 50;
|
||||||
sourceSlow.push(null);
|
const source = new Readable({ objectMode: true });
|
||||||
|
const expectedElements = ["a", "b", "c", "d", "e"];
|
||||||
|
const start = performance.now();
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
source
|
||||||
|
.pipe(rate(targetRate))
|
||||||
|
.on("data", (element: string[]) => {
|
||||||
|
const currentRate = (i / (performance.now() - start)) * 1000;
|
||||||
|
expect(element).to.deep.equal(expectedElements[i]);
|
||||||
|
expect(currentRate).lessThan(targetRate);
|
||||||
|
t.pass();
|
||||||
|
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(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.cb("rate() sends data at a rate of 1", t => {
|
||||||
|
t.plan(5);
|
||||||
|
const targetRate = 1;
|
||||||
|
const source = new Readable({ objectMode: true });
|
||||||
|
const expectedElements = ["a", "b", "c", "d", "e"];
|
||||||
|
const start = performance.now();
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
source
|
||||||
|
.pipe(rate(targetRate))
|
||||||
|
.on("data", (element: string[]) => {
|
||||||
|
const currentRate = (i / (performance.now() - start)) * 1000;
|
||||||
|
expect(element).to.deep.equal(expectedElements[i]);
|
||||||
|
expect(currentRate).lessThan(targetRate);
|
||||||
|
t.pass();
|
||||||
|
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(null);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user