summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-locks/held.https.any.js
blob: 7fc4c7354054c08a70e6b1ad050958c0303af5fa (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
// META: title=Web Locks API: Lock held until callback result resolves
// META: script=resources/helpers.js
// META: global=window,dedicatedworker,sharedworker,serviceworker

'use strict';

// For uncaught rejections.
setup({allow_uncaught_exception: true});

function snooze(t, ms) { return new Promise(r => t.step_timeout(r, ms)); }

promise_test(async t => {
  const res = uniqueName(t);
  const p = navigator.locks.request(res, lock => 123);
  assert_equals(Promise.resolve(p), p, 'request() result is a Promise');
  assert_equals(await p, 123, 'promise resolves to the returned value');
}, 'callback\'s result is promisified if not async');

promise_test(async t => {
  const res = uniqueName(t);
  // Resolved when the lock is granted.
  let granted;
  const lock_granted_promise = new Promise(r => { granted = r; });

  // Lock is held until this is resolved.
  let resolve;
  const lock_release_promise = new Promise(r => { resolve = r; });

  const order = [];

  navigator.locks.request(res, lock => {
    granted(lock);
    return lock_release_promise;
  });
  await lock_granted_promise;

  await Promise.all([
    snooze(t, 50).then(() => {
      order.push('1st lock released');
      resolve();
    }),
    navigator.locks.request(res, () => {
      order.push('2nd lock granted');
    })
  ]);

  assert_array_equals(order, ['1st lock released', '2nd lock granted']);
}, 'lock is held until callback\'s returned promise resolves');

promise_test(async t => {
  const res = uniqueName(t);
  // Resolved when the lock is granted.
  let granted;
  const lock_granted_promise = new Promise(r => { granted = r; });

  // Lock is held until this is rejected.
  let reject;
  const lock_release_promise = new Promise((_, r) => { reject = r; });

  const order = [];

  navigator.locks.request(res, lock => {
    granted(lock);
    return lock_release_promise;
  });
  await lock_granted_promise;

  await Promise.all([
    snooze(t, 50).then(() => {
      order.push('reject');
      reject(new Error('this uncaught rejection is expected'));
    }),
    navigator.locks.request(res, () => {
      order.push('2nd lock granted');
    })
  ]);

  assert_array_equals(order, ['reject', '2nd lock granted']);
}, 'lock is held until callback\'s returned promise rejects');

promise_test(async t => {
  const res = uniqueName(t);
  let callback_called = false;
  await navigator.locks.request(res, async lock => {
    await navigator.locks.request(res, {ifAvailable: true}, lock => {
      callback_called = true;
      assert_equals(lock, null, 'lock request should fail if held');
    });
  });
  assert_true(callback_called, 'callback should have executed');
}, 'held lock prevents the same client from acquiring it');