summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/sync/test_bookmark_abort_merging.js
blob: 877feb99f452f465bb5ea212ee8bd89d885d8b65 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var { AsyncShutdown } = ChromeUtils.importESModule(
  "resource://gre/modules/AsyncShutdown.sys.mjs"
);

add_task(async function test_transaction_in_progress() {
  let buf = await openMirror("transaction_in_progress");

  await storeRecords(buf, [
    {
      id: "menu",
      parentid: "places",
      type: "folder",
      children: ["bookmarkAAAA"],
    },
    {
      id: "bookmarkAAAA",
      parentid: "menu",
      type: "bookmark",
      title: "A",
      bmkUri: "http://example.com/a",
    },
  ]);

  // This transaction should block merging until the transaction is committed.
  info("Open transaction on Places connection");
  await buf.db.execute("BEGIN EXCLUSIVE");

  await Assert.rejects(
    buf.apply(),
    ex => ex.name == "MergeConflictError",
    "Should not merge when a transaction is in progress"
  );

  info("Commit open transaction");
  await buf.db.execute("COMMIT");

  info("Merging should succeed after committing");
  await buf.apply();

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});

add_task(async function test_abort_store() {
  let buf = await openMirror("abort_store");

  let controller = new AbortController();
  controller.abort();
  await Assert.rejects(
    storeRecords(
      buf,
      [
        {
          id: "menu",
          parentid: "places",
          type: "folder",
          children: [],
        },
      ],
      { signal: controller.signal }
    ),
    ex => ex.name == "InterruptedError",
    "Should abort storing when signaled"
  );

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});

add_task(async function test_abort_merging() {
  let buf = await openMirror("abort_merging");

  let controller = new AbortController();
  controller.abort();
  await Assert.rejects(
    buf.apply({ signal: controller.signal }),
    ex => ex.name == "InterruptedError",
    "Should abort merge when signaled"
  );

  // Even though the merger is already finalized on the Rust side, the DB
  // connection is still open on the JS side. Finalizing `buf` closes it.
  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});

add_task(async function test_blocker_state() {
  let barrier = new AsyncShutdown.Barrier("Test");
  let buf = await SyncedBookmarksMirror.open({
    path: "blocker_state_buf.sqlite",
    finalizeAt: barrier.client,
    recordStepTelemetry(...args) {},
    recordValidationTelemetry(...args) {},
  });
  await storeRecords(buf, [
    {
      id: "menu",
      parentid: "places",
      type: "folder",
      children: ["bookmarkAAAA"],
    },
    {
      id: "bookmarkAAAA",
      parentid: "menu",
      type: "bookmark",
      title: "A",
      bmkUri: "http://example.com/a",
    },
  ]);

  await buf.tryApply(buf.finalizeController.signal);
  await barrier.wait();

  let state = buf.progress.fetchState();
  let names = [];
  for (let s of state.steps) {
    equal(typeof s.at, "number", `Should report timestamp for ${s.step}`);
    switch (s.step) {
      case "fetchLocalTree":
        greaterOrEqual(
          s.took,
          0,
          "Should report time taken to fetch local tree"
        );
        deepEqual(
          s.counts,
          [
            { name: "items", count: 6 },
            { name: "deletions", count: 0 },
          ],
          "Should report number of items in local tree"
        );
        break;

      case "fetchRemoteTree":
        greaterOrEqual(
          s.took,
          0,
          "Should report time taken to fetch remote tree"
        );
        deepEqual(
          s.counts,
          [
            { name: "items", count: 6 },
            { name: "deletions", count: 0 },
          ],
          "Should report number of items in remote tree"
        );
        break;

      case "merge":
        greaterOrEqual(s.took, 0, "Should report time taken to merge");
        deepEqual(
          s.counts,
          [{ name: "items", count: 6 }],
          "Should report merge stats"
        );
        break;

      case "apply":
        greaterOrEqual(s.took, 0, "Should report time taken to apply");
        ok(!("counts" in s), "Should not report counts for applying");
        break;

      case "notifyObservers":
        greaterOrEqual(
          s.took,
          0,
          "Should report time taken to notify observers"
        );
        ok(!("counts" in s), "Should not report counts for observers");
        break;

      case "fetchLocalChangeRecords":
        greaterOrEqual(
          s.took,
          0,
          "Should report time taken to fetch records for upload"
        );
        deepEqual(
          s.counts,
          [{ name: "items", count: 4 }],
          "Should report number of records to upload"
        );
        break;

      case "finalize":
        ok(!("took" in s), "Should not report time taken to finalize");
        ok(!("counts" in s), "Should not report counts for finalizing");
    }
    names.push(s.step);
  }
  deepEqual(
    names,
    [
      "fetchLocalTree",
      "fetchRemoteTree",
      "merge",
      "apply",
      "notifyObservers",
      "fetchLocalChangeRecords",
      "finalize",
    ],
    "Should report merge progress after waiting on blocker"
  );
  ok(
    buf.finalizeController.signal.aborted,
    "Should abort finalize signal on shutdown"
  );

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});