Bug 1261138 - Allow marionette to have any socket timeout, including None indicating no timeout, r=ato
authorJames Graham <james@hoppipolla.co.uk>
Thu, 31 Mar 2016 18:55:14 +0100
changeset 291122 3ea44d60cb3fe23293fe3326c43636336e4ac8dc
parent 291121 f2c661c123c089b02b6b542bbf95bb624bcafc22
child 291123 b2d35c87d6cddeea7f88e5bc4a7bccebd3100fce
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1261138
milestone48.0a1
Bug 1261138 - Allow marionette to have any socket timeout, including None indicating no timeout, r=ato MozReview-Commit-ID: L6iYzF1cnnX
testing/marionette/client/marionette_driver/marionette.py
testing/marionette/client/marionette_driver/transport.py
--- a/testing/marionette/client/marionette_driver/marionette.py
+++ b/testing/marionette/client/marionette_driver/marionette.py
@@ -535,20 +535,20 @@ class Marionette(object):
     TIMEOUT_PAGE = 'page load'
     DEFAULT_SOCKET_TIMEOUT = 360
     DEFAULT_STARTUP_TIMEOUT = 60
 
     def __init__(self, host='localhost', port=2828, app=None, app_args=None, bin=None,
                  profile=None, addons=None, emulator=None, sdcard=None, emulator_img=None,
                  emulator_binary=None, emulator_res=None, connect_to_running_emulator=False,
                  gecko_log=None, homedir=None, baseurl=None, no_window=False, logdir=None,
-                 busybox=None, symbols_path=None, timeout=None, socket_timeout=None,
-                 device_serial=None, adb_path=None, process_args=None,
-                 adb_host=None, adb_port=None, prefs=None, startup_timeout=None,
-                 workspace=None, verbose=0):
+                 busybox=None, symbols_path=None, timeout=None,
+                 socket_timeout=DEFAULT_SOCKET_TIMEOUT, device_serial=None, adb_path=None,
+                 process_args=None, adb_host=None, adb_port=None, prefs=None,
+                 startup_timeout=None, workspace=None, verbose=0):
         self.host = host
         self.port = self.local_port = port
         self.bin = bin
         self.profile = profile
         self.addons = addons
         self.instance = None
         self.session = None
         self.session_id = None
@@ -556,17 +556,17 @@ class Marionette(object):
         self.chrome_window = None
         self.runner = None
         self.emulator = None
         self.extra_emulators = []
         self.baseurl = baseurl
         self.no_window = no_window
         self._test_name = None
         self.timeout = timeout
-        self.socket_timeout = socket_timeout or self.DEFAULT_SOCKET_TIMEOUT
+        self.socket_timeout = socket_timeout
         self.device_serial = device_serial
         self.adb_host = adb_host
         self.adb_port = adb_port
 
         startup_timeout = startup_timeout or self.DEFAULT_STARTUP_TIMEOUT
 
         if bin:
             port = int(self.port)
--- a/testing/marionette/client/marionette_driver/transport.py
+++ b/testing/marionette/client/marionette_driver/transport.py
@@ -5,16 +5,30 @@
 import datetime
 import errno
 import json
 import socket
 import time
 import types
 
 
+class SocketTimeout(object):
+    def __init__(self, socket, timeout):
+        self.sock = socket
+        self.timeout = timeout
+        self.old_timeout = None
+
+    def __enter__(self):
+        self.old_timeout = self.sock.gettimeout()
+        self.sock.settimeout(self.timeout)
+
+    def __exit__(self, *args, **kwargs):
+        self.sock.settimeout(self.old_timeout)
+
+
 class Message(object):
     def __init__(self, msgid):
         self.id = msgid
 
     def __eq__(self, other):
         return self.id == other.id
 
 
@@ -211,21 +225,20 @@ class TcpTransport(object):
         try:
             self.sock.connect((self.addr, self.port))
         except:
             # Unset self.sock so that the next attempt to send will cause
             # another connection attempt.
             self.sock = None
             raise
 
-        self.sock.settimeout(2.0)
-
-        # first packet is always a JSON Object
-        # which we can use to tell which protocol level we are at
-        raw = self.receive(unmarshal=False)
+        with SocketTimeout(self.sock, 2.0):
+            # first packet is always a JSON Object
+            # which we can use to tell which protocol level we are at
+            raw = self.receive(unmarshal=False)
         hello = json.loads(raw)
         self.protocol = hello.get("marionetteProtocol", 1)
         self.application_type = hello.get("applicationType")
 
         return (self.protocol, self.application_type)
 
     def send(self, obj):
         """Send message to the remote server.  Allowed input is a