summaryrefslogtreecommitdiffstats
path: root/devtools/shared/worker/tests/browser/browser_worker-01.js
blob: 7679e41663037b65359391b8330c53d9f8914d1e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that the devtools/shared/worker communicates properly
// as both CommonJS module and as a JSM.

const WORKER_URL =
  "resource://devtools/client/shared/widgets/GraphsWorker.js";

const count = 100000;
const WORKER_DATA = (function () {
  let timestamps = [];
  for (let i = 0; i < count; i++) {
    timestamps.push(i);
  }
  return timestamps;
})();
const INTERVAL = 100;
const DURATION = 1000;

add_task(function* () {
  // Test both CJS and JSM versions

  yield testWorker("JSM", () => Cu.import("resource://devtools/shared/worker/worker.js", {}));
  yield testWorker("CommonJS", () => require("devtools/shared/worker/worker"));
});

function* testWorker(context, workerFactory) {
  let { DevToolsWorker, workerify } = workerFactory();
  let worker = new DevToolsWorker(WORKER_URL);
  let results = yield worker.performTask("plotTimestampsGraph", {
    timestamps: WORKER_DATA,
    interval: INTERVAL,
    duration: DURATION
  });

  ok(results.plottedData.length,
    `worker should have returned an object with array properties in ${context}`);

  let fn = workerify(function (x) { return x * x; });
  is((yield fn(5)), 25, `workerify works in ${context}`);
  fn.destroy();

  worker.destroy();
}