summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-locks/signal.https.any.js
blob: 5a37e3ae87182eb2ea1909c9e5e29b3f50fd248b (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
// META: title=Web Locks API: AbortSignal integration
// META: script=resources/helpers.js
// META: global=window,dedicatedworker,sharedworker,serviceworker

'use strict';

promise_test(async t => {
  const res = uniqueName(t);

  // These cases should not work:
  for (const signal of ['string', 12.34, false, {}, Symbol(), () => {}, self]) {
    await promise_rejects_js(
      t, TypeError,
      navigator.locks.request(
        res, {signal}, t.unreached_func('callback should not run')),
      'Bindings should throw if the signal option is a not an AbortSignal');
  }
}, 'The signal option must be an AbortSignal');

promise_test(async t => {
  const res = uniqueName(t);
  const controller = new AbortController();
  controller.abort();

  await promise_rejects_dom(
    t, 'AbortError',
    navigator.locks.request(res, {signal: controller.signal},
                            t.unreached_func('callback should not run')),
    'Request should reject with AbortError');
}, 'Passing an already aborted signal aborts');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();
  const reason = 'My dog ate it.';
  controller.abort(reason);

  const promise =
        navigator.locks.request(res, {signal: controller.signal},
                                t.unreached_func('callback should not run'));

  await promise_rejects_exactly(
    t, reason, promise, "Rejection should give the abort reason");
}, 'Passing an already aborted signal rejects with the custom abort reason.');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();
  controller.abort();

  const promise =
        navigator.locks.request(res, {signal: controller.signal},
                                t.unreached_func('callback should not run'));

  await promise_rejects_exactly(
    t, controller.signal.reason, promise,
    "Rejection should give the abort reason");
}, 'Passing an already aborted signal rejects with the default abort reason.');

promise_test(async t => {
  const res = uniqueName(t);

  // Grab a lock and hold it until this subtest completes.
  requestLockAndHold(t, res);

  const controller = new AbortController();

  const promise =
    navigator.locks.request(res, {signal: controller.signal},
                            t.unreached_func('callback should not run'));

  // Verify the request is enqueued:
  const state = await navigator.locks.query();
  assert_equals(state.held.filter(lock => lock.name === res).length, 1,
                'Number of held locks');
  assert_equals(state.pending.filter(lock => lock.name === res).length, 1,
                'Number of pending locks');

  const rejected = promise_rejects_dom(
    t, 'AbortError', promise, 'Request should reject with AbortError');

  controller.abort();

  await rejected;

}, 'An aborted request results in AbortError');

promise_test(async t => {
  const res = uniqueName(t);

  // Grab a lock and hold it until this subtest completes.
  requestLockAndHold(t, res);

  const controller = new AbortController();

  const promise =
    navigator.locks.request(res, {signal: controller.signal}, lock => {});

  // Verify the request is enqueued:
  const state = await navigator.locks.query();
  assert_equals(state.held.filter(lock => lock.name === res).length, 1,
                'Number of held locks');
  assert_equals(state.pending.filter(lock => lock.name === res).length, 1,
                'Number of pending locks');

  const rejected = promise_rejects_dom(
    t, 'AbortError', promise, 'Request should reject with AbortError');

  let callback_called = false;
  t.step_timeout(() => {
    callback_called = true;
    controller.abort();
  }, 10);

  await rejected;
  assert_true(callback_called, 'timeout should have caused the abort');

}, 'Abort after a timeout');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();

  let got_lock = false;
  await navigator.locks.request(
    res, {signal: controller.signal}, async lock => { got_lock = true; });

  assert_true(got_lock, 'Lock should be acquired if abort is not signaled.');

}, 'Signal that is not aborted');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();

  let got_lock = false;
  const p = navigator.locks.request(
    res, {signal: controller.signal}, lock => { got_lock = true; });

  // Even though lock is grantable, this abort should be processed synchronously.
  controller.abort();

  await promise_rejects_dom(t, 'AbortError', p, 'Request should abort');

  assert_false(got_lock, 'Request should be aborted if signal is synchronous');

  await navigator.locks.request(res, lock => { got_lock = true; });
  assert_true(got_lock, 'Subsequent request should not be blocked');

}, 'Synchronously signaled abort');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();

  // Make a promise that resolves when the lock is acquired.
  const [acquired_promise, acquired_func] = makePromiseAndResolveFunc();

  // Request the lock.
  let release_func;
  const released_promise = navigator.locks.request(
    res, {signal: controller.signal}, lock => {
      acquired_func();

      // Hold lock until release_func is called.
      const [waiting_promise, waiting_func] = makePromiseAndResolveFunc();
      release_func = waiting_func;
      return waiting_promise;
    });

  // Wait for the lock to be acquired.
  await acquired_promise;

  // Signal an abort.
  controller.abort();

  // Release the lock.
  release_func('resolved ok');

  assert_equals(await released_promise, 'resolved ok',
                'Lock released promise should not reject');

}, 'Abort signaled after lock granted');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();

  // Make a promise that resolves when the lock is acquired.
  const [acquired_promise, acquired_func] = makePromiseAndResolveFunc();

  // Request the lock.
  let release_func;
  const released_promise = navigator.locks.request(
    res, {signal: controller.signal}, lock => {
      acquired_func();

      // Hold lock until release_func is called.
      const [waiting_promise, waiting_func] = makePromiseAndResolveFunc();
      release_func = waiting_func;
      return waiting_promise;
    });

  // Wait for the lock to be acquired.
  await acquired_promise;

  // Release the lock.
  release_func('resolved ok');

  // Signal an abort.
  controller.abort();

  assert_equals(await released_promise, 'resolved ok',
                'Lock released promise should not reject');

}, 'Abort signaled after lock released');

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();
  const first = requestLockAndHold(t, res, { signal: controller.signal });
  const next = navigator.locks.request(res, () => "resolved");
  controller.abort();

  await promise_rejects_dom(t, "AbortError", first, "Request should abort");
  assert_equals(
    await next,
    "resolved",
    "The next request is processed after abort"
  );
}, "Abort should process the next pending lock request");

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();
  const promise = requestLockAndHold(t, res, { signal: controller.signal });

  const reason = "My cat handled it";
  controller.abort(reason);

  await promise_rejects_exactly(t, reason, promise, "Rejection should give the abort reason");
}, "Aborted promise should reject with the custom abort reason");

promise_test(async t => {
  const res = uniqueName(t);

  const controller = new AbortController();
  const promise = requestLockAndHold(t, res, { signal: controller.signal });

  controller.abort();

  await promise_rejects_exactly(t, controller.signal.reason, promise, "Should be the same reason");
}, "Aborted promise should reject with the default abort reason");