testing/mozbase/mozdevice/tests/sut.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2
michael@0 3 # Any copyright is dedicated to the Public Domain.
michael@0 4 # http://creativecommons.org/publicdomain/zero/1.0/
michael@0 5
michael@0 6 import datetime
michael@0 7 import socket
michael@0 8 import time
michael@0 9
michael@0 10 from threading import Thread
michael@0 11
michael@0 12 class MockAgent(object):
michael@0 13
michael@0 14 MAX_WAIT_TIME_SECONDS = 10
michael@0 15 SOCKET_TIMEOUT_SECONDS = 5
michael@0 16
michael@0 17 def __init__(self, tester, start_commands = None, commands = []):
michael@0 18 if start_commands:
michael@0 19 self.commands = start_commands
michael@0 20 else:
michael@0 21 self.commands = [("testroot", "/mnt/sdcard"),
michael@0 22 ("isdir /mnt/sdcard/tests", "TRUE"),
michael@0 23 ("ver", "SUTAgentAndroid Version 1.14")]
michael@0 24 self.commands = self.commands + commands
michael@0 25
michael@0 26 self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
michael@0 27 self._sock.bind(("127.0.0.1", 0))
michael@0 28 self._sock.listen(1)
michael@0 29
michael@0 30 self.tester = tester
michael@0 31
michael@0 32 self.thread = Thread(target=self._serve_thread)
michael@0 33 self.thread.start()
michael@0 34
michael@0 35 self.should_stop = False
michael@0 36
michael@0 37 @property
michael@0 38 def port(self):
michael@0 39 return self._sock.getsockname()[1]
michael@0 40
michael@0 41 def _serve_thread(self):
michael@0 42 conn = None
michael@0 43 while self.commands:
michael@0 44 if not conn:
michael@0 45 conn, addr = self._sock.accept()
michael@0 46 conn.settimeout(self.SOCKET_TIMEOUT_SECONDS)
michael@0 47 conn.send("$>\x00")
michael@0 48 (command, response) = self.commands.pop(0)
michael@0 49 data = ''
michael@0 50 timeout = datetime.datetime.now() + datetime.timedelta(
michael@0 51 seconds=self.MAX_WAIT_TIME_SECONDS)
michael@0 52 # The data might come in chunks, particularly if we are expecting
michael@0 53 # multiple lines, as with push commands.
michael@0 54 while (len(data) < len(command) and
michael@0 55 datetime.datetime.now() < timeout):
michael@0 56 try:
michael@0 57 data += conn.recv(1024)
michael@0 58 except socket.timeout:
michael@0 59 # We handle timeouts in the main loop.
michael@0 60 pass
michael@0 61 self.tester.assertEqual(data.strip(), command)
michael@0 62 # send response and prompt separately to test for bug 789496
michael@0 63 # FIXME: Improve the mock agent, since overloading the meaning
michael@0 64 # of 'response' is getting confusing.
michael@0 65 if response is None: # code for "shut down"
michael@0 66 conn.shutdown(socket.SHUT_RDWR)
michael@0 67 conn.close()
michael@0 68 conn = None
michael@0 69 elif type(response) is int: # code for "time out"
michael@0 70 max_timeout = 15.0
michael@0 71 timeout = 0.0
michael@0 72 interval = 0.1
michael@0 73 while not self.should_stop and timeout < max_timeout:
michael@0 74 time.sleep(interval)
michael@0 75 timeout += interval
michael@0 76 if timeout >= max_timeout:
michael@0 77 raise Exception("Maximum timeout reached! This should not "
michael@0 78 "happen")
michael@0 79 return
michael@0 80 else:
michael@0 81 # pull is handled specially, as we just pass back the full
michael@0 82 # command line
michael@0 83 if "pull" in command:
michael@0 84 conn.send(response)
michael@0 85 else:
michael@0 86 conn.send("%s\n" % response)
michael@0 87 conn.send("$>\x00")
michael@0 88
michael@0 89 def wait(self):
michael@0 90 self.thread.join()

mercurial