summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/transform-streams/cancel.any.js
blob: 5c7fc4eae5d55b84a562192726066a82b3c19858 (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
// META: global=window,worker,shadowrealm
// META: script=../resources/test-utils.js
'use strict';

const thrownError = new Error('bad things are happening!');
thrownError.name = 'error1';

const originalReason = new Error('original reason');
originalReason.name = 'error2';

promise_test(async t => {
  let cancelled = undefined;
  const ts = new TransformStream({
    cancel(reason) {
      cancelled = reason;
    }
  });
  const res = await ts.readable.cancel(thrownError);
  assert_equals(res, undefined, 'readable.cancel() should return undefined');
  assert_equals(cancelled, thrownError, 'transformer.cancel() should be called with the passed reason');
}, 'cancelling the readable side should call transformer.cancel()');

promise_test(async t => {
  const ts = new TransformStream({
    cancel(reason) {
      assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
      throw thrownError;
    }
  });
  const writer = ts.writable.getWriter();
  const cancelPromise = ts.readable.cancel(originalReason);
  await promise_rejects_exactly(t, thrownError, cancelPromise, 'readable.cancel() should reject with thrownError');
  await promise_rejects_exactly(t, thrownError, writer.closed, 'writer.closed should reject with thrownError');
}, 'cancelling the readable side should reject if transformer.cancel() throws');

promise_test(async t => {
  let aborted = undefined;
  const ts = new TransformStream({
    cancel(reason) {
      aborted = reason;
    },
    flush: t.unreached_func('flush should not be called')
  });
  const res = await ts.writable.abort(thrownError);
  assert_equals(res, undefined, 'writable.abort() should return undefined');
  assert_equals(aborted, thrownError, 'transformer.abort() should be called with the passed reason');
}, 'aborting the writable side should call transformer.abort()');

promise_test(async t => {
  const ts = new TransformStream({
    cancel(reason) {
      assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
      throw thrownError;
    },
    flush: t.unreached_func('flush should not be called')
  });
  const reader = ts.readable.getReader();
  const abortPromise = ts.writable.abort(originalReason);
  await promise_rejects_exactly(t, thrownError, abortPromise, 'writable.abort() should reject with thrownError');
  await promise_rejects_exactly(t, thrownError, reader.closed, 'reader.closed should reject with thrownError');
}, 'aborting the writable side should reject if transformer.cancel() throws');

promise_test(async t => {
  const ts = new TransformStream({
    async cancel(reason) {
      assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
      throw thrownError;
    },
    flush: t.unreached_func('flush should not be called')
  });
  const cancelPromise = ts.readable.cancel(originalReason);
  const closePromise = ts.writable.close();
  await Promise.all([
    promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'),
    promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'),
  ]);
}, 'closing the writable side should reject if a parallel transformer.cancel() throws');

promise_test(async t => {
  let controller;
  const ts = new TransformStream({
    start(c) {
      controller = c;
    },
    async cancel(reason) {
      assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
      controller.error(thrownError);
    },
    flush: t.unreached_func('flush should not be called')
  });
  const cancelPromise = ts.readable.cancel(originalReason);
  const closePromise = ts.writable.close();
  await Promise.all([
    promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError'),
    promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError'),
  ]);
}, 'readable.cancel() and a parallel writable.close() should reject if a transformer.cancel() calls controller.error()');

promise_test(async t => {
  let controller;
  const ts = new TransformStream({
    start(c) {
      controller = c;
    },
    async cancel(reason) {
      assert_equals(reason, originalReason, 'transformer.cancel() should be called with the passed reason');
      controller.error(thrownError);
    },
    flush: t.unreached_func('flush should not be called')
  });
  const cancelPromise = ts.writable.abort(originalReason);
  await promise_rejects_exactly(t, thrownError, cancelPromise, 'cancelPromise should reject with thrownError');
  const closePromise = ts.readable.cancel(1);
  await promise_rejects_exactly(t, thrownError, closePromise, 'closePromise should reject with thrownError');
}, 'writable.abort() and readable.cancel() should reject if a transformer.cancel() calls controller.error()');