test_idle_maintenance_and_databases.js (2403B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 /* exported testSteps */ 7 async function testSteps() { 8 info("Creating databases"); 9 10 for (let originIndex = 0; originIndex < 3; originIndex++) { 11 const principal = getPrincipal( 12 "https://www.example" + originIndex + ".com" 13 ); 14 15 for (let databaseIndex = 0; databaseIndex < 5; databaseIndex++) { 16 const dbName = "foo-" + databaseIndex; 17 18 const request = indexedDB.openForPrincipal(principal, dbName, 1); 19 20 { 21 const event = await expectingUpgrade(request); 22 23 const database = event.target.result; 24 25 const objectStore = database.createObjectStore("foo"); 26 27 // Add lots of data... 28 for (let i = 0; i < 100; i++) { 29 objectStore.add("abcdefghijklmnopqrstuvwxyz0123456789", i); 30 } 31 32 // And then clear it so that maintenance has some space to reclaim. 33 objectStore.clear(); 34 } 35 36 const event = await expectingSuccess(request); 37 38 const database = event.target.result; 39 40 database.close(); 41 } 42 } 43 44 info("Starting idle maintenance"); 45 46 const indexedDatabaseManager = Cc[ 47 "@mozilla.org/dom/indexeddb/manager;1" 48 ].getService(Ci.nsIIndexedDatabaseManager); 49 50 const maintenancePromise = indexedDatabaseManager.doMaintenance(); 51 52 info("Getting databases"); 53 54 // Getting databases while idle daily maintenance is running should be delayed 55 // and only processed after the maintenance is done. 56 const completePromises = (function () { 57 let promises = []; 58 59 for (let index = 0; index < 10; index++) { 60 async function sandboxScript() { 61 await indexedDB.databases(); 62 } 63 64 const sandbox = new Cu.Sandbox( 65 getPrincipal("https://www.example" + index + ".org"), 66 { 67 wantGlobalProperties: ["indexedDB"], 68 } 69 ); 70 71 const promise = new Promise(function (resolve, reject) { 72 sandbox.resolve = resolve; 73 sandbox.reject = reject; 74 }); 75 76 Cu.evalInSandbox( 77 sandboxScript.toSource() + " sandboxScript().then(resolve, reject);", 78 sandbox 79 ); 80 81 promises.push(promise); 82 } 83 84 return promises; 85 })(); 86 87 info("Waiting for maintenance to finish"); 88 89 await maintenancePromise; 90 91 info("Waiting for databases operation to finish"); 92 93 await Promise.all(completePromises); 94 }