• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: global=window,worker
2'use strict';
3
4test(() => {
5
6  const theError = new Error('a unique string');
7
8  assert_throws_exactly(theError, () => {
9    new ReadableStream({}, {
10      get size() {
11        throw theError;
12      },
13      highWaterMark: 5
14    });
15  }, 'construction should re-throw the error');
16
17}, 'Readable stream: throwing strategy.size getter');
18
19promise_test(t => {
20
21  const controllerError = { name: 'controller error' };
22  const thrownError = { name: 'thrown error' };
23
24  let controller;
25  const rs = new ReadableStream(
26    {
27      start(c) {
28        controller = c;
29      }
30    },
31    {
32      size() {
33        controller.error(controllerError);
34        throw thrownError;
35      },
36      highWaterMark: 5
37    }
38  );
39
40  assert_throws_exactly(thrownError, () => controller.enqueue('a'), 'enqueue should re-throw the error');
41
42  return promise_rejects_exactly(t, controllerError, rs.getReader().closed);
43
44}, 'Readable stream: strategy.size errors the stream and then throws');
45
46promise_test(t => {
47
48  const theError = { name: 'my error' };
49
50  let controller;
51  const rs = new ReadableStream(
52    {
53      start(c) {
54        controller = c;
55      }
56    },
57    {
58      size() {
59        controller.error(theError);
60        return Infinity;
61      },
62      highWaterMark: 5
63    }
64  );
65
66  assert_throws_js(RangeError, () => controller.enqueue('a'), 'enqueue should throw a RangeError');
67
68  return promise_rejects_exactly(t, theError, rs.getReader().closed, 'closed should reject with the error');
69
70}, 'Readable stream: strategy.size errors the stream and then returns Infinity');
71
72promise_test(() => {
73
74  const theError = new Error('a unique string');
75  const rs = new ReadableStream(
76    {
77      start(c) {
78        assert_throws_exactly(theError, () => c.enqueue('a'), 'enqueue should throw the error');
79      }
80    },
81    {
82      size() {
83        throw theError;
84      },
85      highWaterMark: 5
86    }
87  );
88
89  return rs.getReader().closed.catch(e => {
90    assert_equals(e, theError, 'closed should reject with the error');
91  });
92
93}, 'Readable stream: throwing strategy.size method');
94
95test(() => {
96
97  const theError = new Error('a unique string');
98
99  assert_throws_exactly(theError, () => {
100    new ReadableStream({}, {
101      size() {
102        return 1;
103      },
104      get highWaterMark() {
105        throw theError;
106      }
107    });
108  }, 'construction should re-throw the error');
109
110}, 'Readable stream: throwing strategy.highWaterMark getter');
111
112test(() => {
113
114  for (const highWaterMark of [-1, -Infinity, NaN, 'foo', {}]) {
115    assert_throws_js(RangeError, () => {
116      new ReadableStream({}, {
117        size() {
118          return 1;
119        },
120        highWaterMark
121      });
122    }, 'construction should throw a RangeError for ' + highWaterMark);
123  }
124
125}, 'Readable stream: invalid strategy.highWaterMark');
126
127promise_test(() => {
128
129  const promises = [];
130  for (const size of [NaN, -Infinity, Infinity, -1]) {
131    let theError;
132    const rs = new ReadableStream(
133      {
134        start(c) {
135          try {
136            c.enqueue('hi');
137            assert_unreached('enqueue didn\'t throw');
138          } catch (error) {
139            assert_equals(error.name, 'RangeError', 'enqueue should throw a RangeError for ' + size);
140            theError = error;
141          }
142        }
143      },
144      {
145        size() {
146          return size;
147        },
148        highWaterMark: 5
149      }
150    );
151
152    promises.push(rs.getReader().closed.catch(e => {
153      assert_equals(e, theError, 'closed should reject with the error for ' + size);
154    }));
155  }
156
157  return Promise.all(promises);
158
159}, 'Readable stream: invalid strategy.size return value');
160