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 // Flags: --expose_gc |
| 5 |
| 6 print("Tests that Runtime.awaitPromise works."); |
| 7 |
| 8 InspectorTest.evaluateInPage( |
| 9 ` |
| 10 var resolveCallback; |
| 11 var rejectCallback; |
| 12 function createPromise() |
| 13 { |
| 14 return new Promise((resolve, reject) => { resolveCallback = resolve; rejectC
allback = reject }); |
| 15 } |
| 16 |
| 17 function resolvePromise() |
| 18 { |
| 19 resolveCallback(239); |
| 20 resolveCallback = undefined; |
| 21 rejectCallback = undefined; |
| 22 } |
| 23 |
| 24 function rejectPromise() |
| 25 { |
| 26 rejectCallback(239); |
| 27 resolveCallback = undefined; |
| 28 rejectCallback = undefined; |
| 29 } |
| 30 |
| 31 //# sourceURL=test.js`); |
| 32 |
| 33 InspectorTest.sendCommandPromise("Debugger.enable", {}) |
| 34 .then(() => InspectorTest.sendCommandPromise("Debugger.setAsyncCallStackDept
h", { maxDepth: 128 })) |
| 35 .then(() => testSuite()); |
| 36 |
| 37 function dumpResult(result) |
| 38 { |
| 39 if (result.exceptionDetails) { |
| 40 if (result.exceptionDetails.stackTrace && result.exceptionDetails.stackTrace
.parent) { |
| 41 for (var frame of result.exceptionDetails.stackTrace.parent.callFrames) { |
| 42 frame.scriptId = 0; |
| 43 if (!frame.url) |
| 44 frame.url = "(empty)"; |
| 45 if (!frame.functionName) |
| 46 frame.functionName = "(anonymous)"; |
| 47 } |
| 48 } |
| 49 result.exceptionDetails.exceptionId = 0; |
| 50 if (result.exceptionDetails.exception) |
| 51 result.exceptionDetails.exception.objectId = 0; |
| 52 } |
| 53 InspectorTest.logObject(result); |
| 54 } |
| 55 |
| 56 function testSuite() |
| 57 { |
| 58 InspectorTest.runTestSuite([ |
| 59 function testResolvedPromise(next) |
| 60 { |
| 61 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "Promis
e.resolve(239)"}) |
| 62 .then((result) => InspectorTest.sendCommandPromise("Runtime.awaitPromise
", { promiseObjectId: result.result.result.objectId, returnByValue: false, gener
atePreview: true })) |
| 63 .then((result) => dumpResult(result.result)) |
| 64 .then(() => next()); |
| 65 }, |
| 66 |
| 67 function testRejectedPromise(next) |
| 68 { |
| 69 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "Promis
e.reject({ a : 1 })"}) |
| 70 .then((result) => InspectorTest.sendCommandPromise("Runtime.awaitPromise
", { promiseObjectId: result.result.result.objectId, returnByValue: true, genera
tePreview: false })) |
| 71 .then((result) => dumpResult(result.result)) |
| 72 .then(() => next()); |
| 73 }, |
| 74 |
| 75 function testRejectedPromiseWithStack(next) |
| 76 { |
| 77 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "create
Promise()"}) |
| 78 .then((result) => scheduleRejectAndAwaitPromise(result)) |
| 79 .then((result) => dumpResult(result.result)) |
| 80 .then(() => next()); |
| 81 |
| 82 function scheduleRejectAndAwaitPromise(result) |
| 83 { |
| 84 var promise = InspectorTest.sendCommandPromise("Runtime.awaitPromise", {
promiseObjectId: result.result.result.objectId }); |
| 85 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "reje
ctPromise()" }); |
| 86 return promise; |
| 87 } |
| 88 }, |
| 89 |
| 90 function testPendingPromise(next) |
| 91 { |
| 92 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "create
Promise()"}) |
| 93 .then((result) => scheduleFulfillAndAwaitPromise(result)) |
| 94 .then((result) => dumpResult(result.result)) |
| 95 .then(() => next()); |
| 96 |
| 97 function scheduleFulfillAndAwaitPromise(result) |
| 98 { |
| 99 var promise = InspectorTest.sendCommandPromise("Runtime.awaitPromise", {
promiseObjectId: result.result.result.objectId }); |
| 100 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "reso
lvePromise()" }); |
| 101 return promise; |
| 102 } |
| 103 }, |
| 104 |
| 105 function testResolvedWithoutArgsPromise(next) |
| 106 { |
| 107 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "Promis
e.resolve()"}) |
| 108 .then((result) => InspectorTest.sendCommandPromise("Runtime.awaitPromise
", { promiseObjectId: result.result.result.objectId, returnByValue: true, genera
tePreview: false })) |
| 109 .then((result) => dumpResult(result.result)) |
| 110 .then(() => next()); |
| 111 }, |
| 112 |
| 113 function testGarbageCollectedPromise(next) |
| 114 { |
| 115 InspectorTest.sendCommandPromise("Runtime.evaluate", { expression: "new Pr
omise(() => undefined)" }) |
| 116 .then((result) => scheduleGCAndawaitPromise(result)) |
| 117 .then((result) => InspectorTest.logObject(result.error)) |
| 118 .then(() => next()); |
| 119 |
| 120 function scheduleGCAndawaitPromise(result) |
| 121 { |
| 122 var objectId = result.result.result.objectId; |
| 123 var promise = InspectorTest.sendCommandPromise("Runtime.awaitPromise", {
promiseObjectId: objectId }); |
| 124 gcPromise(objectId); |
| 125 return promise; |
| 126 } |
| 127 |
| 128 function gcPromise(objectId) |
| 129 { |
| 130 InspectorTest.sendCommandPromise("Runtime.releaseObject", { objectId: ob
jectId}) |
| 131 .then(() => InspectorTest.sendCommandPromise("Runtime.evaluate", { exp
ression: "gc()" })); |
| 132 } |
| 133 } |
| 134 ]); |
| 135 } |
OLD | NEW |