summaryrefslogtreecommitdiff
path: root/std/node/_stream/end_of_stream_test.ts
blob: 571e75b99af75f2fd46b8b362fef36d33f131155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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"));
});