await-promise.js 3.74 KB
Newer Older
1 2 3 4 5
// 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.
// Flags: --expose_gc

6
InspectorTest.log("Tests that Runtime.awaitPromise works.");
7

8
InspectorTest.addScript(
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
`
var resolveCallback;
var rejectCallback;
function createPromise()
{
    return new Promise((resolve, reject) => { resolveCallback = resolve; rejectCallback = reject });
}

function resolvePromise()
{
    resolveCallback(239);
    resolveCallback = undefined;
    rejectCallback = undefined;
}

function rejectPromise()
{
    rejectCallback(239);
    resolveCallback = undefined;
    rejectCallback = undefined;
}

//# sourceURL=test.js`);

33 34 35
Protocol.Debugger.enable()
  .then(() => Protocol.Debugger.setAsyncCallStackDepth({ maxDepth: 128 }))
  .then(() => testSuite());
36 37 38 39 40 41

function testSuite()
{
  InspectorTest.runTestSuite([
    function testResolvedPromise(next)
    {
42 43 44
      Protocol.Runtime.evaluate({ expression: "Promise.resolve(239)"})
        .then(result => Protocol.Runtime.awaitPromise({ promiseObjectId: result.result.result.objectId, returnByValue: false, generatePreview: true }))
        .then(result => InspectorTest.logMessage(result))
45 46 47 48 49
        .then(() => next());
    },

    function testRejectedPromise(next)
    {
50 51 52
      Protocol.Runtime.evaluate({ expression: "Promise.reject({ a : 1 })"})
        .then(result => Protocol.Runtime.awaitPromise({ promiseObjectId: result.result.result.objectId, returnByValue: true, generatePreview: false }))
        .then(result => InspectorTest.logMessage(result))
53 54 55 56 57
        .then(() => next());
    },

    function testRejectedPromiseWithStack(next)
    {
58 59 60
      Protocol.Runtime.evaluate({ expression: "createPromise()"})
        .then(result => scheduleRejectAndAwaitPromise(result))
        .then(result => InspectorTest.logMessage(result))
61 62 63 64
        .then(() => next());

      function scheduleRejectAndAwaitPromise(result)
      {
65 66
        var promise = Protocol.Runtime.awaitPromise({ promiseObjectId: result.result.result.objectId });
        Protocol.Runtime.evaluate({ expression: "rejectPromise()" });
67 68 69 70 71 72
        return promise;
      }
    },

    function testPendingPromise(next)
    {
73 74 75
      Protocol.Runtime.evaluate({ expression: "createPromise()"})
        .then(result => scheduleFulfillAndAwaitPromise(result))
        .then(result => InspectorTest.logMessage(result))
76 77 78 79
        .then(() => next());

      function scheduleFulfillAndAwaitPromise(result)
      {
80 81
        var promise = Protocol.Runtime.awaitPromise({ promiseObjectId: result.result.result.objectId });
        Protocol.Runtime.evaluate({ expression: "resolvePromise()" });
82 83 84 85 86 87
        return promise;
      }
    },

    function testResolvedWithoutArgsPromise(next)
    {
88 89 90
      Protocol.Runtime.evaluate({ expression: "Promise.resolve()"})
        .then(result => Protocol.Runtime.awaitPromise({ promiseObjectId: result.result.result.objectId, returnByValue: true, generatePreview: false }))
        .then(result => InspectorTest.logMessage(result))
91 92 93 94 95
        .then(() => next());
    },

    function testGarbageCollectedPromise(next)
    {
96 97 98
      Protocol.Runtime.evaluate({ expression: "new Promise(() => undefined)" })
        .then(result => scheduleGCAndawaitPromise(result))
        .then(result => InspectorTest.logMessage(result))
99 100 101 102 103
        .then(() => next());

      function scheduleGCAndawaitPromise(result)
      {
        var objectId = result.result.result.objectId;
104
        var promise = Protocol.Runtime.awaitPromise({ promiseObjectId: objectId });
105 106 107 108 109 110
        gcPromise(objectId);
        return promise;
      }

      function gcPromise(objectId)
      {
111 112
        Protocol.Runtime.releaseObject({ objectId: objectId})
          .then(() => Protocol.Runtime.evaluate({ expression: "gc()" }));
113 114 115 116
      }
    }
  ]);
}