summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/browser_worker_use_counters.js
blob: d651da31d8e238c822ea866d0d100664d3295464 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const gHttpTestRoot = "https://example.com/browser/dom/workers/test/";

function unscream(s) {
  // Takes SCREAMINGCASE `s` and returns "Screamingcase".
  return s.charAt(0) + s.slice(1).toLowerCase();
}

function screamToCamel(s) {
  // Takes SCREAMING_CASE `s` and returns "screamingCase".
  const pascal = s.split("_").map(unscream).join("");
  return pascal.charAt(0).toLowerCase() + pascal.slice(1);
}

var check_use_counter_worker = async function (
  use_counter_name,
  worker_type,
  content_task
) {
  info(`checking ${use_counter_name} use counters for ${worker_type} worker`);

  let newTab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  gBrowser.selectedTab = newTab;
  newTab.linkedBrowser.stop();

  // Hold on to the current values of the instrumentation we're
  // interested in.
  await Services.fog.testFlushAllChildren();
  let glean_before =
    Glean[`useCounterWorker${unscream(worker_type)}`][
      screamToCamel(use_counter_name)
    ].testGetValue();
  let glean_destructions_before =
    Glean.useCounter[
      `${worker_type.toLowerCase()}WorkersDestroyed`
    ].testGetValue();

  BrowserTestUtils.startLoadingURIString(
    gBrowser.selectedBrowser,
    gHttpTestRoot + "file_use_counter_worker.html"
  );
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  await content_task(gBrowser.selectedBrowser);

  // Tear down the page.
  let tabClosed = BrowserTestUtils.waitForTabClosing(newTab);
  gBrowser.removeTab(newTab);
  await tabClosed;

  // Grab data again and compare.
  // We'd like for this to be synchronous, but use counters are reported on
  // worker destruction which we don't directly observe.
  // So we check in a quick loop.
  await BrowserTestUtils.waitForCondition(async () => {
    await Services.fog.testFlushAllChildren();
    return (
      glean_before !=
      Glean[`useCounterWorker${unscream(worker_type)}`][
        screamToCamel(use_counter_name)
      ].testGetValue()
    );
  });
  let glean_after =
    Glean[`useCounterWorker${unscream(worker_type)}`][
      screamToCamel(use_counter_name)
    ].testGetValue();
  let glean_destructions_after =
    Glean.useCounter[
      `${worker_type.toLowerCase()}WorkersDestroyed`
    ].testGetValue();

  is(
    glean_after,
    glean_before + 1,
    `Glean counter ${use_counter_name} for ${worker_type} worker is correct.`
  );
  // There might be other workers created by prior tests get destroyed during
  // this tests.
  Assert.greater(
    glean_destructions_after,
    glean_destructions_before ?? 0,
    `Glean ${worker_type} worker counts are correct`
  );
};

add_task(async function test_dedicated_worker() {
  await check_use_counter_worker("CONSOLE_LOG", "DEDICATED", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      return new Promise(resolve => {
        let worker = new content.Worker("file_use_counter_worker.js");
        worker.onmessage = function (e) {
          if (e.data === "DONE") {
            worker.terminate();
            resolve();
          }
        };
      });
    });
  });
});

add_task(async function test_shared_worker() {
  await check_use_counter_worker("CONSOLE_LOG", "SHARED", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      return new Promise(resolve => {
        let worker = new content.SharedWorker(
          "file_use_counter_shared_worker.js"
        );
        worker.port.onmessage = function (e) {
          if (e.data === "DONE") {
            resolve();
          }
        };
        worker.port.postMessage("RUN");
      });
    });
  });
});

add_task(async function test_shared_worker_microtask() {
  await check_use_counter_worker("CONSOLE_LOG", "SHARED", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      return new Promise(resolve => {
        let worker = new content.SharedWorker(
          "file_use_counter_shared_worker_microtask.js"
        );
        worker.port.onmessage = function (e) {
          if (e.data === "DONE") {
            resolve();
          }
        };
        worker.port.postMessage("RUN");
      });
    });
  });
});

add_task(async function test_service_worker() {
  await check_use_counter_worker("CONSOLE_LOG", "SERVICE", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      let waitForActivated = async function (registration) {
        return new Promise(resolve => {
          let worker =
            registration.installing ||
            registration.waiting ||
            registration.active;
          if (worker.state === "activated") {
            resolve(worker);
            return;
          }

          worker.addEventListener("statechange", function onStateChange() {
            if (worker.state === "activated") {
              worker.removeEventListener("statechange", onStateChange);
              resolve(worker);
            }
          });
        });
      };

      return new Promise(resolve => {
        content.navigator.serviceWorker
          .register("file_use_counter_service_worker.js")
          .then(async registration => {
            content.navigator.serviceWorker.onmessage = function (e) {
              if (e.data === "DONE") {
                registration.unregister().then(resolve);
              }
            };
            let worker = await waitForActivated(registration);
            worker.postMessage("RUN");
          });
      });
    });
  });
});