Skip to content

Commit

Permalink
Fix a race in /IndexedDB/get-databases.any.js
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D225482

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1924323
gecko-commit: b8171301bcf35ae5a90fc910d69885673d507e17
gecko-reviewers: dom-storage-reviewers, asuth
  • Loading branch information
janvarga authored and moz-wptsync-bot committed Oct 29, 2024
1 parent 5cbb452 commit 7965556
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions IndexedDB/get-databases.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,32 @@ promise_test(async testCase => {
}, "Make sure an empty list is returned for the case of no databases.");

promise_test(async testCase => {
function sleep_sync(msec) {
const start = new Date().getTime();
while (new Date().getTime() - start < msec) {}
}

// Delete any databases that may not have been cleaned up after previous test
// runs as well as the two databases made above.
await deleteAllDatabases(testCase);

const db1 = await createNamedDatabase(testCase, "DB1", ()=>{});
let databases_promise1;
const db2 = await createNamedDatabase(testCase, "DB2", async () => {
const databases_result1 = await indexedDB.databases();
assert_equals(
databases_result1.length,
1,
"The result of databases() should be only those databases which have "
+ "been created at the time of calling, regardless of versionchange "
+ "transactions currently running.");
databases_promise1 = indexedDB.databases();

// Give databases() operation a chance to fetch all current info about
// existing databases. This must be a sync sleep since await would trigger
// auto commit of the upgrade transaction.
sleep_sync(1000);
});
const databases_result1 = await databases_promise1;
assert_equals(
databases_result1.length,
1,
"The result of databases() should be only those databases which have "
+ "been created at the time of calling, regardless of versionchange "
+ "transactions currently running.");
db1.close();
db2.close();
const databases_result2 = await indexedDB.databases();
Expand All @@ -107,13 +119,20 @@ promise_test(async testCase => {
2,
"The result of databases() should include all databases which have "
+ "been created at the time of calling.");
let databases_promise3;
await migrateNamedDatabase(testCase, "DB2", 2, async () => {
const databases_result3 = await indexedDB.databases();
assert_true(
databases_result3[0].version === 1
&& databases_result3[1].version === 1,
"The result of databases() should contain the versions of databases "
+ "at the time of calling, regardless of versionchange transactions "
+ "currently running.");
databases_promise3 = indexedDB.databases();

// Give databases() operation a chance to fetch all current info about
// existing databases. This must be a sync sleep since await would trigger
// auto commit of the upgrade transaction.
sleep_sync(1000);
});
const databases_result3 = await databases_promise3;
assert_true(
databases_result3[0].version === 1
&& databases_result3[1].version === 1,
"The result of databases() should contain the versions of databases "
+ "at the time of calling, regardless of versionchange transactions "
+ "currently running.");
}, "Ensure that databases() doesn't pick up changes that haven't commited.");

0 comments on commit 7965556

Please sign in to comment.