Bug 1945643 - Update to mozilla-nimbus-schemas 2025.1.1 r=chumphreys
[gecko.git] / dom / quota / test / xpcshell / test_storagePressure.js
blobb115c68629176c243429b82a0ac99fb8190c58b9
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 /**
7 * This test is mainly to verify that the storage pressure event is fired when
8 * the eviction process is not able to free some space when a quota client
9 * attempts to write over the global limit or when the global limit is reduced
10 * below the global usage.
13 const { TestUtils } = ChromeUtils.importESModule(
14 "resource://testing-common/TestUtils.sys.mjs"
17 loadScript("dom/quota/test/common/file.js");
19 async function awaitStoragePressure() {
20 const [subject] = await TestUtils.topicObserved(
21 "QuotaManager::StoragePressure"
23 const usage = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
24 return usage;
27 async function testSteps() {
28 const globalLimitKB = 2;
30 const principal = getPrincipal("https://example.com");
32 info("Setting limits");
34 setGlobalLimit(globalLimitKB);
36 info("Initializing");
38 let request = init();
39 await requestFinished(request);
41 info("Initializing temporary storage");
43 request = initTemporaryStorage();
44 await requestFinished(request);
46 info("Persisting and filling an origin");
48 // We need to persist the origin first to omit the group limit checks.
49 // Otherwise, we would have to fill five separate origins.
50 request = persist(principal);
51 await requestFinished(request);
53 let database = getSimpleDatabase(principal);
55 request = database.open("data");
56 await requestFinished(request);
58 try {
59 request = database.write(getBuffer(globalLimitKB * 1024));
60 await requestFinished(request);
62 ok(true, "Should not have thrown");
63 } catch (ex) {
64 ok(false, "Should not have thrown");
67 info("Testing storage pressure by writing over the global limit");
69 info("Storing one more byte to get the storage pressure event while writing");
71 let promiseStoragePressure = awaitStoragePressure();
73 try {
74 request = database.write(getBuffer(1));
75 await requestFinished(request);
77 ok(false, "Should have thrown");
78 } catch (e) {
79 ok(true, "Should have thrown");
80 Assert.strictEqual(
81 e.resultCode,
82 NS_ERROR_FILE_NO_DEVICE_SPACE,
83 "Threw right result code"
87 info("Checking the storage pressure event");
89 let usage = await promiseStoragePressure;
90 Assert.equal(usage, globalLimitKB * 1024, "Got correct usage");
92 info("Testing storage pressure by reducing the global limit");
94 info(
95 "Reducing the global limit to get the storage pressuse event while the" +
96 " temporary storage is being initialized"
99 setGlobalLimit(globalLimitKB - 1);
101 request = reset();
102 await requestFinished(request);
104 info("Initializing");
106 request = init();
107 await requestFinished(request);
109 promiseStoragePressure = awaitStoragePressure();
111 info("Initializing temporary storage");
113 request = initTemporaryStorage();
114 await requestFinished(request);
116 info("Checking the storage pressure event");
118 usage = await promiseStoragePressure;
119 Assert.equal(usage, globalLimitKB * 1024, "Got correct usage");
121 info("Resetting limits");
123 resetGlobalLimit();
125 request = reset();
126 await requestFinished(request);