Bug 1164811 - Let TCP presentation server can be inited again in |listener.onClose|. r=fabrice
authorJunior Hsu <juhsu@mozilla.com>
Thu, 14 May 2015 03:37:00 -0400
changeset 244260 f49f111e85cb3fb6eac9ce96b848b2f1d97fb3e5
parent 244259 254fe695060738af0cef57507f10291f1937d521
child 244261 307db24f332a3d72e8e0b62b6c0013ba7bf7c612
push id28773
push usercbook@mozilla.com
push dateMon, 18 May 2015 11:43:50 +0000
treeherdermozilla-central@f578b845c4b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs1164811
milestone41.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 1164811 - Let TCP presentation server can be inited again in |listener.onClose|. r=fabrice
dom/presentation/provider/TCPPresentationServer.js
dom/presentation/tests/xpcshell/test_tcp_control_channel.js
--- a/dom/presentation/provider/TCPPresentationServer.js
+++ b/dom/presentation/provider/TCPPresentationServer.js
@@ -230,27 +230,31 @@ TCPPresentationServer.prototype = {
       delete this._controlChannels[index];
     }
   },
 
   // nsIServerSocketListener (Triggered by nsIServerSocket.init)
   onStopListening: function(aServerSocket, aStatus) {
     DEBUG && log("TCPPresentationServer - onStopListening: " + aStatus);
 
-    // manually closed
-    if (aStatus === Cr.NS_BINDING_ABORTED) {
+    if (this._serverSocket) {
+      DEBUG && log("TCPPresentationServer - should be non-manually closed");
+      this.close();
+    } else if (aStatus === Cr.NS_BINDING_ABORTED) {
+      DEBUG && log("TCPPresentationServer - should be manually closed");
       aStatus = Cr.NS_OK;
     }
+
     this._listener && this._listener.onClose(aStatus);
-    this._serverSocket = null;
   },
 
   close: function() {
-    DEBUG && log("TCPPresentationServer - close signalling channel");
+    DEBUG && log("TCPPresentationServer - close");
     if (this._serverSocket) {
+      DEBUG && log("TCPPresentationServer - close server socket");
       this._serverSocket.close();
       this._serverSocket = null;
     }
     this._id = null;
     this._port = 0;
     this._devices && this._devices.clear();
     this._devices = null;
   },
--- a/dom/presentation/tests/xpcshell/test_tcp_control_channel.js
+++ b/dom/presentation/tests/xpcshell/test_tcp_control_channel.js
@@ -36,36 +36,33 @@ function TestDescription(aType, aTcpAddr
   }
   this.tcpPort = aTcpPort;
 }
 
 TestDescription.prototype = {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationChannelDescription]),
 }
 
-function loopOfferAnser()
-{
+function loopOfferAnser() {
   const CONTROLLER_CONTROL_CHANNEL_PORT = 36777;
   const PRESENTER_CONTROL_CHANNEL_PORT = 36888;
 
   // presenter's presentation channel description
   const OFFER_ADDRESS = '192.168.123.123';
   const OFFER_PORT = 123;
 
   // controller's presentation channel description
   const ANSWER_ADDRESS = '192.168.321.321';
   const ANSWER_PORT = 321;
 
   let yayFuncs = makeJointSuccess(['controllerControlChannelClose',
                                    'presenterControlChannelClose']);
   let controllerDevice, controllerControlChannel;
   let presenterDevice, presenterControlChannel;
 
-  Services.prefs.setBoolPref("dom.presentation.tcp_server.debug", true);
-
   tps = Cc["@mozilla.org/presentation-device/tcp-presentation-server;1"]
         .createInstance(Ci.nsITCPPresentationServer);
   tps.init(null, PRESENTER_CONTROL_CHANNEL_PORT);
   tps.id = 'controllerID';
   controllerDevice = tps.createTCPDevice('controllerID',
                                          'controllerName',
                                          'testType',
                                          '127.0.0.1',
@@ -166,26 +163,56 @@ function loopOfferAnser()
       this.status = 'closed';
       Assert.equal(aReason, Cr.NS_OK, '3. presenterControlChannel notify closed NS_OK');
       yayFuncs.presenterControlChannelClose();
     },
     QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationControlChannelListener]),
   };
 }
 
-function shutdown()
-{
+function shutdownAndOneMoreLoop() {
+  const PRESENTER_CONTROL_CHANNEL_PORT = 36888;
+
+  let isFirstClose = true;
+  let expectedReason;
   tps.listener = {
     onClose: function(aReason) {
-      Assert.equal(aReason, Cr.NS_OK, 'TCPPresentationServer close success');
-      Services.prefs.clearUserPref("dom.presentation.tcp_server.debug");
-      run_next_test();
+      expectedReason = isFirstClose ? Cr.NS_ERROR_ABORT // non-manually closed
+                                    : Cr.NS_OK;         // manually closed
+      Assert.equal(aReason, expectedReason, 'TCPPresentationServer close as expected');
+      if (isFirstClose){
+        isFirstClose = false;
+        try {
+          tps.init('controllerID', PRESENTER_CONTROL_CHANNEL_PORT);
+          tps.close();
+        } catch (e) {
+          Assert.ok(false, 'TCP presentation init fail:' + e);
+          run_next_test();
+        }
+      } else {
+        run_next_test();
+      }
     },
   }
-  tps.close();
+
+  // A fake event to pretend the server socket is closed non-manually
+  tps.QueryInterface(Ci.nsIServerSocketListener)
+     .onStopListening(null, Cr.NS_ERROR_ABORT);
 }
 
+function setPref() {
+  Services.prefs.setBoolPref("dom.presentation.tcp_server.debug", true);
+  run_next_test();
+}
+
+function clearPref() {
+  Services.prefs.clearUserPref("dom.presentation.tcp_server.debug");
+  run_next_test();
+}
+
+add_test(setPref);
 add_test(loopOfferAnser);
-add_test(shutdown);
+add_test(shutdownAndOneMoreLoop);
+add_test(clearPref);
 
 function run_test() {
   run_next_test();
 }