summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_frecency_recalculator.js
blob: 414882eaf5bf3aa681f653032e1b6e274bf015c0 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test PlacesFrecencyRecalculator scheduling.

// Enable the collection (during test) for all products so even products
// that don't collect the data will be able to run the test without failure.
Services.prefs.setBoolPref(
  "toolkit.telemetry.testing.overrideProductsCheck",
  true
);

async function getOriginFrecency(origin) {
  let db = await PlacesUtils.promiseDBConnection();
  return (
    await db.execute(
      `SELECT frecency
       FROM moz_origins
       WHERE host = :origin`,
      { origin }
    )
  )[0].getResultByIndex(0);
}

async function resetOriginFrecency(origin) {
  await PlacesUtils.withConnectionWrapper(
    "test_frecency_recalculator reset origin",
    async db => {
      await db.executeCached(
        `UPDATE moz_origins
         SET frecency = -1
         WHERE host = :origin`,
        { origin }
      );
    }
  );
}

async function addVisitsAndSetRecalc(urls) {
  await PlacesTestUtils.addVisits(urls);
  await PlacesUtils.withConnectionWrapper(
    "test_frecency_recalculator set recalc",
    async db => {
      await db.executeCached(
        `UPDATE moz_places
         SET frecency = -1
         WHERE url in (
           ${PlacesUtils.sqlBindPlaceholders(urls)}
         )`,
        urls
      );
      await db.executeCached(
        `UPDATE moz_places
         SET recalc_frecency = (CASE WHEN url in (
               ${PlacesUtils.sqlBindPlaceholders(urls)}
             ) THEN 1 ELSE 0 END)`,
        urls
      );
    }
  );
}

add_task(async function test() {
  info("On startup a recalculation is always pending.");
  Assert.ok(
    PlacesFrecencyRecalculator.isRecalculationPending,
    "Recalculation should be pending"
  );
  // If everything gets recalculated, then it should not be pending anymore.
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  Assert.ok(
    !PlacesFrecencyRecalculator.isRecalculationPending,
    "Recalculation should not be pending"
  );

  // If after a recalculation there's outdated entries left, a new recalculation
  // should be pending.
  info("Insert outdated frecencies");
  const url1 = new URL("https://test1.moz.org/");
  const url2 = new URL("https://test2.moz.org/");
  await addVisitsAndSetRecalc([url1.href, url2.href]);
  await resetOriginFrecency(url1.host);
  await resetOriginFrecency(url2.host);

  Assert.ok(
    PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should have set shouldStartFrecencyRecalculation"
  );
  await PlacesFrecencyRecalculator.recalculateSomeFrecencies({ chunkSize: 1 });
  Assert.ok(
    PlacesFrecencyRecalculator.isRecalculationPending,
    "Recalculation should be pending"
  );
  // Recalculating uri1 will set its origin to recalc, that means there's 2
  // origins to recalc now. Passing chunkSize: 2 here would then retrigger the
  // recalc, thinking we saturated the chunk, thus we use 3.
  await PlacesFrecencyRecalculator.recalculateSomeFrecencies({ chunkSize: 3 });
  Assert.ok(
    !PlacesFrecencyRecalculator.isRecalculationPending,
    "Recalculation should not be pending"
  );
  Assert.ok(
    !PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should have unset shouldStartFrecencyRecalculation"
  );

  Assert.greater(await getOriginFrecency(url1.host), 0);
  Assert.greater(await getOriginFrecency(url2.host), 0);

  info("Changing recalc_frecency of an entry adds a pending recalculation.");
  PlacesUtils.history.shouldStartFrecencyRecalculation = false;
  let promiseNotify = TestUtils.topicObserved("frecency-recalculation-needed");
  await PlacesUtils.withConnectionWrapper(
    "test_frecency_recalculator",
    async db => {
      await db.executeCached(
        `UPDATE moz_places SET recalc_frecency = 1 WHERE url = :url`,
        { url: url1.href }
      );
    }
  );
  Assert.ok(
    PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should have set shouldStartFrecencyRecalculation"
  );

  await promiseNotify;
  Assert.ok(
    PlacesFrecencyRecalculator.isRecalculationPending,
    "Recalculation should be pending"
  );
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  Assert.ok(
    !PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should have unset shouldStartFrecencyRecalculation"
  );
});

add_task(async function test_chunk_time_telemetry() {
  await PlacesUtils.bookmarks.insert({
    url: "https://test-bookmark.com",
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
  });
  Assert.ok(
    PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should have set shouldStartFrecencyRecalculation"
  );
  let histogram = TelemetryTestUtils.getAndClearHistogram(
    "PLACES_FRECENCY_RECALC_CHUNK_TIME_MS"
  );
  let subject = {};
  PlacesFrecencyRecalculator.observe(subject, "test-execute-taskFn", "");
  await subject.promise;
  let snapshot = histogram.snapshot();
  Assert.equal(
    Object.values(snapshot.values).reduce((a, b) => a + b, 0),
    1
  );
  Assert.greater(snapshot.sum, 0);
  Assert.ok(
    !PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should have unset shouldStartFrecencyRecalculation"
  );

  // It should now not report any new time, since there's nothing to recalculate.
  histogram.clear();
  PlacesFrecencyRecalculator.observe(subject, "test-execute-taskFn", "");
  await subject.promise;
  snapshot = histogram.snapshot();
  Assert.equal(
    Object.values(snapshot.values).reduce((a, b) => a + b, 0),
    0
  );
  Assert.ok(
    !PlacesUtils.history.shouldStartFrecencyRecalculation,
    "Should still not have set shouldStartFrecencyRecalculation"
  );
});

add_task(async function test_acceleration() {
  await PlacesTestUtils.addVisits(
    new Array(300).fill("https://www.mozilla.org/")
  );

  Assert.ok(
    PlacesFrecencyRecalculator.maybeUpdateRecalculationSpeed(),
    "Recalculation accelerated"
  );
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  Assert.ok(
    !PlacesFrecencyRecalculator.maybeUpdateRecalculationSpeed(),
    "Recalculation back to normal rate"
  );
});