OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 print('Checks that async chains for promises are correct.'); |
| 6 |
| 7 InspectorTest.addScript(` |
| 8 function foo1() { |
| 9 debugger; |
| 10 } |
| 11 |
| 12 function foo2() { |
| 13 debugger; |
| 14 } |
| 15 |
| 16 function promise() { |
| 17 var resolve; |
| 18 var p1 = new Promise(r => resolve = r); |
| 19 var p2 = p1.then(foo1); |
| 20 resolve(); |
| 21 return p2; |
| 22 } |
| 23 |
| 24 function promiseResolvedBySetTimeout() { |
| 25 var resolve; |
| 26 var p1 = new Promise(r => resolve = r); |
| 27 var p2 = p1.then(foo1); |
| 28 setTimeout(resolve, 0); |
| 29 return p2; |
| 30 } |
| 31 |
| 32 function promiseAll() { |
| 33 var resolve1; |
| 34 var resolve2; |
| 35 var p1 = new Promise(resolve => resolve1 = resolve); |
| 36 var p2 = new Promise(resolve => resolve2 = resolve); |
| 37 var p3 = Promise.all([ p1, p2 ]).then(foo1); |
| 38 resolve1(); |
| 39 resolve2(); |
| 40 return p3; |
| 41 } |
| 42 |
| 43 function promiseAllReverseOrder() { |
| 44 var resolve1; |
| 45 var resolve2; |
| 46 var p1 = new Promise(resolve => resolve1 = resolve); |
| 47 var p2 = new Promise(resolve => resolve2 = resolve); |
| 48 var p3 = Promise.all([ p1, p2 ]).then(foo1); |
| 49 resolve2(); |
| 50 resolve1(); |
| 51 return p3; |
| 52 } |
| 53 |
| 54 function promiseRace() { |
| 55 var resolve1; |
| 56 var resolve2; |
| 57 var p1 = new Promise(resolve => resolve1 = resolve); |
| 58 var p2 = new Promise(resolve => resolve2 = resolve); |
| 59 var p3 = Promise.race([ p1, p2 ]).then(foo1); |
| 60 resolve1(); |
| 61 resolve2(); |
| 62 return p3; |
| 63 } |
| 64 |
| 65 function twoChainedCallbacks() { |
| 66 var resolve1; |
| 67 var p1 = new Promise(resolve => resolve1 = resolve); |
| 68 var p2 = p1.then(foo1).then(foo2); |
| 69 resolve1(); |
| 70 return p2; |
| 71 } |
| 72 |
| 73 function promiseResolve() { |
| 74 return Promise.resolve().then(foo1).then(foo2); |
| 75 } |
| 76 |
| 77 function thenableJobResolvedInSetTimeout() { |
| 78 function thenableJob() { |
| 79 var resolve2; |
| 80 var p2 = new Promise(resolve => resolve2 = resolve); |
| 81 setTimeout(resolve2, 0); |
| 82 return p2; |
| 83 } |
| 84 var resolve1; |
| 85 var p1 = new Promise(resolve => resolve1 = resolve); |
| 86 var p3 = p1.then(() => thenableJob()).then(foo1); |
| 87 resolve1(); |
| 88 return p3; |
| 89 } |
| 90 |
| 91 function thenableJobResolvedInSetTimeoutWithStack() { |
| 92 function thenableJob() { |
| 93 function inner() { |
| 94 resolve2(); |
| 95 } |
| 96 |
| 97 var resolve2; |
| 98 var p2 = new Promise(resolve => resolve2 = resolve); |
| 99 setTimeout(inner, 0); |
| 100 return p2; |
| 101 } |
| 102 var resolve1; |
| 103 var p1 = new Promise(resolve => resolve1 = resolve); |
| 104 var p3 = p1.then(() => thenableJob()).then(foo1); |
| 105 resolve1(); |
| 106 return p3; |
| 107 } |
| 108 |
| 109 function thenableJobResolvedByPromise() { |
| 110 function thenableJob() { |
| 111 var resolve2; |
| 112 var p2 = new Promise(resolve => resolve2 = resolve); |
| 113 Promise.resolve().then(resolve2); |
| 114 return p2; |
| 115 } |
| 116 var resolve1; |
| 117 var p1 = new Promise(resolve => resolve1 = resolve); |
| 118 var p3 = p1.then(() => thenableJob()).then(foo1); |
| 119 resolve1(); |
| 120 return p3; |
| 121 } |
| 122 |
| 123 function thenableJobResolvedByPromiseWithStack() { |
| 124 function thenableJob() { |
| 125 function inner() { |
| 126 resolve2(); |
| 127 } |
| 128 |
| 129 var resolve2; |
| 130 var p2 = new Promise(resolve => resolve2 = resolve); |
| 131 Promise.resolve().then(inner); |
| 132 return p2; |
| 133 } |
| 134 var resolve1; |
| 135 var p1 = new Promise(resolve => resolve1 = resolve); |
| 136 var p3 = p1.then(() => thenableJob()).then(foo1); |
| 137 resolve1(); |
| 138 return p3; |
| 139 } |
| 140 |
| 141 function lateThenCallback() { |
| 142 var resolve1; |
| 143 var p1 = new Promise(resolve => resolve1 = resolve); |
| 144 resolve1(); |
| 145 return p1.then(foo1); |
| 146 } |
| 147 |
| 148 function complex() { |
| 149 var testResolve; |
| 150 var testPromise = new Promise(resolve => testResolve = resolve); |
| 151 |
| 152 function foo1() { |
| 153 function inner1() { |
| 154 debugger; |
| 155 } |
| 156 inner1(); |
| 157 } |
| 158 |
| 159 function foo2() { |
| 160 var resolve20; |
| 161 function inner2() { |
| 162 resolve20(); |
| 163 } |
| 164 var p20 = new Promise(resolve => resolve20 = resolve); |
| 165 Promise.resolve().then(inner2); |
| 166 return p20; |
| 167 } |
| 168 |
| 169 function foo3() { |
| 170 var resolve17; |
| 171 function inner3() { |
| 172 resolve17(); |
| 173 } |
| 174 var p17 = new Promise(resolve => resolve17 = resolve); |
| 175 setTimeout(inner3, 0); |
| 176 return p17; |
| 177 } |
| 178 |
| 179 function foo4() { |
| 180 function inner4() { |
| 181 return; |
| 182 } |
| 183 return inner4(); |
| 184 } |
| 185 |
| 186 function foo5() { |
| 187 return Promise.all([ Promise.resolve(), Promise.resolve() ]) |
| 188 .then(() => 42); |
| 189 } |
| 190 |
| 191 function foo6() { |
| 192 return Promise.race([ Promise.resolve(), Promise.resolve()]) |
| 193 .then(() => 42); |
| 194 } |
| 195 |
| 196 var p = Promise.resolve() |
| 197 .then(foo6) |
| 198 .then(foo5) |
| 199 .then(foo4) |
| 200 .then(foo3) |
| 201 .then(foo2) |
| 202 .then(foo1); |
| 203 |
| 204 setTimeout(() => { |
| 205 p.then(() => { |
| 206 p.then(() => { |
| 207 debugger; |
| 208 testResolve(); |
| 209 }) |
| 210 }) |
| 211 }, 0) |
| 212 |
| 213 return testPromise; |
| 214 } |
| 215 |
| 216 function reject() { |
| 217 return Promise.reject().catch(foo1); |
| 218 } |
| 219 |
| 220 //# sourceURL=test.js`, 7, 26); |
| 221 |
| 222 InspectorTest.setupScriptMap(); |
| 223 Protocol.Debugger.onPaused(message => { |
| 224 InspectorTest.logCallFrames(message.params.callFrames); |
| 225 var asyncStackTrace = message.params.asyncStackTrace; |
| 226 while (asyncStackTrace) { |
| 227 InspectorTest.log(`-- ${asyncStackTrace.description} --`); |
| 228 InspectorTest.logCallFrames(asyncStackTrace.callFrames); |
| 229 asyncStackTrace = asyncStackTrace.parent; |
| 230 } |
| 231 InspectorTest.log(''); |
| 232 Protocol.Debugger.resume(); |
| 233 }); |
| 234 |
| 235 Protocol.Debugger.enable(); |
| 236 Protocol.Debugger.setAsyncCallStackDepth({ maxDepth: 128 }); |
| 237 var testList = [ |
| 238 'promise', |
| 239 'promiseResolvedBySetTimeout', |
| 240 'promiseAll', |
| 241 'promiseAllReverseOrder', |
| 242 'promiseRace', |
| 243 'twoChainedCallbacks', |
| 244 'promiseResolve', |
| 245 'thenableJobResolvedInSetTimeout', |
| 246 'thenableJobResolvedInSetTimeoutWithStack', |
| 247 'thenableJobResolvedByPromise', |
| 248 'thenableJobResolvedByPromiseWithStack', |
| 249 'lateThenCallback', |
| 250 'complex', |
| 251 'reject', |
| 252 ] |
| 253 InspectorTest.runTestSuite(testList.map(name => { |
| 254 return eval(` |
| 255 (function test${capitalize(name)}(next) { |
| 256 Protocol.Runtime.evaluate({ expression: \`${name}() |
| 257 //# sourceURL=test${capitalize(name)}.js\`, awaitPromise: true}) |
| 258 .then(next); |
| 259 }) |
| 260 `); |
| 261 })); |
| 262 |
| 263 function capitalize(string) { |
| 264 return string.charAt(0).toUpperCase() + string.slice(1); |
| 265 } |
OLD | NEW |