summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_sqlite_autoVacuum.js
blob: f3a6dced3fb8ba7b2e59b1305a4503f60427b47e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";

const { Sqlite } = ChromeUtils.importESModule(
  "resource://gre/modules/Sqlite.sys.mjs"
);
const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

/**
 * Sends a fake idle-daily notification to the VACUUM Manager.
 */
function synthesize_idle_daily() {
  Cc["@mozilla.org/storage/vacuum;1"]
    .getService(Ci.nsIObserver)
    .observe(null, "idle-daily", null);
}

function unregister_vacuum_participants() {
  // First unregister other participants.
  for (let { data: entry } of Services.catMan.enumerateCategory(
    "vacuum-participant"
  )) {
    Services.catMan.deleteCategoryEntry("vacuum-participant", entry, false);
  }
}

function reset_vacuum_date(dbname) {
  let date = parseInt(Date.now() / 1000 - 31 * 86400);
  // Set last VACUUM to a date in the past.
  Services.prefs.setIntPref(`storage.vacuum.last.${dbname}`, date);
  return date;
}

function get_vacuum_date(dbname) {
  return Services.prefs.getIntPref(`storage.vacuum.last.${dbname}`, 0);
}

async function get_freelist_count(conn) {
  return (await conn.execute("PRAGMA freelist_count"))[0].getResultByIndex(0);
}

async function get_auto_vacuum(conn) {
  return (await conn.execute("PRAGMA auto_vacuum"))[0].getResultByIndex(0);
}

async function test_vacuum(options = {}) {
  unregister_vacuum_participants();
  const dbName = "testVacuum.sqlite";
  const dbFile = PathUtils.join(PathUtils.profileDir, dbName);
  let lastVacuumDate = reset_vacuum_date(dbName);
  let conn = await Sqlite.openConnection(
    Object.assign(
      {
        path: dbFile,
        vacuumOnIdle: true,
      },
      options
    )
  );
  // Ensure the category manager is up-to-date.
  await TestUtils.waitForTick();

  Assert.equal(
    await get_auto_vacuum(conn),
    options.incrementalVacuum ? 2 : 0,
    "Check auto_vacuum"
  );

  // Generate some freelist page.
  await conn.execute("CREATE TABLE test (id INTEGER)");
  await conn.execute("DROP TABLE test");
  Assert.greater(await get_freelist_count(conn), 0, "Check freelist_count");

  let promiseVacuumEnd = TestUtils.topicObserved(
    "vacuum-end",
    (_, d) => d == dbName
  );
  synthesize_idle_daily();
  info("Await vacuum end");
  await promiseVacuumEnd;

  Assert.greater(get_vacuum_date(dbName), lastVacuumDate);

  Assert.equal(await get_freelist_count(conn), 0, "Check freelist_count");

  await conn.close();
  await IOUtils.remove(dbFile);
}

add_task(async function test_vacuumOnIdle() {
  info("Test full vacuum");
  await test_vacuum();
  info("Test incremental vacuum");
  await test_vacuum({ incrementalVacuum: true });
});