summaryrefslogtreecommitdiffstats
path: root/toolkit/components/asyncshutdown/tests/xpcshell/head.js
blob: 5cbf2359a2dceb50fef82eecf8753f45c12147bd (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

const { PromiseUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PromiseUtils.sys.mjs"
);

var { AsyncShutdown } = ChromeUtils.importESModule(
  "resource://gre/modules/AsyncShutdown.sys.mjs"
);

var asyncShutdownService = Cc[
  "@mozilla.org/async-shutdown-service;1"
].getService(Ci.nsIAsyncShutdownService);

Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);

/**
 * Utility function used to provide the same API for various sources
 * of async shutdown barriers.
 *
 * @param {string} kind One of
 * - "phase" to test an AsyncShutdown phase;
 * - "barrier" to test an instance of AsyncShutdown.Barrier;
 * - "xpcom-barrier" to test an instance of nsIAsyncShutdownBarrier;
 * - "xpcom-barrier-unwrapped" to test the field `jsclient` of a nsIAsyncShutdownClient.
 *
 * @return An object with the following methods:
 *   - addBlocker() - the same method as AsyncShutdown phases and barrier clients
 *   - wait() - trigger the resolution of the lock
 */
function makeLock(kind) {
  if (kind == "phase") {
    let topic = "test-Phase-" + ++makeLock.counter;
    let phase = AsyncShutdown._getPhase(topic);
    return {
      addBlocker(...args) {
        return phase.addBlocker(...args);
      },
      removeBlocker(blocker) {
        return phase.removeBlocker(blocker);
      },
      wait() {
        Services.obs.notifyObservers(null, topic);
        return Promise.resolve();
      },
      get isClosed() {
        return phase.isClosed;
      },
    };
  } else if (kind == "barrier") {
    let name = "test-Barrier-" + ++makeLock.counter;
    let barrier = new AsyncShutdown.Barrier(name);
    return {
      addBlocker: barrier.client.addBlocker,
      removeBlocker: barrier.client.removeBlocker,
      wait() {
        return barrier.wait();
      },
      get isClosed() {
        return barrier.client.isClosed;
      },
    };
  } else if (kind == "xpcom-barrier") {
    let name = "test-xpcom-Barrier-" + ++makeLock.counter;
    let barrier = asyncShutdownService.makeBarrier(name);
    return {
      addBlocker(blockerName, condition, state) {
        if (condition == null) {
          // Slight trick as `null` or `undefined` cannot be used as keys
          // for `xpcomMap`. Note that this has no incidence on the result
          // of the test as the XPCOM interface imposes that the condition
          // is a method, so it cannot be `null`/`undefined`.
          condition = "<this case can't happen with the xpcom interface>";
        }
        let blocker = makeLock.xpcomMap.get(condition);
        if (!blocker) {
          blocker = {
            name: blockerName,
            state,
            blockShutdown(aBarrierClient) {
              return (async function () {
                try {
                  if (typeof condition == "function") {
                    await Promise.resolve(condition());
                  } else {
                    await Promise.resolve(condition);
                  }
                } finally {
                  aBarrierClient.removeBlocker(blocker);
                }
              })();
            },
          };
          makeLock.xpcomMap.set(condition, blocker);
        }
        let { fileName, lineNumber, stack } = new Error();
        return barrier.client.addBlocker(blocker, fileName, lineNumber, stack);
      },
      removeBlocker(condition) {
        let blocker = makeLock.xpcomMap.get(condition);
        if (!blocker) {
          return;
        }
        barrier.client.removeBlocker(blocker);
      },
      wait() {
        return new Promise(resolve => {
          barrier.wait(resolve);
        });
      },
      get isClosed() {
        return barrier.client.isClosed;
      },
    };
  } else if ("unwrapped-xpcom-barrier") {
    let name = "unwrapped-xpcom-barrier-" + ++makeLock.counter;
    let barrier = asyncShutdownService.makeBarrier(name);
    let client = barrier.client.jsclient;
    return {
      addBlocker: client.addBlocker,
      removeBlocker: client.removeBlocker,
      wait() {
        return new Promise(resolve => {
          barrier.wait(resolve);
        });
      },
      get isClosed() {
        return client.isClosed;
      },
    };
  }
  throw new TypeError("Unknown kind " + kind);
}
makeLock.counter = 0;
makeLock.xpcomMap = new Map(); // Note: Not a WeakMap as we wish to handle non-gc-able keys (e.g. strings)

/**
 * An asynchronous task that takes several ticks to complete.
 *
 * @param {*=} resolution The value with which the resulting promise will be
 * resolved once the task is complete. This may be a rejected promise,
 * in which case the resulting promise will itself be rejected.
 * @param {object=} outResult An object modified by side-effect during the task.
 * Initially, its field |isFinished| is set to |false|. Once the task is
 * complete, its field |isFinished| is set to |true|.
 *
 * @return {promise} A promise fulfilled once the task is complete
 */
function longRunningAsyncTask(resolution = undefined, outResult = {}) {
  outResult.isFinished = false;
  if (!("countFinished" in outResult)) {
    outResult.countFinished = 0;
  }
  return new Promise(resolve => {
    do_timeout(100, function () {
      ++outResult.countFinished;
      outResult.isFinished = true;
      resolve(resolution);
    });
  });
}

function get_exn(f) {
  try {
    f();
    return null;
  } catch (ex) {
    return ex;
  }
}

function do_check_exn(exn, constructor) {
  Assert.notEqual(exn, null);
  if (exn.name == constructor) {
    Assert.equal(exn.constructor.name, constructor);
    return;
  }
  info("Wrong error constructor");
  info(exn.constructor.name);
  info(exn.stack);
  Assert.ok(false);
}