Skip to content

Commit

Permalink
test(DirectStream): add new 4kb and 4mb encode tests
Browse files Browse the repository at this point in the history
After other implementations of this, it was determined that expect(4mb).toEqual(4mb) took around 30 seconds
to run, so we only really check (and care about) the first three bytes.
  • Loading branch information
thetayloredman committed Feb 23, 2023
1 parent 0e8d349 commit 8e1e99f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["TCPIO"]
"cSpell.words": ["bufncmp", "TCPIO"]
}
27 changes: 26 additions & 1 deletion src/tests/stream/DirectStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ import { kb, mb } from "../../lib/sizeHelpers";
import { ControlCharacters } from "../../stream/controlCharacters";
import DirectStream, { DirectStreamEvents } from "../../stream/DirectStream";

/** Like C's memncmp() for buffers. Used in the large-buffer tests to avoid long test times */
function bufncmp(n: number, left: Buffer, right: Buffer): number {
const len = Math.min(left.length, right.length, n);
for (let i = 0; i < len; i++) {
if (left[i] < right[i]) return -1;
if (left[i] > right[i]) return 1;
}
return 0;
}

/** Makes a new {@link DirectStream} and attaches listeners. */
function makeNewStream(): { stream: DirectStream; onRead: jest.Mock; onPacket: jest.Mock; onReset: jest.Mock } {
const stream = new DirectStream();
Expand Down Expand Up @@ -317,7 +327,22 @@ describe("Stream", () => {
0xff
])
);
// TODO: more?
it("4kb", () => {
const stream = new DirectStream();

const expected = Buffer.concat([Buffer.from([ControlCharacters.ReadKB, 0x00]), Buffer.alloc(kb(4))]);

// Running .toEqual is extremely slow on huge buffers, so we use a custom bufncmp function
expect(bufncmp(3, stream.encode(expected.subarray(2)), expected)).toBe(0);
});
it("4mb", () => {
const stream = new DirectStream();

const expected = Buffer.concat([Buffer.from([ControlCharacters.ReadMB, 0x00]), Buffer.alloc(mb(4))]);

// See above
expect(bufncmp(3, stream.encode(expected.subarray(2)), expected)).toBe(0);
});
});

it("fails for unsupported read sizes", () => {
Expand Down

0 comments on commit 8e1e99f

Please sign in to comment.