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

/**
 * Test the eventLoopLag actor.
 */

"use strict";

function run_test()
{
  let {EventLoopLagFront} = require("devtools/shared/fronts/eventlooplag");

  DebuggerServer.init();
  DebuggerServer.addBrowserActors();

  // As seen in EventTracer.cpp
  let threshold = 20;
  let interval = 10;


  let front;
  let client = new DebuggerClient(DebuggerServer.connectPipe());

  // Start tracking event loop lags.
  client.connect().then(function () {
    client.listTabs(function (resp) {
      front = new EventLoopLagFront(client, resp);
      front.start().then(success => {
        do_check_true(success);
        front.once("event-loop-lag", gotLagEvent);
        do_execute_soon(lag);
      });
    });
  });

  // Force a lag
  function lag() {
    let start = new Date();
    let duration = threshold + interval + 1;
    while (true) {
      if (((new Date()) - start) > duration) {
        break;
      }
    }
  }

  // Got a lag event. The test will time out if the actor
  // fails to detect the lag.
  function gotLagEvent(time) {
    do_print("lag: " + time);
    do_check_true(time >= threshold);
    front.stop().then(() => {
      finishClient(client);
    });
  }

  do_test_pending();
}