summaryrefslogtreecommitdiff
path: root/test/parallel/test-stream-transform-destroy.js
blob: 47cce87264b5c1d204972fffb855d250f384575e (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';

const common = require('../common');
const { Transform } = require('stream');
const assert = require('assert');

{
  const transform = new Transform({
    transform(chunk, enc, cb) {}
  });

  transform.resume();

  transform.on('end', common.mustNotCall());
  transform.on('close', common.mustCall());
  transform.on('finish', common.mustNotCall());

  transform.destroy();
}

{
  const transform = new Transform({
    transform(chunk, enc, cb) {}
  });
  transform.resume();

  const expected = new Error('kaboom');

  transform.on('end', common.mustNotCall());
  transform.on('finish', common.mustNotCall());
  transform.on('close', common.mustCall());
  transform.on('error', common.mustCall((err) => {
    assert.strictEqual(err, expected);
  }));

  transform.destroy(expected);
}

{
  const transform = new Transform({
    transform(chunk, enc, cb) {}
  });

  transform._destroy = common.mustCall(function(err, cb) {
    assert.strictEqual(err, expected);
    cb(err);
  }, 1);

  const expected = new Error('kaboom');

  transform.on('finish', common.mustNotCall('no finish event'));
  transform.on('close', common.mustCall());
  transform.on('error', common.mustCall((err) => {
    assert.strictEqual(err, expected);
  }));

  transform.destroy(expected);
}

{
  const expected = new Error('kaboom');
  const transform = new Transform({
    transform(chunk, enc, cb) {},
    destroy: common.mustCall(function(err, cb) {
      assert.strictEqual(err, expected);
      cb();
    }, 1)
  });
  transform.resume();

  transform.on('end', common.mustNotCall('no end event'));
  transform.on('close', common.mustCall());
  transform.on('finish', common.mustNotCall('no finish event'));

  // error is swallowed by the custom _destroy
  transform.on('error', common.mustNotCall('no error event'));

  transform.destroy(expected);
}

{
  const transform = new Transform({
    transform(chunk, enc, cb) {}
  });

  transform._destroy = common.mustCall(function(err, cb) {
    assert.strictEqual(err, null);
    cb();
  }, 1);

  transform.destroy();
}

{
  const transform = new Transform({
    transform(chunk, enc, cb) {}
  });
  transform.resume();

  transform._destroy = common.mustCall(function(err, cb) {
    assert.strictEqual(err, null);
    process.nextTick(() => {
      this.push(null);
      this.end();
      cb();
    });
  }, 1);

  const fail = common.mustNotCall('no event');

  transform.on('finish', fail);
  transform.on('end', fail);
  transform.on('close', common.mustCall());

  transform.destroy();

  transform.removeListener('end', fail);
  transform.removeListener('finish', fail);
  transform.on('end', common.mustCall());
  transform.on('finish', common.mustCall());
}

{
  const transform = new Transform({
    transform(chunk, enc, cb) {}
  });

  const expected = new Error('kaboom');

  transform._destroy = common.mustCall(function(err, cb) {
    assert.strictEqual(err, null);
    cb(expected);
  }, 1);

  transform.on('close', common.mustCall());
  transform.on('finish', common.mustNotCall('no finish event'));
  transform.on('end', common.mustNotCall('no end event'));
  transform.on('error', common.mustCall((err) => {
    assert.strictEqual(err, expected);
  }));

  transform.destroy();
}