summaryrefslogtreecommitdiffstats
path: root/devtools/shared/protocol/tests/xpcshell/test_protocol_async.js
blob: dd7196710bdaa7aded2c76bb7e40fd97e9922735 (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
185
186
187
188
189
190
191
192
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Make sure we get replies in the same order that we sent their
 * requests even when earlier requests take several event ticks to
 * complete.
 */

const { waitForTick } = require("resource://devtools/shared/DevToolsUtils.js");
const protocol = require("resource://devtools/shared/protocol.js");
const { Arg, RetVal } = protocol;

const rootSpec = protocol.generateActorSpec({
  typeName: "root",

  methods: {
    simpleReturn: {
      response: { value: RetVal() },
    },
    promiseReturn: {
      request: { toWait: Arg(0, "number") },
      response: { value: RetVal("number") },
    },
    simpleThrow: {
      response: { value: RetVal("number") },
    },
    promiseThrow: {
      request: { toWait: Arg(0, "number") },
      response: { value: RetVal("number") },
    },
  },
});

class RootActor extends protocol.Actor {
  constructor(conn) {
    super(conn, rootSpec);

    // Root actor owns itself.
    this.manage(this);
    this.actorID = "root";
    this.sequence = 0;
  }

  sayHello() {
    return {
      from: "root",
      applicationType: "xpcshell-tests",
      traits: [],
    };
  }

  simpleReturn() {
    return this.sequence++;
  }

  // Guarantee that this resolves after simpleReturn returns.
  async promiseReturn(toWait) {
    const sequence = this.sequence++;

    // Wait until the number of requests specified by toWait have
    // happened, to test queuing.
    while (this.sequence - sequence < toWait) {
      await waitForTick();
    }

    return sequence;
  }

  simpleThrow() {
    throw new Error(this.sequence++);
  }

  // Guarantee that this resolves after simpleReturn returns.
  promiseThrow(toWait) {
    return this.promiseReturn(toWait).then(Promise.reject);
  }
}

class RootFront extends protocol.FrontClassWithSpec(rootSpec) {
  constructor(client) {
    super(client);
    this.actorID = "root";
    // Root owns itself.
    this.manage(this);
  }
}
protocol.registerFront(RootFront);

add_task(async function () {
  DevToolsServer.createRootActor = conn => new RootActor(conn);
  DevToolsServer.init();

  const trace = connectPipeTracing();
  const client = new DevToolsClient(trace);
  await client.connect();

  const rootFront = client.mainRoot;

  const calls = [];
  let sequence = 0;

  // Execute a call that won't finish processing until 2
  // more calls have happened
  calls.push(
    rootFront.promiseReturn(2).then(ret => {
      // Check right return order
      Assert.equal(sequence, 0);
      // Check request handling order
      Assert.equal(ret, sequence++);
    })
  );

  // Put a few requests into the backlog

  calls.push(
    rootFront.simpleReturn().then(ret => {
      // Check right return order
      Assert.equal(sequence, 1);
      // Check request handling order
      Assert.equal(ret, sequence++);
    })
  );

  calls.push(
    rootFront.simpleReturn().then(ret => {
      // Check right return order
      Assert.equal(sequence, 2);
      // Check request handling order
      Assert.equal(ret, sequence++);
    })
  );

  calls.push(
    rootFront.simpleThrow().then(
      () => {
        Assert.ok(false, "simpleThrow shouldn't succeed!");
      },
      error => {
        // Check right return order
        Assert.equal(sequence++, 3);
      }
    )
  );

  calls.push(
    rootFront.promiseThrow(2).then(
      () => {
        Assert.ok(false, "promiseThrow shouldn't succeed!");
      },
      error => {
        // Check right return order
        Assert.equal(sequence++, 4);
        Assert.ok(true, "simple throw should throw");
      }
    )
  );

  calls.push(
    rootFront.simpleReturn().then(ret => {
      // Check right return order
      Assert.equal(sequence, 5);
      // Check request handling order
      Assert.equal(ret, sequence++);
    })
  );

  // Break up the backlog with a long request that waits
  // for another simpleReturn before completing
  calls.push(
    rootFront.promiseReturn(1).then(ret => {
      // Check right return order
      Assert.equal(sequence, 6);
      // Check request handling order
      Assert.equal(ret, sequence++);
    })
  );

  calls.push(
    rootFront.simpleReturn().then(ret => {
      // Check right return order
      Assert.equal(sequence, 7);
      // Check request handling order
      Assert.equal(ret, sequence++);
    })
  );

  await Promise.all(calls);
  await client.close();
});