summaryrefslogtreecommitdiffstats
path: root/devtools/shared/discovery/tests/unit/test_discovery.js
blob: c31340b08966361d019559dcba76ce1e2dc661d7 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

var Cu = Components.utils;

const { require } =
  Cu.import("resource://devtools/shared/Loader.jsm", {});
const Services = require("Services");
const promise = require("promise");
const defer = require("devtools/shared/defer");
const EventEmitter = require("devtools/shared/event-emitter");
const discovery = require("devtools/shared/discovery/discovery");
const { setTimeout, clearTimeout } = require("sdk/timers");

Services.prefs.setBoolPref("devtools.discovery.log", true);

do_register_cleanup(() => {
  Services.prefs.clearUserPref("devtools.discovery.log");
});

function log(msg) {
  do_print("DISCOVERY: " + msg);
}

// Global map of actively listening ports to TestTransport instances
var gTestTransports = {};

/**
 * Implements the same API as Transport in discovery.js.  Here, no UDP sockets
 * are used.  Instead, messages are delivered immediately.
 */
function TestTransport(port) {
  EventEmitter.decorate(this);
  this.port = port;
  gTestTransports[this.port] = this;
}

TestTransport.prototype = {

  send: function (object, port) {
    log("Send to " + port + ":\n" + JSON.stringify(object, null, 2));
    if (!gTestTransports[port]) {
      log("No listener on port " + port);
      return;
    }
    let message = JSON.stringify(object);
    gTestTransports[port].onPacketReceived(null, message);
  },

  destroy: function () {
    delete gTestTransports[this.port];
  },

  // nsIUDPSocketListener

  onPacketReceived: function (socket, message) {
    let object = JSON.parse(message);
    object.from = "localhost";
    log("Recv on " + this.port + ":\n" + JSON.stringify(object, null, 2));
    this.emit("message", object);
  },

  onStopListening: function (socket, status) {}

};

// Use TestTransport instead of the usual Transport
discovery._factories.Transport = TestTransport;

// Ignore name generation on b2g and force a fixed value
Object.defineProperty(discovery.device, "name", {
  get: function () {
    return "test-device";
  }
});

function run_test() {
  run_next_test();
}

add_task(function* () {
  // At startup, no remote devices are known
  deepEqual(discovery.getRemoteDevicesWithService("devtools"), []);
  deepEqual(discovery.getRemoteDevicesWithService("penguins"), []);

  discovery.scan();

  // No services added yet, still empty
  deepEqual(discovery.getRemoteDevicesWithService("devtools"), []);
  deepEqual(discovery.getRemoteDevicesWithService("penguins"), []);

  discovery.addService("devtools", { port: 1234 });

  // Changes not visible until next scan
  deepEqual(discovery.getRemoteDevicesWithService("devtools"), []);
  deepEqual(discovery.getRemoteDevicesWithService("penguins"), []);

  yield scanForChange("devtools", "added");

  // Now we see the new service
  deepEqual(discovery.getRemoteDevicesWithService("devtools"), ["test-device"]);
  deepEqual(discovery.getRemoteDevicesWithService("penguins"), []);

  discovery.addService("penguins", { tux: true });
  yield scanForChange("penguins", "added");

  deepEqual(discovery.getRemoteDevicesWithService("devtools"), ["test-device"]);
  deepEqual(discovery.getRemoteDevicesWithService("penguins"), ["test-device"]);
  deepEqual(discovery.getRemoteDevices(), ["test-device"]);

  deepEqual(discovery.getRemoteService("devtools", "test-device"),
            { port: 1234, host: "localhost" });
  deepEqual(discovery.getRemoteService("penguins", "test-device"),
            { tux: true, host: "localhost" });

  discovery.removeService("devtools");
  yield scanForChange("devtools", "removed");

  discovery.addService("penguins", { tux: false });
  yield scanForChange("penguins", "updated");

  // Scan again, but nothing should be removed
  yield scanForNoChange("penguins", "removed");

  // Split the scanning side from the service side to simulate the machine with
  // the service becoming unreachable
  gTestTransports = {};

  discovery.removeService("penguins");
  yield scanForChange("penguins", "removed");
});

function scanForChange(service, changeType) {
  let deferred = defer();
  let timer = setTimeout(() => {
    deferred.reject(new Error("Reply never arrived"));
  }, discovery.replyTimeout + 500);
  discovery.on(service + "-device-" + changeType, function onChange() {
    discovery.off(service + "-device-" + changeType, onChange);
    clearTimeout(timer);
    deferred.resolve();
  });
  discovery.scan();
  return deferred.promise;
}

function scanForNoChange(service, changeType) {
  let deferred = defer();
  let timer = setTimeout(() => {
    deferred.resolve();
  }, discovery.replyTimeout + 500);
  discovery.on(service + "-device-" + changeType, function onChange() {
    discovery.off(service + "-device-" + changeType, onChange);
    clearTimeout(timer);
    deferred.reject(new Error("Unexpected change occurred"));
  });
  discovery.scan();
  return deferred.promise;
}