Bug 1529612 - Part 3: Update a mochitest to expect bufferedamountlow events even when the threshold is 0 (Part 2 fixed this bug). r=mjf
authorByron Campen [:bwc] <docfaraday@gmail.com>
Thu, 09 May 2019 15:37:55 +0000
changeset 532091 5ea4e8902e7ebea07211b37e4cb50b3e15cd5fc4
parent 532090 2375b78d0f7a16a27cdec42b8475e806a0f9d5eb
child 532092 074678d618bbbd75055a45de59c0d810bd80f8b0
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmjf
bugs1529612
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1529612 - Part 3: Update a mochitest to expect bufferedamountlow events even when the threshold is 0 (Part 2 fixed this bug). r=mjf Differential Revision: https://phabricator.services.mozilla.com/D28528
dom/media/tests/mochitest/pc.js
--- a/dom/media/tests/mochitest/pc.js
+++ b/dom/media/tests/mochitest/pc.js
@@ -232,41 +232,53 @@ PeerConnectionTest.prototype.closeDataCh
  *        Data to send to the other peer. For Blobs the MIME type will be lost.
  * @param {Object} [options={ }]
  *        Options to specify the data channels to be used
  * @param {DataChannelWrapper} [options.sourceChannel=pcLocal.dataChannels[length - 1]]
  *        Data channel to use for sending the message
  * @param {DataChannelWrapper} [options.targetChannel=pcRemote.dataChannels[length - 1]]
  *        Data channel to use for receiving the message
  */
-PeerConnectionTest.prototype.send = function(data, options) {
+PeerConnectionTest.prototype.send = async function(data, options) {
   options = options || { };
-  var source = options.sourceChannel ||
+  const source = options.sourceChannel ||
            this.pcLocal.dataChannels[this.pcLocal.dataChannels.length - 1];
-  var target = options.targetChannel ||
+  const target = options.targetChannel ||
            this.pcRemote.dataChannels[this.pcRemote.dataChannels.length - 1];
-  var bufferedamount = options.bufferedAmountLowThreshold || 0;
-  var bufferlow_fired = true; // to make testing later easier
-  if (bufferedamount != 0) {
-    source.bufferedAmountLowThreshold = bufferedamount;
-    bufferlow_fired = false;
-    source.onbufferedamountlow = function() {
-      bufferlow_fired = true;
-    };
-  }
+  source.bufferedAmountLowThreshold = options.bufferedAmountLowThreshold || 0;
+
+  const getSizeInBytes = d => {
+    if (d instanceof Blob) {
+      return d.size;
+    } else if (d instanceof ArrayBuffer) {
+      return d.byteLength;
+    } else if (d instanceof String || typeof d === "string") {
+      return (new TextEncoder('utf-8')).encode(d).length;
+    } else {
+      ok(false);
+    }
+  };
+
+  const expectedSizeInBytes = getSizeInBytes(data);
+  const bufferedAmount = source.bufferedAmount;
+
+  source.send(data);
+  is(source.bufferedAmount, expectedSizeInBytes + bufferedAmount,
+    `Buffered amount should be ${expectedSizeInBytes}`);
+
+  await new Promise(resolve => source.onbufferedamountlow = resolve);
 
   return new Promise(resolve => {
     // Register event handler for the target channel
       target.onmessage = e => {
-        ok(bufferlow_fired, "bufferedamountlow event fired");
+        is(getSizeInBytes(e.data), expectedSizeInBytes,
+          `Expected to receive the same number of bytes as we sent (${expectedSizeInBytes})`);
 	resolve({ channel: target, data: e.data });
     };
-
-    source.send(data);
-  });
+  });;
 };
 
 /**
  * Create a data channel
  *
  * @param {Dict} options
  *        Options for the data channel (see nsIPeerConnection)
  */
@@ -712,16 +724,20 @@ DataChannelWrapper.prototype = {
    * Returns the readyState bit of the data channel
    *
    * @returns {String} The state of the channel
    */
   get readyState() {
     return this._channel.readyState;
   },
 
+  get bufferedAmount() {
+    return this._channel.bufferedAmount;
+  },
+
   /**
    * Sets the bufferlowthreshold of the channel
    *
    * @param {integer} amoutn
    *        The new threshold for the chanel
    */
   set bufferedAmountLowThreshold(amount) {
     this._channel.bufferedAmountLowThreshold = amount;