11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst classes = [
61cb0ef41Sopenharmony_ci  {
71cb0ef41Sopenharmony_ci    name: 'TextDecoderStream',
81cb0ef41Sopenharmony_ci    input: new Uint8Array([65])
91cb0ef41Sopenharmony_ci  },
101cb0ef41Sopenharmony_ci  {
111cb0ef41Sopenharmony_ci    name: 'TextEncoderStream',
121cb0ef41Sopenharmony_ci    input: 'A'
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci];
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst microtasksRun = () => new Promise(resolve => step_timeout(resolve, 0));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifor (const streamClass of classes) {
191cb0ef41Sopenharmony_ci  promise_test(async () => {
201cb0ef41Sopenharmony_ci    const stream = new self[streamClass.name]();
211cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
221cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
231cb0ef41Sopenharmony_ci    const events = [];
241cb0ef41Sopenharmony_ci    await microtasksRun();
251cb0ef41Sopenharmony_ci    const writePromise = writer.write(streamClass.input);
261cb0ef41Sopenharmony_ci    writePromise.then(() => events.push('write'));
271cb0ef41Sopenharmony_ci    await microtasksRun();
281cb0ef41Sopenharmony_ci    events.push('paused');
291cb0ef41Sopenharmony_ci    await reader.read();
301cb0ef41Sopenharmony_ci    events.push('read');
311cb0ef41Sopenharmony_ci    await writePromise;
321cb0ef41Sopenharmony_ci    assert_array_equals(events, ['paused', 'read', 'write'],
331cb0ef41Sopenharmony_ci                        'write should happen after read');
341cb0ef41Sopenharmony_ci  }, 'write() should not complete until read relieves backpressure for ' +
351cb0ef41Sopenharmony_ci     `${streamClass.name}`);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  promise_test(async () => {
381cb0ef41Sopenharmony_ci    const stream = new self[streamClass.name]();
391cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
401cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
411cb0ef41Sopenharmony_ci    const events = [];
421cb0ef41Sopenharmony_ci    await microtasksRun();
431cb0ef41Sopenharmony_ci    const readPromise1 = reader.read();
441cb0ef41Sopenharmony_ci    readPromise1.then(() => events.push('read1'));
451cb0ef41Sopenharmony_ci    const writePromise1 = writer.write(streamClass.input);
461cb0ef41Sopenharmony_ci    const writePromise2 = writer.write(streamClass.input);
471cb0ef41Sopenharmony_ci    writePromise1.then(() => events.push('write1'));
481cb0ef41Sopenharmony_ci    writePromise2.then(() => events.push('write2'));
491cb0ef41Sopenharmony_ci    await microtasksRun();
501cb0ef41Sopenharmony_ci    events.push('paused');
511cb0ef41Sopenharmony_ci    const readPromise2 = reader.read();
521cb0ef41Sopenharmony_ci    readPromise2.then(() => events.push('read2'));
531cb0ef41Sopenharmony_ci    await Promise.all([writePromise1, writePromise2,
541cb0ef41Sopenharmony_ci                       readPromise1, readPromise2]);
551cb0ef41Sopenharmony_ci    assert_array_equals(events, ['read1', 'write1', 'paused', 'read2',
561cb0ef41Sopenharmony_ci                                 'write2'],
571cb0ef41Sopenharmony_ci                        'writes should not happen before read2');
581cb0ef41Sopenharmony_ci  }, 'additional writes should wait for backpressure to be relieved for ' +
591cb0ef41Sopenharmony_ci     `class ${streamClass.name}`);
601cb0ef41Sopenharmony_ci}
61