summaryrefslogtreecommitdiff
path: root/test/parallel/test-stream-auto-destroy.js
blob: 2a1a5190debb57e26557e418e742114de9ecb956 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const common = require('../common');
const stream = require('stream');
const assert = require('assert');

{
  const r = new stream.Readable({
    autoDestroy: true,
    read() {
      this.push('hello');
      this.push('world');
      this.push(null);
    },
    destroy: common.mustCall((err, cb) => cb())
  });

  let ended = false;

  r.resume();

  r.on('end', common.mustCall(() => {
    ended = true;
  }));

  r.on('close', common.mustCall(() => {
    assert(ended);
  }));
}

{
  const w = new stream.Writable({
    autoDestroy: true,
    write(data, enc, cb) {
      cb(null);
    },
    destroy: common.mustCall((err, cb) => cb())
  });

  let finished = false;

  w.write('hello');
  w.write('world');
  w.end();

  w.on('finish', common.mustCall(() => {
    finished = true;
  }));

  w.on('close', common.mustCall(() => {
    assert(finished);
  }));
}

{
  const t = new stream.Transform({
    autoDestroy: true,
    transform(data, enc, cb) {
      cb(null, data);
    },
    destroy: common.mustCall((err, cb) => cb())
  });

  let ended = false;
  let finished = false;

  t.write('hello');
  t.write('world');
  t.end();

  t.resume();

  t.on('end', common.mustCall(() => {
    ended = true;
  }));

  t.on('finish', common.mustCall(() => {
    finished = true;
  }));

  t.on('close', common.mustCall(() => {
    assert(ended);
    assert(finished);
  }));
}

{
  const r = new stream.Readable({
    read() {
      r2.emit('error', new Error('fail'));
    }
  });
  const r2 = new stream.Readable({
    autoDestroy: true,
    destroy: common.mustCall((err, cb) => cb())
  });

  r.pipe(r2);
}

{
  const r = new stream.Readable({
    read() {
      w.emit('error', new Error('fail'));
    }
  });
  const w = new stream.Writable({
    autoDestroy: true,
    destroy: common.mustCall((err, cb) => cb())
  });

  r.pipe(w);
}