summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/unit/test_profiler_events-02.js
blob: fed702043cfc50605dd3a92bdce26df304d516ef (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests the event notification service for the profiler actor.
 */

const Profiler = Cc["@mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler);
const MAX_PROFILER_ENTRIES = 10000000;
const { ProfilerFront } = require("devtools/shared/fronts/profiler");
const { waitForTime } = DevToolsUtils;

function run_test() {
  run_next_test();
}

add_task(function* () {
  let [client, form] = yield getChromeActors();
  let front = new ProfilerFront(client, form);

  // Ensure the profiler is not running when the test starts (it could
  // happen if the MOZ_PROFILER_STARTUP environment variable is set).
  Profiler.StopProfiler();
  let eventsCalled = 0;
  let handledThreeTimes = promise.defer();

  front.on("profiler-status", (response) => {
    dump("'profiler-status' fired\n");
    do_check_true(typeof response.position === "number");
    do_check_true(typeof response.totalSize === "number");
    do_check_true(typeof response.generation === "number");
    do_check_true(response.position > 0 && response.position < response.totalSize);
    do_check_true(response.totalSize === MAX_PROFILER_ENTRIES);
    // There's no way we'll fill the buffer in this test.
    do_check_true(response.generation === 0);

    eventsCalled++;
    if (eventsCalled > 2) {
      handledThreeTimes.resolve();
    }
  });

  yield front.setProfilerStatusInterval(1);
  dump("Set the profiler-status event interval to 1\n");
  yield front.startProfiler();
  yield waitForTime(500);
  yield front.stopProfiler();

  do_check_true(eventsCalled === 0, "No 'profiler-status' events should be fired before registering.");

  let ret = yield front.registerEventNotifications({ events: ["profiler-status"] });
  do_check_true(ret.registered.length === 1);

  yield front.startProfiler();
  yield handledThreeTimes.promise;
  yield front.stopProfiler();
  do_check_true(eventsCalled >= 3, "profiler-status fired atleast three times while recording");

  let totalEvents = eventsCalled;
  yield waitForTime(50);
  do_check_true(totalEvents === eventsCalled, "No more profiler-status events after recording.");
});

function getChromeActors() {
  let deferred = promise.defer();
  get_chrome_actors((client, form) => deferred.resolve([client, form]));
  return deferred.promise;
}