summaryrefslogtreecommitdiffstats
path: root/devtools/shared/transport/tests/xpcshell/test_queue.js
blob: 498f8bfca4b49e27308be5890d8c0e4aa152b08c (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
193
194
195
196
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * This test verifies that the transport's queue operates correctly when various
 * packets are scheduled simultaneously.
 */

var { FileUtils } = ChromeUtils.import("resource://gre/modules/FileUtils.jsm");

function run_test() {
  initTestDevToolsServer();

  add_task(async function() {
    await test_transport(socket_transport);
    await test_transport(local_transport);
    DevToolsServer.destroy();
  });

  run_next_test();
}

/** * Tests ***/

var test_transport = async function(transportFactory) {
  let clientResolve;
  const clientDeferred = new Promise(resolve => {
    clientResolve = resolve;
  });

  let serverResolve;
  const serverDeferred = new Promise(resolve => {
    serverResolve = resolve;
  });

  // Ensure test files are not present from a failed run
  cleanup_files();
  const reallyLong = really_long();
  writeTestTempFile("bulk-input", reallyLong);

  Assert.equal(Object.keys(DevToolsServer._connections).length, 0);

  const transport = await transportFactory();

  // Sending from client to server
  function write_data({ copyFrom }) {
    NetUtil.asyncFetch(
      {
        uri: NetUtil.newURI(getTestTempFile("bulk-input")),
        loadUsingSystemPrincipal: true,
      },
      function(input, status) {
        copyFrom(input).then(() => {
          input.close();
        });
      }
    );
  }

  // Receiving on server from client
  function on_bulk_packet({ actor, type, length, copyTo }) {
    Assert.equal(actor, "root");
    Assert.equal(type, "file-stream");
    Assert.equal(length, reallyLong.length);

    const outputFile = getTestTempFile("bulk-output", true);
    outputFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));

    const output = FileUtils.openSafeFileOutputStream(outputFile);

    copyTo(output)
      .then(() => {
        FileUtils.closeSafeFileOutputStream(output);
        return verify();
      })
      .then(() => {
        // It's now safe to close
        transport.hooks.onClosed = () => {
          clientResolve();
        };
        transport.close();
      });
  }

  // Client

  function send_packets() {
    // Specifically, we want to ensure that multiple send()s proceed without
    // causing the transport to die.
    transport.send({
      actor: "root",
      type: "explode",
    });

    transport
      .startBulkSend({
        actor: "root",
        type: "file-stream",
        length: reallyLong.length,
      })
      .then(write_data);
  }

  transport.hooks = {
    onPacket: function(packet) {
      if (packet.error) {
        transport.hooks.onError(packet);
      } else if (packet.applicationType) {
        transport.hooks.onServerHello(packet);
      } else {
        do_throw("Unexpected server reply");
      }
    },

    onServerHello: function(packet) {
      // We've received the initial start up packet
      Assert.equal(packet.from, "root");
      Assert.equal(packet.applicationType, "xpcshell-tests");

      // Server
      Assert.equal(Object.keys(DevToolsServer._connections).length, 1);
      info(Object.keys(DevToolsServer._connections));
      for (const connId in DevToolsServer._connections) {
        DevToolsServer._connections[connId].onBulkPacket = on_bulk_packet;
      }

      DevToolsServer.on("connectionchange", type => {
        if (type === "closed") {
          serverResolve();
        }
      });

      send_packets();
    },

    onError: function(packet) {
      // The explode actor doesn't exist
      Assert.equal(packet.from, "root");
      Assert.equal(packet.error, "noSuchActor");
    },

    onClosed: function() {
      do_throw("Transport closed before we expected");
    },
  };

  transport.ready();

  return Promise.all([clientDeferred, serverDeferred]);
};

/** * Test Utils ***/

function verify() {
  const reallyLong = really_long();

  const inputFile = getTestTempFile("bulk-input");
  const outputFile = getTestTempFile("bulk-output");

  Assert.equal(inputFile.fileSize, reallyLong.length);
  Assert.equal(outputFile.fileSize, reallyLong.length);

  // Ensure output file contents actually match
  return new Promise(resolve => {
    NetUtil.asyncFetch(
      {
        uri: NetUtil.newURI(getTestTempFile("bulk-output")),
        loadUsingSystemPrincipal: true,
      },
      input => {
        const outputData = NetUtil.readInputStreamToString(
          input,
          reallyLong.length
        );
        // Avoid do_check_eq here so we don't log the contents
        Assert.ok(outputData === reallyLong);
        input.close();
        resolve();
      }
    );
  }).then(cleanup_files);
}

function cleanup_files() {
  const inputFile = getTestTempFile("bulk-input", true);
  if (inputFile.exists()) {
    inputFile.remove(false);
  }

  const outputFile = getTestTempFile("bulk-output", true);
  if (outputFile.exists()) {
    outputFile.remove(false);
  }
}