From db058034a0e8bf1163da647b478dea2dfa407f20 Mon Sep 17 00:00:00 2001 From: daengdaengLee Date: Wed, 16 Aug 2023 13:25:39 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EA=B3=B5=ED=86=B5=20=ED=95=A8=EC=88=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/take-stream-factory.test.ts | 57 ++++------------- src/take-while-stream-factory.test.ts | 61 ++++--------------- .../source-stream-close-test-template.ts | 37 +++++++++++ 3 files changed, 61 insertions(+), 94 deletions(-) create mode 100644 src/test-util/source-stream-close-test-template.ts diff --git a/src/take-stream-factory.test.ts b/src/take-stream-factory.test.ts index b0ea937..ec8f730 100644 --- a/src/take-stream-factory.test.ts +++ b/src/take-stream-factory.test.ts @@ -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"; @@ -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 => { - let isSourceStreamDone = false; - let isWrappedStreamDone = false; - const sourceStreamOutputsAfterWrappedStreamDone: Array = []; - - const sourceStream = Readable.from(inputs()); - sourceStream.on(`data`, (value) => { - if (isWrappedStreamDone) { - sourceStreamOutputsAfterWrappedStreamDone.push(value); - } - }); - const sourceStreamDone = new Promise((resolve) => { - sourceStream.on(`close`, () => { - isSourceStreamDone = true; - resolve(); - }); - }); - - const wrappedStream = wrappedStreamFactory(sourceStream); - const wrappedStreamDone = new Promise((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); }); }); } diff --git a/src/take-while-stream-factory.test.ts b/src/take-while-stream-factory.test.ts index e11b775..9ef223e 100644 --- a/src/take-while-stream-factory.test.ts +++ b/src/take-while-stream-factory.test.ts @@ -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"; @@ -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 => { - let isSourceStreamDone = false; - let isWrappedStreamDone = false; - const sourceStreamOutputsAfterWrappedStreamDone: Array = []; - - const sourceStream = Readable.from(inputs()); - sourceStream.on(`data`, (value) => { - if (isWrappedStreamDone) { - sourceStreamOutputsAfterWrappedStreamDone.push(value); - } - }); - const sourceStreamDone = new Promise((resolve) => { - sourceStream.on(`close`, () => { - isSourceStreamDone = true; - resolve(); - }); - }); - - const wrappedStream = wrappedStreamFactory(sourceStream); - const wrappedStreamDone = new Promise((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); }); }); } diff --git a/src/test-util/source-stream-close-test-template.ts b/src/test-util/source-stream-close-test-template.ts new file mode 100644 index 0000000..6c9de8d --- /dev/null +++ b/src/test-util/source-stream-close-test-template.ts @@ -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 => { + let isSourceStreamDone = false; + let isWrappedStreamDone = false; + const sourceStreamOutputsAfterWrappedStreamDone: Array = []; + + sourceStream.on(`data`, (value) => { + if (isWrappedStreamDone) { + sourceStreamOutputsAfterWrappedStreamDone.push(value); + } + }); + const sourceStreamDone = new Promise((resolve) => { + sourceStream.on(`close`, () => { + isSourceStreamDone = true; + resolve(); + }); + }); + + const wrappedStreamDone = new Promise((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); +};