diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/web-platform/tests/web-locks/held.tentative.https.any.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/web-locks/held.tentative.https.any.js')
-rw-r--r-- | testing/web-platform/tests/web-locks/held.tentative.https.any.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/testing/web-platform/tests/web-locks/held.tentative.https.any.js b/testing/web-platform/tests/web-locks/held.tentative.https.any.js new file mode 100644 index 0000000000..7fc4c73540 --- /dev/null +++ b/testing/web-platform/tests/web-locks/held.tentative.https.any.js @@ -0,0 +1,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'); |