diff options
author | Steven Guerrero <stephenguerrero43@gmail.com> | 2020-11-26 07:50:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-26 13:50:08 +0100 |
commit | 9042fcc12e7774cdd0ca3a5d08918a07dae8102b (patch) | |
tree | 8b5ff11412aae9bb714e0bb0b9b0358db64a8657 /std/node/_stream/end_of_stream_test.ts | |
parent | 60e980c78180ee3b0a14d692307be275dc181c8d (diff) |
feat(std/node/stream): Add Duplex, Transform, Passthrough, pipeline, finished and promises (#7940)
Diffstat (limited to 'std/node/_stream/end_of_stream_test.ts')
-rw-r--r-- | std/node/_stream/end_of_stream_test.ts | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/std/node/_stream/end_of_stream_test.ts b/std/node/_stream/end_of_stream_test.ts new file mode 100644 index 000000000..571e75b99 --- /dev/null +++ b/std/node/_stream/end_of_stream_test.ts @@ -0,0 +1,97 @@ +// Copyright Node.js contributors. All rights reserved. MIT License. +import finished from "./end_of_stream.ts"; +import Readable from "./readable.ts"; +import Transform from "./transform.ts"; +import Writable from "./writable.ts"; +import { mustCall } from "../_utils.ts"; +import { assert, fail } from "../../testing/asserts.ts"; +import { deferred, delay } from "../../async/mod.ts"; + +Deno.test("Finished appends to Readable correctly", async () => { + const rs = new Readable({ + read() {}, + }); + + const [finishedExecution, finishedCb] = mustCall((err) => { + assert(!err); + }); + + finished(rs, finishedCb); + + rs.push(null); + rs.resume(); + + await finishedExecution; +}); + +Deno.test("Finished appends to Writable correctly", async () => { + const ws = new Writable({ + write(_data, _enc, cb) { + cb(); + }, + }); + + const [finishedExecution, finishedCb] = mustCall((err) => { + assert(!err); + }); + + finished(ws, finishedCb); + + ws.end(); + + await finishedExecution; +}); + +Deno.test("Finished appends to Transform correctly", async () => { + const tr = new Transform({ + transform(_data, _enc, cb) { + cb(); + }, + }); + + let finish = false; + let ended = false; + + tr.on("end", () => { + ended = true; + }); + + tr.on("finish", () => { + finish = true; + }); + + const [finishedExecution, finishedCb] = mustCall((err) => { + assert(!err); + assert(finish); + assert(ended); + }); + + finished(tr, finishedCb); + + tr.end(); + tr.resume(); + + await finishedExecution; +}); + +Deno.test("The function returned by Finished clears the listeners", async () => { + const finishedExecution = deferred(); + + const ws = new Writable({ + write(_data, _env, cb) { + cb(); + }, + }); + + const removeListener = finished(ws, () => { + finishedExecution.reject(); + }); + removeListener(); + ws.end(); + + await Promise.race([ + delay(100), + finishedExecution, + ]) + .catch(() => fail("Finished was executed")); +}); |