summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/piping/flow-control.any.js
blob: 09c4420f872adcf77a61b79ecf4f982cfe3e0a29 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// META: global=window,worker
// META: script=../resources/test-utils.js
// META: script=../resources/rs-utils.js
// META: script=../resources/recording-streams.js
'use strict';

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

promise_test(t => {

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue('a');
      controller.enqueue('b');
      controller.close();
    }
  });

  const ws = recordingWritableStream(undefined, new CountQueuingStrategy({ highWaterMark: 0 }));

  const pipePromise = rs.pipeTo(ws, { preventCancel: true });

  // Wait and make sure it doesn't do any reading.
  return flushAsyncEvents().then(() => {
    ws.controller.error(error1);
  })
  .then(() => promise_rejects_exactly(t, error1, pipePromise, 'pipeTo must reject with the same error'))
  .then(() => {
    assert_array_equals(rs.eventsWithoutPulls, []);
    assert_array_equals(ws.events, []);
  })
  .then(() => readableStreamToArray(rs))
  .then(chunksNotPreviouslyRead => {
    assert_array_equals(chunksNotPreviouslyRead, ['a', 'b']);
  });

}, 'Piping from a non-empty ReadableStream into a WritableStream that does not desire chunks');

promise_test(() => {

  const rs = recordingReadableStream({
    start(controller) {
      controller.enqueue('b');
      controller.close();
    }
  });

  let resolveWritePromise;
  const ws = recordingWritableStream({
    write() {
      if (!resolveWritePromise) {
        // first write
        return new Promise(resolve => {
          resolveWritePromise = resolve;
        });
      }
      return undefined;
    }
  });

  const writer = ws.getWriter();
  const firstWritePromise = writer.write('a');
  assert_equals(writer.desiredSize, 0, 'after writing the writer\'s desiredSize must be 0');
  writer.releaseLock();

  // firstWritePromise won't settle until we call resolveWritePromise.

  const pipePromise = rs.pipeTo(ws);

  return flushAsyncEvents().then(() => resolveWritePromise())
    .then(() => Promise.all([firstWritePromise, pipePromise]))
    .then(() => {
      assert_array_equals(rs.eventsWithoutPulls, []);
      assert_array_equals(ws.events, ['write', 'a', 'write', 'b', 'close']);
    });

}, 'Piping from a non-empty ReadableStream into a WritableStream that does not desire chunks, but then does');

promise_test(() => {

  const rs = recordingReadableStream();

  let resolveWritePromise;
  const ws = recordingWritableStream({
    write() {
      if (!resolveWritePromise) {
        // first write
        return new Promise(resolve => {
          resolveWritePromise = resolve;
        });
      }
      return undefined;
    }
  });

  const writer = ws.getWriter();
  writer.write('a');

  return flushAsyncEvents().then(() => {
    assert_array_equals(ws.events, ['write', 'a']);
    assert_equals(writer.desiredSize, 0, 'after writing the writer\'s desiredSize must be 0');
    writer.releaseLock();

    const pipePromise = rs.pipeTo(ws);

    rs.controller.enqueue('b');
    resolveWritePromise();
    rs.controller.close();

    return pipePromise.then(() => {
      assert_array_equals(rs.eventsWithoutPulls, []);
      assert_array_equals(ws.events, ['write', 'a', 'write', 'b', 'close']);
    });
  });

}, 'Piping from an empty ReadableStream into a WritableStream that does not desire chunks, but then the readable ' +
   'stream becomes non-empty and the writable stream starts desiring chunks');

promise_test(() => {
  const unreadChunks = ['b', 'c', 'd'];

  const rs = recordingReadableStream({
    pull(controller) {
      controller.enqueue(unreadChunks.shift());
      if (unreadChunks.length === 0) {
        controller.close();
      }
    }
  }, new CountQueuingStrategy({ highWaterMark: 0 }));

  let resolveWritePromise;
  const ws = recordingWritableStream({
    write() {
      if (!resolveWritePromise) {
        // first write
        return new Promise(resolve => {
          resolveWritePromise = resolve;
        });
      }
      return undefined;
    }
  }, new CountQueuingStrategy({ highWaterMark: 3 }));

  const writer = ws.getWriter();
  const firstWritePromise = writer.write('a');
  assert_equals(writer.desiredSize, 2, 'after writing the writer\'s desiredSize must be 2');
  writer.releaseLock();

  // firstWritePromise won't settle until we call resolveWritePromise.

  const pipePromise = rs.pipeTo(ws);

  return flushAsyncEvents().then(() => {
    assert_array_equals(ws.events, ['write', 'a']);
    assert_equals(unreadChunks.length, 1, 'chunks should continue to be enqueued until the HWM is reached');
  }).then(() => resolveWritePromise())
    .then(() => Promise.all([firstWritePromise, pipePromise]))
    .then(() => {
      assert_array_equals(rs.events, ['pull', 'pull', 'pull']);
      assert_array_equals(ws.events, ['write', 'a', 'write', 'b','write', 'c','write', 'd', 'close']);
    });

}, 'Piping from a ReadableStream to a WritableStream that desires more chunks before finishing with previous ones');

class StepTracker {
  constructor() {
    this.waiters = [];
    this.wakers = [];
  }

