node/test/fixtures/wpt/web-locks/storage-buckets.tentative.https.any.js
ishabi 062e8b5a74 worker: add web locks api
PR-URL: https://github.com/nodejs/node/pull/58666
Fixes: https://github.com/nodejs/node/pull/36502
Refs: https://w3c.github.io/web-locks
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2025-07-18 07:55:10 -07:00

56 lines
1.9 KiB
JavaScript

// META: title=Web Locks API: Storage Buckets have independent lock sets
// META: script=resources/helpers.js
// META: script=/storage/buckets/resources/util.js
// META: global=window,dedicatedworker,sharedworker,serviceworker
'use strict';
/**
* Returns whether bucket1 and bucket2 share locks
* @param {*} t test runner object
* @param {*} bucket1 Storage bucket
* @param {*} bucket2 Storage bucket
*/
async function locksAreShared(t, bucket1, bucket2) {
const lock_name = self.uniqueName(t);
let callback_called = false;
let locks_are_shared;
await bucket1.locks.request(lock_name, async lock => {
await bucket2.locks.request(
lock_name, { ifAvailable: true }, async lock => {
callback_called = true;
locks_are_shared = lock == null;
});
});
assert_true(callback_called, 'callback should be called');
return locks_are_shared;
}
promise_test(async t => {
await prepareForBucketTest(t);
const inboxBucket = await navigator.storageBuckets.open('inbox');
const draftsBucket = await navigator.storageBuckets.open('drafts');
assert_true(
await locksAreShared(t, navigator, navigator),
'The default bucket should share locks with itself');
assert_true(
await locksAreShared(t, inboxBucket, inboxBucket),
'A non default bucket should share locks with itself');
assert_false(
await locksAreShared(t, navigator, inboxBucket),
'The default bucket shouldn\'t share locks with a non default bucket');
assert_false(
await locksAreShared(t, draftsBucket, inboxBucket),
'Two different non default buckets shouldn\'t share locks');
const inboxBucket2 = await navigator.storageBuckets.open('inbox');
assert_true(
await self.locksAreShared(t, inboxBucket, inboxBucket2),
'A two instances of the same non default bucket should share locks with theirselves');
}, 'Storage buckets have independent locks');