11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst { finished, addAbortSignal } = require('stream');
51cb0ef41Sopenharmony_ciconst { ReadableStream, WritableStream } = require('stream/web');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cifunction createTestReadableStream() {
91cb0ef41Sopenharmony_ci  return new ReadableStream({
101cb0ef41Sopenharmony_ci    start(controller) {
111cb0ef41Sopenharmony_ci      controller.enqueue('a');
121cb0ef41Sopenharmony_ci      controller.enqueue('b');
131cb0ef41Sopenharmony_ci      controller.enqueue('c');
141cb0ef41Sopenharmony_ci      controller.close();
151cb0ef41Sopenharmony_ci    }
161cb0ef41Sopenharmony_ci  });
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cifunction createTestWritableStream(values) {
201cb0ef41Sopenharmony_ci  return new WritableStream({
211cb0ef41Sopenharmony_ci    write(chunk) {
221cb0ef41Sopenharmony_ci      values.push(chunk);
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci  const rs = createTestReadableStream();
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const reader = rs.getReader();
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  const ac = new AbortController();
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, rs);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  finished(rs, common.mustCall((err) => {
371cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
381cb0ef41Sopenharmony_ci    assert.rejects(reader.read(), /AbortError/).then(common.mustCall());
391cb0ef41Sopenharmony_ci    assert.rejects(reader.closed, /AbortError/).then(common.mustCall());
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall((result) => {
431cb0ef41Sopenharmony_ci    assert.strictEqual(result.value, 'a');
441cb0ef41Sopenharmony_ci    ac.abort();
451cb0ef41Sopenharmony_ci  }));
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci{
491cb0ef41Sopenharmony_ci  const rs = createTestReadableStream();
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  const ac = new AbortController();
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, rs);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  assert.rejects((async () => {
561cb0ef41Sopenharmony_ci    for await (const chunk of rs) {
571cb0ef41Sopenharmony_ci      if (chunk === 'b') {
581cb0ef41Sopenharmony_ci        ac.abort();
591cb0ef41Sopenharmony_ci      }
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci  })(), /AbortError/).then(common.mustCall());
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci{
651cb0ef41Sopenharmony_ci  const rs1 = createTestReadableStream();
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  const rs2 = createTestReadableStream();
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  const ac = new AbortController();
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, rs1);
721cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, rs2);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  const reader1 = rs1.getReader();
751cb0ef41Sopenharmony_ci  const reader2 = rs2.getReader();
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  finished(rs1, common.mustCall((err) => {
781cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
791cb0ef41Sopenharmony_ci    assert.rejects(reader1.read(), /AbortError/).then(common.mustCall());
801cb0ef41Sopenharmony_ci    assert.rejects(reader1.closed, /AbortError/).then(common.mustCall());
811cb0ef41Sopenharmony_ci  }));
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  finished(rs2, common.mustCall((err) => {
841cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
851cb0ef41Sopenharmony_ci    assert.rejects(reader2.read(), /AbortError/).then(common.mustCall());
861cb0ef41Sopenharmony_ci    assert.rejects(reader2.closed, /AbortError/).then(common.mustCall());
871cb0ef41Sopenharmony_ci  }));
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  ac.abort();
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci{
931cb0ef41Sopenharmony_ci  const rs = createTestReadableStream();
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  const { 0: rs1, 1: rs2 } = rs.tee();
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  const ac = new AbortController();
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, rs);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  const reader1 = rs1.getReader();
1021cb0ef41Sopenharmony_ci  const reader2 = rs2.getReader();
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  finished(rs1, common.mustCall((err) => {
1051cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
1061cb0ef41Sopenharmony_ci    assert.rejects(reader1.read(), /AbortError/).then(common.mustCall());
1071cb0ef41Sopenharmony_ci    assert.rejects(reader1.closed, /AbortError/).then(common.mustCall());
1081cb0ef41Sopenharmony_ci  }));
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  finished(rs2, common.mustCall((err) => {
1111cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
1121cb0ef41Sopenharmony_ci    assert.rejects(reader2.read(), /AbortError/).then(common.mustCall());
1131cb0ef41Sopenharmony_ci    assert.rejects(reader2.closed, /AbortError/).then(common.mustCall());
1141cb0ef41Sopenharmony_ci  }));
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci  ac.abort();
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci{
1201cb0ef41Sopenharmony_ci  const values = [];
1211cb0ef41Sopenharmony_ci  const ws = createTestWritableStream(values);
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  const ac = new AbortController();
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, ws);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  finished(ws, common.mustCall((err) => {
1301cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
1311cb0ef41Sopenharmony_ci    assert.deepStrictEqual(values, ['a']);
1321cb0ef41Sopenharmony_ci    assert.rejects(writer.write('b'), /AbortError/).then(common.mustCall());
1331cb0ef41Sopenharmony_ci    assert.rejects(writer.closed, /AbortError/).then(common.mustCall());
1341cb0ef41Sopenharmony_ci  }));
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  writer.write('a').then(() => {
1371cb0ef41Sopenharmony_ci    ac.abort();
1381cb0ef41Sopenharmony_ci  });
1391cb0ef41Sopenharmony_ci}
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci{
1421cb0ef41Sopenharmony_ci  const values = [];
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  const ws1 = createTestWritableStream(values);
1451cb0ef41Sopenharmony_ci  const ws2 = createTestWritableStream(values);
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  const ac = new AbortController();
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, ws1);
1501cb0ef41Sopenharmony_ci  addAbortSignal(ac.signal, ws2);
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  const writer1 = ws1.getWriter();
1531cb0ef41Sopenharmony_ci  const writer2 = ws2.getWriter();
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  finished(ws1, common.mustCall((err) => {
1561cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
1571cb0ef41Sopenharmony_ci    assert.rejects(writer1.write('a'), /AbortError/).then(common.mustCall());
1581cb0ef41Sopenharmony_ci    assert.rejects(writer1.closed, /AbortError/).then(common.mustCall());
1591cb0ef41Sopenharmony_ci  }));
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci  finished(ws2, common.mustCall((err) => {
1621cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
1631cb0ef41Sopenharmony_ci    assert.rejects(writer2.write('a'), /AbortError/).then(common.mustCall());
1641cb0ef41Sopenharmony_ci    assert.rejects(writer2.closed, /AbortError/).then(common.mustCall());
1651cb0ef41Sopenharmony_ci  }));
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  ac.abort();
1681cb0ef41Sopenharmony_ci}
169