  // Returns promise which resolves when step `n` is reached. Also schedules step n + 1 to happen shortly after the
  // promise is resolved.
  waitThenAdvance(n) {
    if (this.waiters[n] === undefined) {
      this.waiters[n] = new Promise(resolve => {
        this.wakers[n] = resolve;
      });
      this.waiters[n]
          .then(() => flushAsyncEvents())
          .then(() => {
            if (this.wakers[n + 1] !== undefined) {
              this.wakers[n + 1]();
            }
          });
    }
    if (n == 0) {
      this.wakers[0]();
    }
    return this.waiters[n];
  }
}

promise_test(() => {
  const steps = new StepTracker();
  const desiredSizes = [];
  const rs = recordingReadableStream({
    start(controller) {
      steps.waitThenAdvance(1).then(() => enqueue('a'));
      steps.waitThenAdvance(3).then(() => enqueue('b'));
      steps.waitThenAdvance(5).then(() => enqueue('c'));
      steps.waitThenAdvance(7).then(() => enqueue('d'));
      steps.waitThenAdvance(11).then(() => controller.close());

      function enqueue(chunk) {
        controller.enqueue(chunk);
        desiredSizes.push(controller.desiredSize);
      }
    }
  });

  const chunksFinishedWriting = [];
  const writableStartPromise = Promise.resolve();
  let writeCalled = false;
  const ws = recordingWritableStream({
    start() {
      return writableStartPromise;
    },
    write(chunk) {
      const waitForStep = writeCalled ? 12 : 9;
      writeCalled = true;
      return steps.waitThenAdvance(waitForStep).then(() => {
        chunksFinishedWriting.push(chunk);
      });
    }
  });

  return writableStartPromise.then(() => {
    const pipePromise = rs.pipeTo(ws);
    steps.waitThenAdvance(0);

    return Promise.all([
      steps.waitThenAdvance(2).then(() => {
        assert_array_equals(chunksFinishedWriting, [], 'at step 2, zero chunks must have finished writing');
        assert_array_equals(ws.events, ['write', 'a'], 'at step 2, one chunk must have been written');

        // When 'a' (the very first chunk) was enqueued, it was immediately used to fulfill the outstanding read request
        // promise, leaving the queue empty.
        assert_array_equals(desiredSizes, [1],
                            'at step 2, the desiredSize at the last enqueue (step 1) must have been 1');
        assert_equals(rs.controller.desiredSize, 1, 'at step 2, the current desiredSize must be 1');
      }),

      steps.waitThenAdvance(4).then(() => {
        assert_array_equals(chunksFinishedWriting, [], 'at step 4, zero chunks must have finished writing');
        assert_array_equals(ws.events, ['write', 'a'], 'at step 4, one chunk must have been written');

        // When 'b' was enqueued at step 3, the queue was also empty, since immediately after enqueuing 'a' at
        // step 1, it was dequeued in order to fulfill the read() call that was made at step 0. Thus the queue
        // had size 1 (thus desiredSize of 0).
        assert_array_equals(desiredSizes, [1, 0],
                            'at step 4, the desiredSize at the last enqueue (step 3) must have been 0');
        assert_equals(rs.controller.desiredSize, 0, 'at step 4, the current desiredSize must be 0');
      }),

      steps.waitThenAdvance(6).then(() => {
        assert_array_equals(chunksFinishedWriting, [], 'at step 6, zero chunks must have finished writing');
        assert_array_equals(ws.events, ['write', 'a'], 'at step 6, one chunk must have been written');

        // When 'c' was enqueued at step 5, the queue was not empty; it had 'b' in it, since 'b' will not be read until
        // the first write completes at step 9. Thus, the queue size is 2 after enqueuing 'c', giving a desiredSize of
        // -1.
        assert_array_equals(desiredSizes, [1, 0, -1],
                            'at step 6, the desiredSize at the last enqueue (step 5) must have been -1');
        assert_equals(rs.controller.desiredSize, -1, 'at step 6, the current desiredSize must be -1');
      }),

      steps.waitThenAdvance(8).then(() => {
        assert_array_equals(chunksFinishedWriting, [], 'at step 8, zero chunks must have finished writing');
        assert_array_equals(ws.events, ['write', 'a'], 'at step 8, one chunk must have been written');

        // When 'd' was enqueued at step 7, the situation is the same as before, leading to a queue containing 'b', 'c',
        // and 'd'.
        assert_array_equals(desiredSizes, [1, 0, -1, -2],
                            'at step 8, the desiredSize at the last enqueue (step 7) must have been -2');
        assert_equals(rs.controller.desiredSize, -2, 'at step 8, the current desiredSize must be -2');
      }),

      steps.waitThenAdvance(10).then(() => {
        assert_array_equals(chunksFinishedWriting, ['a'], 'at step 10, one chunk must have finished writing');
        assert_array_equals(ws.events, ['write', 'a', 'write', 'b'],
                            'at step 10, two chunks must have been written');

        assert_equals(rs.controller.desiredSize, -1, 'at step 10, the current desiredSize must be -1');
      }),

      pipePromise.then(() => {
        assert_array_equals(desiredSizes, [1, 0, -1, -2], 'backpressure must have been exerted at the source');
        assert_array_equals(chunksFinishedWriting, ['a', 'b', 'c', 'd'], 'all chunks finished writing');

        assert_array_equals(rs.eventsWithoutPulls, [], 'nothing unexpected should happen to the ReadableStream');
        assert_array_equals(ws.events, ['write', 'a', 'write', 'b', 'write', 'c', 'write', 'd', 'close'],
                            'all chunks were written (and the WritableStream closed)');
      })
    ]);
  });
}, 'Piping to a WritableStream that does not consume the writes fast enough exerts backpressure on the ReadableStream');