Bug 1447086 - Return a promise from stop() in httpd.js r?ted.mielczarek draft
authorThom Chiovoloni <tchiovoloni@mozilla.com>
Mon, 19 Mar 2018 11:40:18 -0700
changeset 769523 a8a31c9793e9f65b0c781f7caadfc2bbc78fbbb5
parent 769320 4f1014eb5039bdfdd7a39fb7785d102df1994a6f
push id103155
push userbmo:tchiovoloni@mozilla.com
push dateMon, 19 Mar 2018 18:40:51 +0000
reviewersted.mielczarek
bugs1447086
milestone61.0a1
Bug 1447086 - Return a promise from stop() in httpd.js r?ted.mielczarek MozReview-Commit-ID: 7pxiQazBMqg
netwerk/test/httpserver/httpd.js
netwerk/test/httpserver/test/test_start_stop.js
--- a/netwerk/test/httpserver/httpd.js
+++ b/netwerk/test/httpserver/httpd.js
@@ -583,36 +583,44 @@ nsHttpServer.prototype =
     }
   },
 
   //
   // see nsIHttpServer.stop
   //
   stop: function(callback)
   {
-    if (!callback)
-      throw Cr.NS_ERROR_NULL_POINTER;
     if (!this._socket)
       throw Cr.NS_ERROR_UNEXPECTED;
 
+    // If no argument was provided to stop, return a promise.
+    let returnValue = undefined;
+    if (!callback) {
+      returnValue = new Promise(resolve => {
+        callback = resolve;
+      });
+    }
+
     this._stopCallback = typeof callback === "function"
                        ? callback
                        : function() { callback.onStopped(); };
 
     dumpn(">>> stopping listening on port " + this._socket.port);
     this._socket.close();
     this._socket = null;
 
     // We can't have this identity any more, and the port on which we're running
     // this server now could be meaningless the next time around.
     this._identity._teardown();
 
     this._doQuit = false;
 
     // socket-close notification and pending request completion happen async
+
+    return returnValue;
   },
 
   //
   // see nsIHttpServer.registerFile
   //
   registerFile: function(path, file)
   {
     if (file && (!file.exists() || file.isDirectory()))
--- a/netwerk/test/httpserver/test/test_start_stop.js
+++ b/netwerk/test/httpserver/test/test_start_stop.js
@@ -37,36 +37,16 @@ function run_test()
     srv.start(PORT);
     do_throw("starting a started server");
   }
   catch (e)
   {
     isException(e, Cr.NS_ERROR_ALREADY_INITIALIZED);
   }
 
-  try
-  {
-    srv.stop();
-    do_throw("missing argument to stop");
-  }
-  catch (e)
-  {
-    isException(e, Cr.NS_ERROR_NULL_POINTER);
-  }
-
-  try
-  {
-    srv.stop(null);
-    do_throw("null argument to stop");
-  }
-  catch (e)
-  {
-    isException(e, Cr.NS_ERROR_NULL_POINTER);
-  }
-
   do_test_pending();
   srv.stop(function()
   {
     try
     {
       do_test_pending();
       run_test_2();
     }
@@ -120,32 +100,59 @@ function run_test_2()
 }
 
 function run_test_3()
 {
   dumpn("*** run_test_3");
 
   do_test_finished();
 
+  srv.start(PORT);
+
+  do_test_pending();
+  try
+  {
+    srv.stop().then(function()
+    {
+      try {
+        do_test_pending();
+        run_test_4();
+      } finally {
+        do_test_finished();
+      }
+    });
+  }
+  catch (e)
+  {
+    do_throw("error stopping with an object: " + e);
+  }
+}
+
+function run_test_4()
+{
+  dumpn("*** run_test_4");
+
+  do_test_finished();
+
   srv.registerPathHandler("/handle", handle);
   srv.start(PORT);
 
   // Don't rely on the exact (but implementation-constant) sequence of events
-  // as it currently exists by making either run_test_4 or serverStopped handle
+  // as it currently exists by making either run_test_5 or serverStopped handle
   // the final shutdown.
   do_test_pending();
 
-  runHttpTests([new Test(PREPATH + "/handle")], run_test_4);
+  runHttpTests([new Test(PREPATH + "/handle")], run_test_5);
 }
 
 var testsComplete = false;
 
-function run_test_4()
+function run_test_5()
 {
-  dumpn("*** run_test_4");
+  dumpn("*** run_test_5");
 
   testsComplete = true;
   if (stopped)
     do_test_finished();
 }
 
 
 const INTERVAL = 500;