Bug 742794 - Client should abort if told to start gecko instance on a used host/port, r=mdas, a=test-only
authorMihnea Dobrescu-Balaur <mihneadb@gmail.com>
Mon, 01 Apr 2013 10:25:38 -0400
changeset 118850 b409fb8b4d255079df1e258329262523380ac026
parent 118849 8d13adf2d15338381aa5e1c2d88e6fbbbc03e35f
child 118851 d6d3e797d1ae0fbc93709e8ac0a1fb5169485b93
push id141
push userjgriffin@mozilla.com
push dateWed, 01 May 2013 23:02:51 +0000
reviewersmdas, test-only
bugs742794
milestone18.0
Bug 742794 - Client should abort if told to start gecko instance on a used host/port, r=mdas, a=test-only
testing/marionette/client/marionette/marionette.py
--- a/testing/marionette/client/marionette/marionette.py
+++ b/testing/marionette/client/marionette/marionette.py
@@ -183,16 +183,20 @@ class Marionette(object):
         self.extra_emulators = []
         self.homedir = homedir
         self.baseurl = baseurl
         self.noWindow = noWindow
         self.logcat_dir = logcat_dir
         self._test_name = None
 
         if bin:
+            port = int(self.port)
+            if not Marionette.is_port_available(port, host=self.host):
+                ex_msg = "%s:%d is unavailable." % (self.host, port)
+                raise MarionetteException(message=ex_msg)
             self.instance = GeckoInstance(host=self.host, port=self.port,
                                           bin=self.bin, profile=self.profile)
             self.instance.start()
             assert(self.wait_for_port())
 
         if emulator:
             self.emulator = Emulator(homedir=homedir,
                                      noWindow=self.noWindow,
@@ -223,16 +227,28 @@ class Marionette(object):
     def __del__(self):
         if self.emulator:
             self.emulator.close()
         if self.instance:
             self.instance.close()
         for qemu in self.extra_emulators:
             qemu.emulator.close()
 
+    @staticmethod
+    def is_port_available(port, host=''):
+        port = int(port)
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        try:
+            s.bind((host, port))
+            return True
+        except socket.error:
+            return False
+        finally:
+            s.close()
+
     @classmethod
     def getMarionetteOrExit(cls, *args, **kwargs):
         try:
             m = cls(*args, **kwargs)
             return m
         except InstallGeckoError:
             # Bug 812395 - the process of installing gecko into the emulator
             # and then restarting B2G tickles some bug in the emulator/b2g