From 9290dd9c7d6286906ef0f1fc9706e67ca0bb4bf4 Mon Sep 17 00:00:00 2001 From: Ot ten Thije Date: Wed, 18 Aug 2010 13:41:59 +0100 Subject: [PATCH] Added adb_interface.WaitForProcess and WaitForProcessEnd. Relies mostly on common code factored out of WaitForDevicePm and WaitForDeviceInstrumentation. --- testrunner/adb_interface.py | 113 ++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 31 deletions(-) diff --git a/testrunner/adb_interface.py b/testrunner/adb_interface.py index 698ea8bc0..ad31fb232 100755 --- a/testrunner/adb_interface.py +++ b/testrunner/adb_interface.py @@ -302,19 +302,10 @@ class AdbInterface: self.SendCommand("wait-for-device") # Now the device is there, but may not be running. # Query the package manager with a basic command - pm_found = False - attempts = 0 - wait_period = 5 - while not pm_found and (attempts*wait_period) < wait_time: - # assume the 'adb shell pm path android' command will always - # return 'package: something' in the success case - output = self.SendShellCommand("pm path android", retry_count=1) - if "package:" in output: - pm_found = True - else: - time.sleep(wait_period) - attempts += 1 - if not pm_found: + try: + self._WaitForShellCommandContents("pm path android", "package:", + wait_time) + except errors.WaitForResponseTimedOutError: raise errors.WaitForResponseTimedOutError( "Package manager did not respond after %s seconds" % wait_time) @@ -331,28 +322,88 @@ class AdbInterface: instrumentation_path = "%s/%s" % (package_name, runner_name) logger.Log("Waiting for instrumentation to be present") # Query the package manager - inst_found = False - attempts = 0 - wait_period = 5 - while not inst_found and (attempts*wait_period) < wait_time: - # assume the 'adb shell pm list instrumentation' - # return 'instrumentation: something' in the success case - try: - output = self.SendShellCommand("pm list instrumentation | grep %s" - % instrumentation_path, retry_count=1) - if "instrumentation:" in output: - inst_found = True - except errors.AbortError, e: - # ignore - pass - if not inst_found: - time.sleep(wait_period) - attempts += 1 - if not inst_found: + try: + command = "pm list instrumentation | grep %s" % instrumentation_path + self._WaitForShellCommandContents(command, "instrumentation:", wait_time, + raise_abort=False) + except errors.WaitForResponseTimedOutError : logger.Log( "Could not find instrumentation %s on device. Does the " "instrumentation in test's AndroidManifest.xml match definition" "in test_defs.xml?" % instrumentation_path) + raise + + def WaitForProcess(self, name, wait_time=120): + """Wait until a process is running on the device. + + Args: + name: the process name as it appears in `ps` + wait_time: time in seconds to wait + + Raises: + WaitForResponseTimedOutError if wait_time elapses and the process is + still not running + """ + logger.Log("Waiting for process %s" % name) + self.SendCommand("wait-for-device") + self._WaitForShellCommandContents("ps", name, wait_time) + + def WaitForProcessEnd(self, name, wait_time=120): + """Wait until a process is no longer running on the device. + + Args: + name: the process name as it appears in `ps` + wait_time: time in seconds to wait + + Raises: + WaitForResponseTimedOutError if wait_time elapses and the process is + still running + """ + logger.Log("Waiting for process %s to end" % name) + self._WaitForShellCommandContents("ps", name, wait_time, invert=True) + + def _WaitForShellCommandContents(self, command, expected, wait_time, + raise_abort=True, invert=False): + """Wait until the response to a command contains a given output. + + Assumes that a only successful execution of "adb shell " contains + the substring expected. Assumes that a device is present. + + Args: + command: adb shell command to execute + expected: the string that should appear to consider the + command successful. + wait_time: time in seconds to wait + raise_abort: if False, retry when executing the command raises an + AbortError, rather than failing. + invert: if True, wait until the command output no longer contains the + expected contents. + + Raises: + WaitForResponseTimedOutError: If wait_time elapses and the command has not + returned an output containing expected yet. + """ + # Query the device with the command + success = False + attempts = 0 + wait_period = 5 + while not success and (attempts*wait_period) < wait_time: + # assume the command will always contain expected in the success case + try: + output = self.SendShellCommand(command, retry_count=1) + if ((not invert and expected in output) + or (invert and expected not in output)): + success = True + except errors.AbortError, e: + if raise_abort: + raise + # ignore otherwise + + if not success: + time.sleep(wait_period) + attempts += 1 + + if not success: raise errors.WaitForResponseTimedOutError() def Sync(self, retry_count=3):