summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_persist_journal.js
blob: 1eea5c703e2ffc32a72523b6f00ddf9f7b15eb39 (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

// Tests the journal persists on close.

async function check_journal_persists(db, journal) {
  let path = db.databaseFile.path;
  info(`testing ${path}`);
  await new Promise(resolve => {
    db.executeSimpleSQLAsync(`PRAGMA journal_mode = ${journal}`, {
      handleCompletion: resolve,
    });
  });

  await new Promise(resolve => {
    db.executeSimpleSQLAsync("CREATE TABLE test (id INTEGER PRIMARY KEY)", {
      handleCompletion: resolve,
    });
  });

  if (journal == "wal") {
    Assert.ok(await IOUtils.exists(path + "-wal"), "-wal exists before close");
    Assert.greater(
      (await IOUtils.stat(path + "-wal")).size,
      0,
      "-wal size is non-zero"
    );
  } else {
    Assert.ok(
      await IOUtils.exists(path + "-journal"),
      "-journal exists before close"
    );
    Assert.equal(
      (await IOUtils.stat(path + "-journal")).size,
      0,
      "-journal is truncated after every transaction"
    );
  }

  await new Promise(resolve => db.asyncClose(resolve));

  if (journal == "wal") {
    Assert.ok(await IOUtils.exists(path + "-wal"), "-wal persists after close");
    Assert.equal(
      (await IOUtils.stat(path + "-wal")).size,
      0,
      "-wal has been truncated"
    );
  } else {
    Assert.ok(
      await IOUtils.exists(path + "-journal"),
      "-journal persists after close"
    );
    Assert.equal(
      (await IOUtils.stat(path + "-journal")).size,
      0,
      "-journal has been truncated"
    );
  }
}

async function getDbPath(name) {
  let path = PathUtils.join(PathUtils.profileDir, name + ".sqlite");
  Assert.ok(!(await IOUtils.exists(path)), "database should not exist");
  return path;
}

add_task(async function () {
  for (let journal of ["truncate", "wal"]) {
    await check_journal_persists(
      Services.storage.openDatabase(
        new FileUtils.File(await getDbPath(`shared-${journal}`))
      ),
      journal
    );
    await check_journal_persists(
      Services.storage.openUnsharedDatabase(
        new FileUtils.File(await getDbPath(`unshared-${journal}`))
      ),
      journal
    );
    await check_journal_persists(
      await openAsyncDatabase(
        new FileUtils.File(await getDbPath(`async-${journal}`))
      ),
      journal
    );
  }
});