Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Commit

Permalink
all: convert tests for flowcontrol
Browse files Browse the repository at this point in the history
  • Loading branch information
bmancini55 committed May 25, 2021
1 parent e8a533a commit b1080ff
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 469 deletions.
108 changes: 108 additions & 0 deletions __tests__/flowcontrol/Batch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { expect } from "chai";
import sinon from "sinon";
import { batch } from "../../src/flowcontrol/Batch";

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

describe("batch", () => {
describe("small batch size", () => {
let fn;
let sut;

beforeEach(() => {
const batchSize = 2;
const delayMs = 50;
fn = sinon.stub();
sut = batch(fn, batchSize, delayMs);
});

it("groups calls within timeout period", async () => {
sut(1);
await wait(10);

sut(2);
await wait(10);

sut(3);
await wait(100);

expect(fn.callCount).to.equal(2);
expect(fn.args[0][0]).to.deep.equal([[1], [2]]);
expect(fn.args[1][0]).to.deep.equal([[3]]);
});

it("groups calls within debounce periods", async () => {
sut(1);
await wait(100);

sut(2);
await wait(100);

sut(3);
await wait(100);

expect(fn.callCount).to.equal(3);
expect(fn.args[0][0]).to.deep.equal([[1]]);
expect(fn.args[1][0]).to.deep.equal([[2]]);
expect(fn.args[2][0]).to.deep.equal([[3]]);
});

it("can reset pending executions", async () => {
sut(1);
sut.cancel();

await wait(100);
expect(fn.callCount).to.equal(0);
});
});

describe("large batch size", () => {
let fn;
let sut;

beforeEach(() => {
const batchSize = 100;
const delayMs = 50;
fn = sinon.stub();
sut = batch(fn, batchSize, delayMs);
});

it("groups calls within timeout period", async () => {
sut(1);
await wait(10);

sut(2);
await wait(10);

sut(3);
await wait(100);

expect(fn.callCount).to.equal(1);
expect(fn.args[0][0]).to.deep.equal([[1], [2], [3]]);
});

it("groups calls within debounce periods", async () => {
sut(1);
await wait(100);

sut(2);
await wait(100);

sut(3);
await wait(100);

expect(fn.callCount).to.equal(3);
expect(fn.args[0][0]).to.deep.equal([[1]]);
expect(fn.args[1][0]).to.deep.equal([[2]]);
expect(fn.args[2][0]).to.deep.equal([[3]]);
});

it("can reset pending executions", async () => {
sut(1);
sut.cancel();

await wait(100);
expect(fn.callCount).to.equal(0);
});
});
});
196 changes: 196 additions & 0 deletions __tests__/flowcontrol/CircularBuffer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/* eslint-disable no-sparse-arrays */
import { CircularBuffer } from "../../src/flowcontrol/CircularBuffer";
import { expect } from "chai";

