summaryrefslogtreecommitdiffstats
path: root/devtools/shared/network-observer/test/xpcshell/test_throttle.js
blob: 5f8ef589fb4b377b8fca49094c9f5402383069c9 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* eslint-disable mozilla/use-chromeutils-generateqi */

const { NetworkThrottleManager } = ChromeUtils.importESModule(
  "resource://devtools/shared/network-observer/NetworkThrottleManager.sys.mjs"
);
const nsIScriptableInputStream = Ci.nsIScriptableInputStream;

function TestStreamListener() {
  this.state = "initial";
}
TestStreamListener.prototype = {
  onStartRequest() {
    this.setState("start");
  },

  onStopRequest() {
    this.setState("stop");
  },

  onDataAvailable(request, inputStream, offset, count) {
    const sin = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
      nsIScriptableInputStream
    );
    sin.init(inputStream);
    this.data = sin.read(count);
    this.setState("data");
  },

  setState(state) {
    this.state = state;
    if (this._deferred) {
      this._deferred.resolve(state);
      this._deferred = null;
    }
  },

  onStateChanged() {
    if (!this._deferred) {
      let resolve, reject;
      const promise = new Promise(function (res, rej) {
        resolve = res;
        reject = rej;
      });
      this._deferred = { resolve, reject, promise };
    }
    return this._deferred.promise;
  },
};

function TestChannel() {
  this.state = "initial";
  this.testListener = new TestStreamListener();
  this._throttleQueue = null;
}
TestChannel.prototype = {
  QueryInterface() {
    return this;
  },

  get throttleQueue() {
    return this._throttleQueue;
  },

  set throttleQueue(q) {
    this._throttleQueue = q;
    this.state = "throttled";
  },

  setNewListener(listener) {
    this.listener = listener;
    this.state = "listener";
    return this.testListener;
  },
};

add_task(async function () {
  const throttler = new NetworkThrottleManager({
    latencyMean: 1,
    latencyMax: 1,
    downloadBPSMean: 500,
    downloadBPSMax: 500,
    uploadBPSMean: 500,
    uploadBPSMax: 500,
  });

  const uploadChannel = new TestChannel();
  throttler.manageUpload(uploadChannel);
  equal(
    uploadChannel.state,
    "throttled",
    "NetworkThrottleManager set throttleQueue"
  );

  const downloadChannel = new TestChannel();
  const testListener = downloadChannel.testListener;

  const listener = throttler.manage(downloadChannel);
  equal(
    downloadChannel.state,
    "listener",
    "NetworkThrottleManager called setNewListener"
  );

  equal(testListener.state, "initial", "test listener in initial state");

  // This method must be passed through immediately.
  listener.onStartRequest(null);
  equal(testListener.state, "start", "test listener started");

  const TEST_INPUT = "hi bob";

  const testStream = Cc["@mozilla.org/storagestream;1"].createInstance(
    Ci.nsIStorageStream
  );
  testStream.init(512, 512);
  const out = testStream.getOutputStream(0);
  out.write(TEST_INPUT, TEST_INPUT.length);
  out.close();
  const testInputStream = testStream.newInputStream(0);

  const activityDistributor = Cc[
    "@mozilla.org/network/http-activity-distributor;1"
  ].getService(Ci.nsIHttpActivityDistributor);
  let activitySeen = false;
  listener.addActivityCallback(
    () => {
      activitySeen = true;
    },
    null,
    null,
    null,
    activityDistributor.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE,
    null,
    TEST_INPUT.length,
    null
  );

  // onDataAvailable is required to immediately read the data.
  listener.onDataAvailable(null, testInputStream, 0, 6);
  equal(testInputStream.available(), 0, "no more data should be available");
  equal(
    testListener.state,
    "start",
    "test listener should not have received data"
  );
  equal(activitySeen, false, "activity not distributed yet");

  let newState = await testListener.onStateChanged();
  equal(newState, "data", "test listener received data");
  equal(testListener.data, TEST_INPUT, "test listener received all the data");
  equal(activitySeen, true, "activity has been distributed");

  const onChange = testListener.onStateChanged();
  listener.onStopRequest(null, null);
  newState = await onChange;
  equal(newState, "stop", "onStateChanged reported");
});