set-async-call-stack-depth.js 2.69 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
let {session, contextGroup, Protocol} = InspectorTest.start('Checks that we report not more then maxDepth call chains.');
6

7
contextGroup.addScript(`
8 9 10
function asyncChain(breakAtEnd) {
  function asyncOpNested() {
    setTimeout(asyncOpNested1, 0);
11
  }
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  function asyncOpNested1() {
    setTimeout(asyncOpNested2, 0);
  }
  function asyncOpNested2() {
    setTimeout(asyncOpNested3, 0);
  }
  function asyncOpNested3() {
    setTimeout(asyncOpNested4, 0);
  }
  function asyncOpNested4() {
    if (breakAtEnd) {
      debugger;
    } else {
      console.trace(42);
    }
  }
  asyncOpNested();
29 30 31 32 33 34
}
`);

Protocol.Debugger.enable();
InspectorTest.runAsyncTestSuite([
  async function testPaused() {
35 36
    const breakAtEnd = true;
    startTest({ limit: 8, breakAtEnd });
37 38 39
    dumpCaptured((await Protocol.Debugger.oncePaused()).params.asyncStackTrace);
    await Protocol.Debugger.resume();

40
    startTest({ limit: 4, breakAtEnd });
41 42 43
    dumpCaptured((await Protocol.Debugger.oncePaused()).params.asyncStackTrace);
    await Protocol.Debugger.resume();

44
    startTest({ limit: 3, breakAtEnd });
45 46 47
    dumpCaptured((await Protocol.Debugger.oncePaused()).params.asyncStackTrace);
    await Protocol.Debugger.resume();

48
    startTest({ limit: 0, breakAtEnd });
49 50 51 52 53 54
    dumpCaptured((await Protocol.Debugger.oncePaused()).params.asyncStackTrace);
    await Protocol.Debugger.resume();
  },

  async function testConsoleTrace() {
    await Protocol.Runtime.enable();
55 56
    const breakAtEnd = false;
    startTest({ limit: 8, breakAtEnd});
57 58 59
    let msg = await Protocol.Runtime.onceConsoleAPICalled();
    dumpCaptured(msg.params.stackTrace.parent);

60
    startTest({ limit: 4, breakAtEnd});
61 62 63
    msg = await Protocol.Runtime.onceConsoleAPICalled();
    dumpCaptured(msg.params.stackTrace.parent);

64
    startTest({ limit: 3, breakAtEnd});
65 66 67
    msg = await Protocol.Runtime.onceConsoleAPICalled();
    dumpCaptured(msg.params.stackTrace.parent);

68
    startTest({ limit: 0, breakAtEnd});
69 70 71 72 73 74 75 76
    msg = await Protocol.Runtime.onceConsoleAPICalled();
    dumpCaptured(msg.params.stackTrace.parent);

    await Protocol.Runtime.disable();
  }
]);

function startTest(params) {
77 78
  InspectorTest.log('Actual call chain length: 4');
  InspectorTest.log(`setAsyncCallStackDepth(maxDepth): ${params.limit}`);
79 80 81

  Protocol.Debugger.setAsyncCallStackDepth({maxDepth: params.limit});
  Protocol.Runtime.evaluate({expression:
82
      `asyncChain(${params.breakAtEnd})`});
83 84 85 86 87 88 89 90 91 92
}

function dumpCaptured(stack) {
  let count = 0;
  while (stack) {
    ++count;
    stack = stack.parent;
  }
  InspectorTest.log('reported: ' + count + '\n');
}