summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/piping/close-propagation-backward.any.js
blob: 25bd475ed13e77d9021a5228021ab23e1b945a91 (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
// META: global=window,worker,shadowrealm
// META: script=../resources/recording-streams.js
'use strict';

const error1 = new Error('error1!');
error1.name = 'error1';

promise_test(() => {

  const rs = recordingReadableStream();

  const ws = recordingWritableStream();
  const writer = ws.getWriter();
  writer.close();
  writer.releaseLock();

  return rs.pipeTo(ws).then(
    () => assert_unreached('the promise must not fulfill'),
    err => {
      assert_equals(err.name, 'TypeError', 'the promise must reject with a TypeError');

      assert_array_equals(rs.eventsWithoutPulls, ['cancel', err]);
      assert_array_equals(ws.events, ['close']);

      return Promise.all([
        rs.getReader().closed,
        ws.getWriter().closed
      ]);
    }
  );

}, 'Closing must be propagated backward: starts closed; preventCancel omitted; fulfilled cancel promise');

promise_test(t => {

  // Our recording streams do not deal well with errors generated by the system, so give them some help
  let recordedError;
  const rs = recordingReadableStream({
    cancel(cancelErr) {
      recordedError = cancelErr;
      throw error1;
    }
  });

  const ws = recordingWritableStream();
  const writer = ws.getWriter();
  writer.close();
  writer.releaseLock();

  return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the same error').then(() => {
    assert_equals(recordedError.name, 'TypeError', 'the cancel reason must be a TypeError');

    assert_array_equals(rs.eventsWithoutPulls, ['cancel', recordedError]);
    assert_array_equals(ws.events, ['close']);

    return Promise.all([
      rs.getReader().closed,
      ws.getWriter().closed
    ]);
  });

}, 'Closing must be propagated backward: starts closed; preventCancel omitted; rejected cancel promise');

for (const falsy of [undefined, null, false, +0, -0, NaN, '']) {
  const stringVersion = Object.is(falsy, -0) ? '-0' : String(falsy);

  promise_test(() => {

    const rs = recordingReadableStream();

    const ws = recordingWritableStream();
    const writer = ws.getWriter();
    writer.close();
    writer.releaseLock();

    return rs.pipeTo(ws, { preventCancel: falsy }).then(
      () => assert_unreached('the promise must not fulfill'),
      err => {
        assert_equals(err.name, 'TypeError', 'the promise must reject with a TypeError');

        assert_array_equals(rs.eventsWithoutPulls, ['cancel', err]);
        assert_array_equals(ws.events, ['close']);

        return Promise.all([
          rs.getReader().closed,
          ws.getWriter().closed
        ]);
      }
    );

  }, `Closing must be propagated backward: starts closed; preventCancel = ${stringVersion} (falsy); fulfilled cancel ` +
     `promise`);
}

for (const truthy of [true, 'a', 1, Symbol(), { }]) {
  promise_test(t => {

    const rs = recordingReadableStream();

    const ws = recordingWritableStream();
    const writer = ws.getWriter();
    writer.close();
    writer.releaseLock();

    return promise_rejects_js(t, TypeError, rs.pipeTo(ws, { preventCancel: truthy })).then(() => {
      assert_array_equals(rs.eventsWithoutPulls, []);
      assert_array_equals(ws.events, ['close']);

      return ws.getWriter().closed;
    });

  }, `Closing must be propagated backward: starts closed; preventCancel = ${String(truthy)} (truthy)`);
}

promise_test(t => {

  const rs = recordingReadableStream();

  const ws = recordingWritableStream();
  const writer = ws.getWriter();
  writer.close();
  writer.releaseLock();

  return promise_rejects_js(t, TypeError, rs.pipeTo(ws, { preventCancel: true, preventAbort: true }))
    .then(() => {
      assert_array_equals(rs.eventsWithoutPulls, []);
      assert_array_equals(ws.events, ['close']);

      return ws.getWriter().closed;
    });

}, 'Closing must be propagated backward: starts closed; preventCancel = true, preventAbort = true');

promise_test(t => {

  const rs = recordingReadableStream();

  const ws = recordingWritableStream();
  const writer = ws.getWriter();
  writer.close();
  writer.releaseLock();

  return promise_rejects_js(t, TypeError,
                         rs.pipeTo(ws, { preventCancel: true, preventAbort: true, preventClose: true }))
  .then(() => {
    assert_array_equals(rs.eventsWithoutPulls, []);
    assert_array_equals(ws.events, ['close']);

    return ws.getWriter().closed;
  });

}, 'Closing must be propagated backward: starts closed; preventCancel = true, preventAbort = true, preventClose ' +
   '= true');