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

"use strict";

const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
const promise = require("promise");
const { NetworkThrottleManager } =
      require("devtools/shared/webconsole/throttle");
const nsIScriptableInputStream = Ci.nsIScriptableInputStream;

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

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

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

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

  onStateChanged: function() {
    if (!this._deferred) {
      this._deferred = promise.defer();
    }
    return this._deferred.promise;
  }
};

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

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

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

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

add_task(function*() {
  let throttler = new NetworkThrottleManager({
    roundTripTimeMean: 1,
    roundTripTimeMax: 1,
    downloadBPSMean: 500,
    downloadBPSMax: 500,
    uploadBPSMean: 500,
    uploadBPSMax: 500,
  });

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

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

  let 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, null);
  equal(testListener.state, "start", "test listener started");

  const TEST_INPUT = "hi bob";

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

  let 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, 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 = yield 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");

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