describe("CircularBuffer", () => {
it("empty read returns undefined", () => {
const sut = new CircularBuffer(4);
expect(sut.read()).to.be.undefined;
});

it("empty read after values", () => {
const sut = new CircularBuffer(4);
sut.write(1);
expect(sut.read()).to.equal(1);
expect(sut.read()).to.be.undefined;
});

it("multi enq/deq", () => {
const sut = new CircularBuffer(4);
sut.write(0);
sut.write(1);
expect(sut.read()).to.equal(0);
expect(sut.read()).to.equal(1);
});

it("multi enq/deq max", () => {
const sut = new CircularBuffer(4);
sut.write(0);
sut.write(1);
sut.write(2);
expect(sut.read()).to.equal(0);
expect(sut.read()).to.equal(1);
expect(sut.read()).to.equal(2);
});

it("multi enq/deq repeatedly", () => {
const sut = new CircularBuffer(4);
for (let i = 0; i < 1024; i++) {
sut.write(0);
sut.write(1);
expect(sut.read()).to.equal(0);
expect(sut.read()).to.equal(1);
}
});

it("cycle 1", () => {
const sut = new CircularBuffer(4);
expect(sut.write(0)).to.be.true;
expect(sut.write(1)).to.be.true;
expect(sut.write(2)).to.be.true;
expect(sut.write(3)).to.be.false;
expect(sut.buffer).to.deep.equal([undefined, 0, 1, 2]);
});

it("cycle 2", () => {
const sut = new CircularBuffer(4);
expect(sut.write(0)).to.be.true;
expect(sut.write(1)).to.be.true;
expect(sut.write(2)).to.be.true;

sut.read();
expect(sut.write(3)).to.be.true;
expect(sut.buffer).to.deep.equal([3, undefined, 1, 2]);

expect(sut.write(4)).to.be.false;
});

it("cycle 3", () => {
const sut = new CircularBuffer(4);
expect(sut.write(0)).to.be.true;
expect(sut.write(1)).to.be.true;
expect(sut.write(2)).to.be.true;

sut.read();
expect(sut.write(3)).to.be.true;
expect(sut.buffer).to.deep.equal([3, undefined, 1, 2]);

sut.read();
expect(sut.write(4)).to.be.true;
expect(sut.buffer).to.deep.equal([3, 4, undefined, 2]);

expect(sut.write(5)).to.be.false;
});

it("cycle 4", () => {
const sut = new CircularBuffer(4);
expect(sut.write(0)).to.be.true;
expect(sut.write(1)).to.be.true;
expect(sut.write(2)).to.be.true;

sut.read();
expect(sut.write(3)).to.be.true;
expect(sut.buffer).to.deep.equal([3, undefined, 1, 2]);

sut.read();
expect(sut.write(4)).to.be.true;
expect(sut.buffer).to.deep.equal([3, 4, undefined, 2]);

sut.read();
expect(sut.write(5)).to.be.true;
expect(sut.buffer).to.deep.equal([3, 4, 5, undefined]);

expect(sut.write(6)).to.be.false;
});

it("cycle 5", () => {
const sut = new CircularBuffer(4);
expect(sut.write(0)).to.be.true;
expect(sut.write(1)).to.be.true;
expect(sut.write(2)).to.be.true;

sut.read();
expect(sut.write(3)).to.be.true;
expect(sut.buffer).to.deep.equal([3, undefined, 1, 2]);

sut.read();
expect(sut.write(4)).to.be.true;
expect(sut.buffer).to.deep.equal([3, 4, undefined, 2]);

sut.read();
expect(sut.write(5)).to.be.true;
expect(sut.buffer).to.deep.equal([3, 4, 5, undefined]);

sut.read();
expect(sut.write(6)).to.be.true;
expect(sut.buffer).to.deep.equal([undefined, 4, 5, 6]);

expect(sut.write(7)).to.be.false;
});

it("fills and empties", () => {
const sut = new CircularBuffer(4);
expect(sut.write(0)).to.be.true;
expect(sut.write(1)).to.be.true;
expect(sut.write(2)).to.be.true;
expect(sut.read()).to.equal(0);
expect(sut.read()).to.equal(1);
expect(sut.read()).to.equal(2);

expect(sut.write(3)).to.be.true;
expect(sut.write(4)).to.be.true;
expect(sut.read()).to.equal(3);
expect(sut.read()).to.equal(4);

expect(sut.write(5)).to.be.true;
expect(sut.read()).to.equal(5);

expect(sut.write(6)).to.be.true;
expect(sut.read()).to.equal(6);

expect(sut.write(7)).to.be.true;
expect(sut.read()).to.equal(7);

expect(sut.write(8)).to.be.true;
expect(sut.read()).to.equal(8);
});

it("full cycles", () => {
const sut = new CircularBuffer(4);
sut.write("a");
sut.write("b");
sut.write("c");

for (let i = 0; i < 1000; i++) {
const a = sut.read();
const b = sut.read();
const c = sut.read();
expect(a).to.equal("a");
expect(b).to.equal("b");
expect(c).to.equal("c");
sut.write(a);
sut.write(b);
sut.write(c);
expect(sut.write("nope")).to.be.false;
}
});

it("partial cycles", () => {
const sut = new CircularBuffer(8);
sut.write("a");
sut.write("b");
sut.write("c");

for (let i = 0; i < 10000; i++) {
const a = sut.read();
const b = sut.read();
const c = sut.read();
expect(a).to.equal("a");
expect(b).to.equal("b");
expect(c).to.equal("c");
sut.write(a);
sut.write(b);
sut.write(c);
}
});
});
54 changes: 54 additions & 0 deletions __tests__/flowcontrol/Debounce.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from "chai";
import sinon from "sinon";
import { debounce } from "../../src/flowcontrol/Debounce";

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

describe("debounce", () => {
let fn;
let sut;

beforeEach(() => {
const debounceMs = 50;
fn = sinon.stub();
sut = debounce(fn, debounceMs);
});

it("groups calls within timeout period", async () => {
sut(1);
await wait(10);

sut(2);
await wait(10);

sut(3);
await wait(100);

expect(fn.callCount).to.equal(1);
expect(fn.args[0][0]).to.deep.equal(3);
});

it("groups calls within debounce periods", async () => {
sut(1);
await wait(100);

sut(2);
await wait(100);

sut(3);
await wait(100);

expect(fn.callCount).to.equal(3);
expect(fn.args[0][0]).to.deep.equal(1);
expect(fn.args[1][0]).to.deep.equal(2);
expect(fn.args[2][0]).to.deep.equal(3);
});

it("can cancel pending executions", async () => {
sut(1);
sut.cancel();

await wait(100);
expect(fn.callCount).to.equal(0);
});
});
Loading

0 comments on commit b1080ff

Please sign in to comment.