summaryrefslogtreecommitdiff
path: root/test/parallel/test-webstreams-abort-controller.js
blob: 1468e418c6cb4ad3f862a8e6fe359e013d364e9b (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';

const common = require('../common');
const { finished, addAbortSignal } = require('stream');
const { ReadableStream, WritableStream } = require('stream/web');
const assert = require('assert');

function createTestReadableStream() {
  return new ReadableStream({
    start(controller) {
      controller.enqueue('a');
      controller.enqueue('b');
      controller.enqueue('c');
      controller.close();
    }
  });
}

function createTestWritableStream(values) {
  return new WritableStream({
    write(chunk) {
      values.push(chunk);
    }
  });
}

{
  const rs = createTestReadableStream();

  const reader = rs.getReader();

  const ac = new AbortController();

  addAbortSignal(ac.signal, rs);

  finished(rs, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(reader.read(), /AbortError/).then(common.mustCall());
    assert.rejects(reader.closed, /AbortError/).then(common.mustCall());
  }));

  reader.read().then(common.mustCall((result) => {
    assert.strictEqual(result.value, 'a');
    ac.abort();
  }));
}

{
  const rs = createTestReadableStream();

  const ac = new AbortController();

  addAbortSignal(ac.signal, rs);

  assert.rejects((async () => {
    for await (const chunk of rs) {
      if (chunk === 'b') {
        ac.abort();
      }
    }
  })(), /AbortError/).then(common.mustCall());
}

{
  const rs1 = createTestReadableStream();

  const rs2 = createTestReadableStream();

  const ac = new AbortController();

  addAbortSignal(ac.signal, rs1);
  addAbortSignal(ac.signal, rs2);

  const reader1 = rs1.getReader();
  const reader2 = rs2.getReader();

  finished(rs1, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(reader1.read(), /AbortError/).then(common.mustCall());
    assert.rejects(reader1.closed, /AbortError/).then(common.mustCall());
  }));

  finished(rs2, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(reader2.read(), /AbortError/).then(common.mustCall());
    assert.rejects(reader2.closed, /AbortError/).then(common.mustCall());
  }));

  ac.abort();
}

{
  const rs = createTestReadableStream();

  const { 0: rs1, 1: rs2 } = rs.tee();

  const ac = new AbortController();

  addAbortSignal(ac.signal, rs);

  const reader1 = rs1.getReader();
  const reader2 = rs2.getReader();

  finished(rs1, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(reader1.read(), /AbortError/).then(common.mustCall());
    assert.rejects(reader1.closed, /AbortError/).then(common.mustCall());
  }));

  finished(rs2, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(reader2.read(), /AbortError/).then(common.mustCall());
    assert.rejects(reader2.closed, /AbortError/).then(common.mustCall());
  }));

  ac.abort();
}

{
  const values = [];
  const ws = createTestWritableStream(values);

  const ac = new AbortController();

  addAbortSignal(ac.signal, ws);

  const writer = ws.getWriter();

  finished(ws, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.deepStrictEqual(values, ['a']);
    assert.rejects(writer.write('b'), /AbortError/).then(common.mustCall());
    assert.rejects(writer.closed, /AbortError/).then(common.mustCall());
  }));

  writer.write('a').then(() => {
    ac.abort();
  });
}

{
  const values = [];

  const ws1 = createTestWritableStream(values);
  const ws2 = createTestWritableStream(values);

  const ac = new AbortController();

  addAbortSignal(ac.signal, ws1);
  addAbortSignal(ac.signal, ws2);

  const writer1 = ws1.getWriter();
  const writer2 = ws2.getWriter();

  finished(ws1, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(writer1.write('a'), /AbortError/).then(common.mustCall());
    assert.rejects(writer1.closed, /AbortError/).then(common.mustCall());
  }));

  finished(ws2, common.mustCall((err) => {
    assert.strictEqual(err.name, 'AbortError');
    assert.rejects(writer2.write('a'), /AbortError/).then(common.mustCall());
    assert.rejects(writer2.closed, /AbortError/).then(common.mustCall());
  }));

  ac.abort();
}