Bug 1295453 - Don't wait for data to be sent when closing socket immediately r=jdm
authorWilliam Lachance <wlachance@mozilla.com>
Tue, 06 Sep 2016 15:51:51 -0400
changeset 354174 3d0b41fdd93bd8233745eadb4e0358e385bf2cb9
parent 354173 66324402a18bb01a39f533aaabff0f202d89cc94
child 354182 a14f88a9af7a59e677478694bafd9375ac53683e
child 354260 223b9a23f37379d3e1dc23a667abbc846b812dff
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs1295453
milestone51.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 1295453 - Don't wait for data to be sent when closing socket immediately r=jdm We can't guarantee any data will be written, it should suffice to just make sure that the socket closes immediately and then verify that a complete set of data wasn't read. MozReview-Commit-ID: CHHuJEGRVgX
dom/network/tests/test_tcpsocket_client_and_server_basics.js
--- a/dom/network/tests/test_tcpsocket_client_and_server_basics.js
+++ b/dom/network/tests/test_tcpsocket_client_and_server_basics.js
@@ -30,33 +30,43 @@ function assertUint8ArraysEqual(a, b, co
 /**
  * Helper method to add event listeners to a socket and provide two Promise-returning
  * helpers (see below for docs on them).  This *must* be called during the turn of
  * the event loop where TCPSocket's constructor is called or the onconnect method is being
  * invoked.
  */
 function listenForEventsOnSocket(socket, socketType) {
   let wantDataLength = null;
+  let wantDataAndClose = false;
   let pendingResolve = null;
   let receivedEvents = [];
   let receivedData = null;
   let handleGenericEvent = function(event) {
     dump('(' + socketType + ' event: ' + event.type + ')\n');
     if (pendingResolve && wantDataLength === null) {
       pendingResolve(event);
       pendingResolve = null;
     } else {
       receivedEvents.push(event);
     }
   };
 
   socket.onopen = handleGenericEvent;
   socket.ondrain = handleGenericEvent;
   socket.onerror = handleGenericEvent;
-  socket.onclose = handleGenericEvent;
+  socket.onclose = function(event) {
+    if (!wantDataAndClose) {
+      handleGenericEvent(event);
+    } else if (pendingResolve) {
+      dump('(' + socketType + ' event: close)\n');
+      pendingResolve(receivedData);
+      pendingResolve = null;
+      wantDataAndClose = false;
+    }
+  }
   socket.ondata = function(event) {
     dump('(' + socketType + ' event: ' + event.type + ' length: ' +
          event.data.byteLength + ')\n');
     ok(socketCompartmentInstanceOfArrayBuffer(event.data),
        'payload is ArrayBuffer');
     var arr = new Uint8Array(event.data);
     if (receivedData === null) {
       receivedData = arr;
@@ -109,16 +119,29 @@ function listenForEventsOnSocket(socket,
         receivedData = null;
         return promise;
       }
       dump('(' + socketType + ' waiting for ' + length + ' bytes)\n');
       return new Promise(function(resolve, reject) {
         pendingResolve = resolve;
         wantDataLength = length;
       });
+    },
+    waitForAnyDataAndClose: function() {
+      if (pendingResolve) {
+        throw new Error('only one wait allowed at a time.');
+      }
+
+      return new Promise(function(resolve, reject) {
+        pendingResolve = resolve;
+        // we may receive no data before getting close, in which case we want to
+        // return an empty array
+        receivedData = new Uint8Array();
+        wantDataAndClose = true;
+      });
     }
   };
 }
 
 /**
  * Return a promise that is resolved when the server receives a connection.  The
  * promise is resolved with { socket, queue } where `queue` is the result of
  * calling listenForEventsOnSocket(socket).  This must be done because we need
@@ -374,23 +397,20 @@ function* test_basics() {
 
   // -- Attempt to send two non-string data.
   is(clientSocket.send(bigUint8Array.buffer, 0, bigUint8Array.length), false,
      'Server sending more than 64k should result in the buffer being full.');
   is(clientSocket.send(bigUint8Array.buffer, 0, bigUint8Array.length), false,
      'Server sending more than 64k should result in the buffer being full.');
   clientSocket.closeImmediately();
 
-  serverReceived = yield serverQueue.waitForDataWithAtLeastLength(1);
+  serverReceived = yield serverQueue.waitForAnyDataAndClose();
 
   is(serverReceived.length < (2 * bigUint8Array.length), true, 'Received array length less than sent array length');
 
-  is((yield serverQueue.waitForEvent()).type, 'close',
-     'The close event is received after calling closeImmediately');
-
   // -- Close the listening server (and try to connect)
   // We want to verify that the server actually closes / stops listening when
   // we tell it to.
   listeningServer.close();
 
   // - try and connect, get an error
   clientSocket = createSocket('127.0.0.1', serverPort,
                               { binaryType: 'arraybuffer' });