summaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/debugger/async-stacks-limit.js
blob: 62206750df58875112fc1c84587423002077f533 (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
// Copyright 2016 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.

InspectorTest.log('Checks that async stacks works good with different limits');

InspectorTest.addScript(`
var resolveTest;

function foo1() {
  debugger;
}

function foo2() {
  debugger;
  if (resolveTest) resolveTest();
}

function promise() {
  var resolve1;
  var p1 = new Promise(resolve => resolve1 = resolve);
  var p2 = p1.then(foo1);
  resolve1();
  return p2;
}

function twoPromises() {
  var resolve1;
  var resolve2;
  var p1 = new Promise(resolve => resolve1 = resolve);
  var p2 = new Promise(resolve => resolve2 = resolve);
  var p3 = p1.then(foo1);
  var p4 = p2.then(foo2);
  resolve1();
  resolve2();
  return Promise.all([p3, p4]);
}

function twoSetTimeout() {
  setTimeout(foo1, 0);
  setTimeout(foo2, 0);
  return new Promise(resolve => resolveTest = resolve);
}

function twentySetTimeout() {
  var resolve1;
  var p1 = new Promise(resolve => resolve1 = resolve);
  for (var i = 1; i <= 19; ++i)
    setTimeout('(function foo' + i + '(){debugger;})()',0);
  setTimeout(resolve1, 0);
  return p1;
}

//# sourceURL=test.js`, 7, 26);

InspectorTest.setupScriptMap();
Protocol.Debugger.onPaused(message => {
  InspectorTest.logCallFrames(message.params.callFrames);
  var asyncStackTrace = message.params.asyncStackTrace;
  while (asyncStackTrace) {
    InspectorTest.log(`-- ${asyncStackTrace.description} --`);
    InspectorTest.logCallFrames(asyncStackTrace.callFrames);
    asyncStackTrace = asyncStackTrace.parent;
  }
  InspectorTest.log('');
  Protocol.Debugger.resume();
});

Protocol.Debugger.enable();
Protocol.Debugger.setAsyncCallStackDepth({ maxDepth: 128 });
InspectorTest.runTestSuite([
  function testZeroLimit(next) {
    Protocol.Runtime.evaluate({
        expression: 'setMaxAsyncTaskStacks(0)//# sourceURL=expr.js'})
      .then(() => Protocol.Runtime.evaluate({
        expression: 'promise()//# sourceURL=expr.js', awaitPromise: true
      }))
      .then(() => cancelAllAsyncTasks())
      .then(next);
  },

  function testTwoLimit(next) {
    // we need one stack for parent task and one for next task.
    Protocol.Runtime
        .evaluate({expression: 'setMaxAsyncTaskStacks(2)//# sourceURL=expr.js'})
        .then(() => Protocol.Runtime.evaluate({
          expression: 'promise()//# sourceURL=expr.js',
          awaitPromise: true
        }))
        .then(() => cancelAllAsyncTasks())
        .then(next);
  },

  function testOneLimitTwoPromises(next) {
    // Should be no async stacks because when first microtask is finished
    // it will resolve and schedule p3 - will remove async stack for scheduled
    // p2.
    Protocol.Runtime.evaluate({
        expression: 'setMaxAsyncTaskStacks(1)//# sourceURL=expr.js'})
      .then(() => Protocol.Runtime.evaluate({
        expression: 'twoPromises()//# sourceURL=expr.js', awaitPromise: true
      }))
      .then(() => cancelAllAsyncTasks())
      .then(next);
  },

  function testFourLimitTwoPromises(next) {
    Protocol.Runtime
        .evaluate({expression: 'setMaxAsyncTaskStacks(4)//# sourceURL=expr.js'})
        .then(() => Protocol.Runtime.evaluate({
          expression: 'twoPromises()//# sourceURL=expr.js',
          awaitPromise: true
        }))
        .then(() => cancelAllAsyncTasks())
        .then(next);
  },

  function testSixLimitTwoPromises(next) {
    Protocol.Runtime
        .evaluate({expression: 'setMaxAsyncTaskStacks(6)//# sourceURL=expr.js'})
        .then(() => Protocol.Runtime.evaluate({
          expression: 'twoPromises()//# sourceURL=expr.js',
          awaitPromise: true
        }))
        .then(() => cancelAllAsyncTasks())
        .then(next);
  },

  function testTwoLimitTwoSetTimeouts(next) {
    Protocol.Runtime.evaluate({
        expression: 'setMaxAsyncTaskStacks(2)//# sourceURL=expr.js'})
      .then(() => Protocol.Runtime.evaluate({
        expression: 'twoSetTimeout()//# sourceURL=expr.js', awaitPromise: true
      }))
      .then(() => cancelAllAsyncTasks())
      .then(next);
  },

  function testThreeLimitTwoSetTimeouts(next) {
    Protocol.Runtime.evaluate({
        expression: 'setMaxAsyncTaskStacks(3)//# sourceURL=expr.js'})
      .then(() => Protocol.Runtime.evaluate({
        expression: 'twoSetTimeout()//# sourceURL=expr.js', awaitPromise: true
      }))
      .then(() => cancelAllAsyncTasks())
      .then(next);
  },

  function testTenLimitTwentySetTimeouts(next) {
    Protocol.Runtime.evaluate({
        expression: 'setMaxAsyncTaskStacks(10)//# sourceURL=expr.js'})
      .then(() => Protocol.Runtime.evaluate({
        expression: 'twentySetTimeout()//# sourceURL=expr.js',
        awaitPromise: true
      }))
      .then(() => cancelAllAsyncTasks())
      .then(next);
  }
]);

function cancelAllAsyncTasks() {
  return Protocol.Debugger.setAsyncCallStackDepth({ maxDepth: 0 })
    .then(() => Protocol.Debugger.setAsyncCallStackDepth({ maxDepth: 128 }));
}