Skip to content

Commit

Permalink
Backed out changeset 198c4bf0c8df (bug 1290372) for failing wpt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BavarianTomcat committed Oct 17, 2016
1 parent 2c6b6ac commit d64de23
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 68 deletions.
70 changes: 14 additions & 56 deletions testing/marionette/client/marionette_driver/marionette.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import ConfigParser
import base64
import datetime
import ConfigParser
import json
import os
import socket
import sys
import time
import traceback
import warnings

Expand Down Expand Up @@ -573,10 +571,11 @@ def __init__(self, host='localhost', port=2828, app=None, bin=None,
self.timeout = timeout
self.socket_timeout = socket_timeout

startup_timeout = startup_timeout or self.DEFAULT_STARTUP_TIMEOUT
if self.bin:
self.instance = self._create_instance(app, instance_args)
self.instance.start()
self.raise_for_connection(timeout=startup_timeout)
self.raise_for_port(self.wait_for_port(timeout=startup_timeout))

def _create_instance(self, app, instance_args):
if not Marionette.is_port_available(self.port, host=self.host):
Expand Down Expand Up @@ -640,55 +639,14 @@ def is_port_available(port, host=''):
finally:
s.close()

def _wait_for_connection(self, timeout=None):
"""Wait until Marionette server has been created the communication socket.
:param timeout: Timeout in seconds for the server to be ready.
"""
if timeout is None:
timeout = self.DEFAULT_STARTUP_TIMEOUT

runner = None
if self.instance is not None:
runner = self.instance.runner

poll_interval = 0.1
starttime = datetime.datetime.now()

while datetime.datetime.now() - starttime < datetime.timedelta(seconds=timeout):
# If the instance we want to connect to is not running return immediately
if runner is not None and not runner.is_running():
return False

sock = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
sock.connect((self.host, self.port))
data = sock.recv(16)
if ":" in data:
return True
except socket.error:
pass
finally:
if sock is not None:
sock.close()

time.sleep(poll_interval)

return False
def wait_for_port(self, timeout=None):
timeout = timeout or self.DEFAULT_STARTUP_TIMEOUT
return transport.wait_for_port(self.host, self.port, timeout=timeout)

@do_process_check
def raise_for_connection(self, timeout=None):
"""Raise socket.timeout if no connection can be established.
:param timeout: Timeout in seconds for the server to be ready.
"""
if not self._wait_for_connection(timeout):
raise socket.timeout("Timed out waiting for connection on {0}:{1}!".format(
self.host, self.port))
def raise_for_port(self, port_obtained):
if not port_obtained:
raise socket.timeout("Timed out waiting for port {}!".format(self.port))

@do_process_check
def _send_message(self, name, params=None, key=None):
Expand Down Expand Up @@ -1074,7 +1032,7 @@ def enforce_gecko_prefs(self, prefs):
if not pref_exists:
self.delete_session()
self.instance.restart(prefs)
self.raise_for_connection()
self.raise_for_port(self.wait_for_port())
self.start_session()
self.reset_timeouts()

Expand Down Expand Up @@ -1167,7 +1125,7 @@ def restart(self, clean=False, in_app=False, callback=None):
self._request_in_app_shutdown("eRestart")

try:
self.raise_for_connection()
self.raise_for_port(self.wait_for_port())
except socket.timeout:
if self.instance.runner.returncode is not None:
exc, val, tb = sys.exc_info()
Expand All @@ -1177,7 +1135,7 @@ def restart(self, clean=False, in_app=False, callback=None):
else:
self.delete_session()
self.instance.restart(clean=clean)
self.raise_for_connection()
self.raise_for_port(self.wait_for_port())

self.start_session(session_id=session_id)
self.reset_timeouts()
Expand Down Expand Up @@ -1220,9 +1178,9 @@ def start_session(self, desired_capabilities=None, session_id=None, timeout=60):
self.port,
self.socket_timeout)

# Call _wait_for_connection() before attempting to connect in
# Call wait_for_port() before attempting to connect in
# the event gecko hasn't started yet.
self._wait_for_connection(timeout=timeout)
self.wait_for_port(timeout=timeout)
self.protocol, _ = self.client.connect()

body = {"capabilities": desired_capabilities, "sessionId": session_id}
Expand Down
25 changes: 25 additions & 0 deletions testing/marionette/client/marionette_driver/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import datetime
import json
import socket
import time
Expand Down Expand Up @@ -282,3 +283,27 @@ def close(self):

def __del__(self):
self.close()


def wait_for_port(host, port, timeout=60):
"""Wait for the specified host/port to become available."""
starttime = datetime.datetime.now()
poll_interval = 0.1
while datetime.datetime.now() - starttime < datetime.timedelta(seconds=timeout):
sock = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.5)
sock.connect((host, port))
data = sock.recv(16)
sock.shutdown(socket.SHUT_RDWR)
sock.close()
if ":" in data:
return True
except socket.error:
pass
finally:
if sock is not None:
sock.close()
time.sleep(poll_interval)
return False
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import itertools
import time

from marionette.marionette_test import MarionetteTestCase

from marionette_driver import errors
from marionette.marionette_test import MarionetteTestCase


class TestMarionette(MarionetteTestCase):
class TestMarionetteProperties(MarionetteTestCase):

def test_correct_test_name(self):
"""Test that the correct test name gets set."""
Expand All @@ -22,14 +20,6 @@ def test_correct_test_name(self):

self.assertEqual(self.marionette.test_name, expected_test_name)

def test_wait_for_connection_non_existing_process(self):
"""Test that wait_for_port doesn't run into a timeout if instance is not running."""
self.marionette.quit()
self.assertIsNotNone(self.marionette.instance.runner.returncode)
start_time = time.time()
self.assertFalse(self.marionette._wait_for_connection(timeout=5))
self.assertLess(time.time() - start_time, 5)


class TestProtocol1Errors(MarionetteTestCase):
def setUp(self):
Expand Down

0 comments on commit d64de23

Please sign in to comment.