Skip to content

Commit

Permalink
refactor: 테스트 공통 함수 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
daengdaengLee committed Aug 16, 2023
1 parent f3c6710 commit db05803
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 94 deletions.
57 changes: 12 additions & 45 deletions src/take-stream-factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Readable, PassThrough } from "node:stream";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { takeStreamFactory } from "./take-stream-factory.js";
import { asyncInputs } from "./test-util/async-inputs.js";
import { sourceStreamCloseTestTemplate } from "./test-util/source-stream-close-test-template.js";
import { syncInputs } from "./test-util/sync-inputs.js";
import { ToArrayStream } from "./to-array-stream.js";

Expand Down Expand Up @@ -39,55 +40,21 @@ describe(`takeStreamFactory Test`, () => {

describe(`close source stream when close wrapped stream.`, () => {
for (const { name, inputs } of testCases) {
const template = async (
wrappedStreamFactory: (sourceStream: Readable) => Readable,
): Promise<void> => {
let isSourceStreamDone = false;
let isWrappedStreamDone = false;
const sourceStreamOutputsAfterWrappedStreamDone: Array<number> = [];

const sourceStream = Readable.from(inputs());
sourceStream.on(`data`, (value) => {
if (isWrappedStreamDone) {
sourceStreamOutputsAfterWrappedStreamDone.push(value);
}
});
const sourceStreamDone = new Promise<void>((resolve) => {
sourceStream.on(`close`, () => {
isSourceStreamDone = true;
resolve();
});
});

const wrappedStream = wrappedStreamFactory(sourceStream);
const wrappedStreamDone = new Promise<void>((resolve) => {
wrappedStream.on(`close`, () => {
isWrappedStreamDone = true;
resolve();
});
});

await pipeline(wrappedStream, new PassThrough({ objectMode: true }));
await Promise.all([sourceStreamDone, wrappedStreamDone]);

expect(isSourceStreamDone).toBeTruthy();
expect(isWrappedStreamDone).toBeTruthy();
expect(sourceStreamOutputsAfterWrappedStreamDone.length).toEqual(0);
};

describe(name, () => {
it(`no curry version`, async () => {
const wrappedStreamFactory = (sourceStream: Readable): Readable => {
return Readable.from(takeStreamFactory({ n: n }, sourceStream));
};
await template(wrappedStreamFactory);
const sourceStream = Readable.from(inputs());
const wrappedStream = Readable.from(
takeStreamFactory({ n: n }, sourceStream),
);
await sourceStreamCloseTestTemplate(sourceStream, wrappedStream);
});

it(`curry version`, async () => {
const wrappedStreamFactory = (sourceStream: Readable): Readable => {
return Readable.from(takeStreamFactory({ n: n })(sourceStream));
};
await template(wrappedStreamFactory);
const sourceStream = Readable.from(inputs());
const wrappedStream = Readable.from(
takeStreamFactory({ n: n })(sourceStream),
);
await sourceStreamCloseTestTemplate(sourceStream, wrappedStream);
});
});
}
Expand Down
61 changes: 12 additions & 49 deletions src/take-while-stream-factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PassThrough, Readable } from "node:stream";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { takeWhileStreamFactory } from "./take-while-stream-factory.js";
import { asyncInputs } from "./test-util/async-inputs.js";
import { sourceStreamCloseTestTemplate } from "./test-util/source-stream-close-test-template.js";
import { syncInputs } from "./test-util/sync-inputs.js";
import { ToArrayStream } from "./to-array-stream.js";
import { delay } from "./util.js";
Expand Down Expand Up @@ -48,59 +49,21 @@ describe(`takeWhileStreamFactory Test`, () => {

describe(`close source stream when close wrapped stream.`, () => {
for (const { name, f, inputs } of testCases) {
const template = async (
wrappedStreamFactory: (sourceStream: Readable) => Readable,
): Promise<void> => {
let isSourceStreamDone = false;
let isWrappedStreamDone = false;
const sourceStreamOutputsAfterWrappedStreamDone: Array<number> = [];

const sourceStream = Readable.from(inputs());
sourceStream.on(`data`, (value) => {
if (isWrappedStreamDone) {
sourceStreamOutputsAfterWrappedStreamDone.push(value);
}
});
const sourceStreamDone = new Promise<void>((resolve) => {
sourceStream.on(`close`, () => {
isSourceStreamDone = true;
resolve();
});
});

const wrappedStream = wrappedStreamFactory(sourceStream);
const wrappedStreamDone = new Promise<void>((resolve) => {
wrappedStream.on(`close`, () => {
isWrappedStreamDone = true;
resolve();
});
});

await pipeline(wrappedStream, new PassThrough({ objectMode: true }));
await Promise.all([sourceStreamDone, wrappedStreamDone]);

expect(isSourceStreamDone).toBeTruthy();
expect(isWrappedStreamDone).toBeTruthy();
expect(sourceStreamOutputsAfterWrappedStreamDone.length).toEqual(0);
};

describe(name, () => {
it(`no curry version`, async () => {
const wrappedStreamFactory = (sourceStream: Readable): Readable => {
return Readable.from(
takeWhileStreamFactory({ f: f }, sourceStream),
);
};
await template(wrappedStreamFactory);
const sourceStream = Readable.from(inputs());
const wrappedStream = Readable.from(
takeWhileStreamFactory({ f: f }, sourceStream),
);
await sourceStreamCloseTestTemplate(sourceStream, wrappedStream);
});

it(`curry version`, async () => {
const wrappedStreamFactory = (sourceStream: Readable): Readable => {
return Readable.from(
takeWhileStreamFactory({ f: f })(sourceStream),
);
};
await template(wrappedStreamFactory);
const sourceStream = Readable.from(inputs());
const wrappedStream = Readable.from(
takeWhileStreamFactory({ f: f })(sourceStream),
);
await sourceStreamCloseTestTemplate(sourceStream, wrappedStream);
});
});
}
Expand Down
37 changes: 37 additions & 0 deletions src/test-util/source-stream-close-test-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PassThrough, Readable } from "node:stream";
import { pipeline } from "node:stream/promises";

export const sourceStreamCloseTestTemplate = async (
sourceStream: Readable,
wrappedStream: Readable,
): Promise<void> => {
let isSourceStreamDone = false;
let isWrappedStreamDone = false;
const sourceStreamOutputsAfterWrappedStreamDone: Array<number> = [];

sourceStream.on(`data`, (value) => {
if (isWrappedStreamDone) {
sourceStreamOutputsAfterWrappedStreamDone.push(value);
}
});
const sourceStreamDone = new Promise<void>((resolve) => {
sourceStream.on(`close`, () => {
isSourceStreamDone = true;
resolve();
});
});

const wrappedStreamDone = new Promise<void>((resolve) => {
wrappedStream.on(`close`, () => {
isWrappedStreamDone = true;
resolve();
});
});

await pipeline(wrappedStream, new PassThrough({ objectMode: true }));
await Promise.all([sourceStreamDone, wrappedStreamDone]);

expect(isSourceStreamDone).toBeTruthy();
expect(isWrappedStreamDone).toBeTruthy();
expect(sourceStreamOutputsAfterWrappedStreamDone.length).toEqual(0);
};

0 comments on commit db05803

Please sign in to comment.