Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Cjian/check_emulator_running' in…
Browse files Browse the repository at this point in the history
…to Cjian/check_emulator_running
  • Loading branch information
jchen351 committed Nov 16, 2024
2 parents 5f0f8a8 + b9ea99c commit 01e3edd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tools/python/util/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def start_emulator(
) -> subprocess.Popen:
if is_emulator_running_by_avd(avd_name=avd_name):
raise RuntimeError(
f"An emulator with avd_name{avd_name} is already running. Please close it before starting a new one.")
f"An emulator with avd_name{avd_name} is already running. Please close it before starting a new one."
)
with contextlib.ExitStack() as emulator_stack, contextlib.ExitStack() as waiter_stack:
emulator_args = [
sdk_tool_paths.emulator,
Expand Down Expand Up @@ -214,6 +215,7 @@ def start_emulator(
raise RuntimeError("Emulator failed to start.")
return emulator_process


def is_emulator_running_by_avd(avd_name: str) -> bool:
"""
Check if an emulator is running based on the provided AVD name.
Expand All @@ -235,9 +237,7 @@ def is_emulator_running_by_avd(avd_name: str) -> bool:
# Step 2: Check each running emulator's AVD name
for emulator in running_emulators:
try:
avd_info = subprocess.check_output(
["adb", "-s", emulator, "emu", "avd", "name"], text=True
).strip()
avd_info = subprocess.check_output(["adb", "-s", emulator, "emu", "avd", "name"], text=True).strip()
if avd_info == avd_name:
return True
except subprocess.SubprocessError:
Expand Down Expand Up @@ -283,8 +283,7 @@ def stop_emulator_by_proc(emulator_proc: subprocess.Popen, timeout: int = 120):

while is_emulator_running_by_proc(emulator_proc):
if datetime.datetime.now() > end_time:
raise RuntimeError(
f"Failed to stop the emulator within the specified timeout = {timeout} seconds.")
raise RuntimeError(f"Failed to stop the emulator within the specified timeout = {timeout} seconds.")
_log.debug("Emulator still running. Checking again in 5 seconds...")
time.sleep(interval)

Expand All @@ -311,7 +310,8 @@ def stop_emulator_by_pid(emulator_pid: int, timeout: int = 120):
while is_emulator_running_by_pid(emulator_pid):
if datetime.datetime.now() > end_time:
raise RuntimeError(
f"Failed to stop the emulator with PID {emulator_pid} within the specified timeout = {timeout} seconds.")
f"Failed to stop the emulator with PID {emulator_pid} within the specified timeout = {timeout} seconds."
)
_log.debug("Emulator still running. Checking again in 5 seconds...")
time.sleep(interval)

Expand Down

0 comments on commit 01e3edd

Please sign in to comment.