Repository: servo/servo
File: etc/ci/scenario/common_function_for_servo_test.py (Python)

Code before:
    # if os.environ.get("CI") and use_mitmproxy == MitmProxyRunType.NOPROXY:
    # if we are in CI and nobody overrode our mitmproxy type we want to replay.
    #    print("Setting mitmproxy replay")
    #    use_mitmproxy = MitmProxyRunType.REPLAY

    dump_file = pathlib.Path("/tmp/mitmdump-current")
    if use_mitmproxy == MitmProxyRunType.REPLAY and not dump_file.is_file():
        print(f"Dump file {dump_file} did not exist. We will abort")
        return
    hdc = HarmonyDeviceConnector()
    try:
        print("Stopping potential old servo instance ...")
        stop_servo()

        setup_hdc_forward()
        with MitmProxy(use_mitmproxy, dump_file, MITMPROXY_PORT):
            print("Starting new servo instance...")
            time.sleep(5)
            cmd_str = f"aa start -a EntryAbility -b org.servo.servo -U {ABOUT_BLANK} --psn=--webdriver"
            if use_mitmproxy.should_servo_proxy():
                cmd_str += f" --psn=--pref=network_https_proxy_uri=http://127.0.0.1:{MITMPROXY_PORT} --psn=--pref=network_http_proxy_uri=http://127.0.0.1:{MITMPROXY_PORT} --psn=--ignore-certificate-errors"
            hdc.cmd(
                cmd_str,
                timeout=10,
            )
            close_usb_popup(hdc)
            with HarmonyDevicePerfMode():
                test_fn()
    except Exception as e:
        print(f"Scenario test `{test_name}` failed with error: {e} (exception: {type(e)})")
        hdc.screenshot(f"servo_scenario_{test_name}_error.jpg")
        stop_servo()
        sys.exit(1)
    print("\033[32mTest Succeeded.\033[0m")
    stop_servo()


Review comment (refactor):
Let's move this into the `with HarmonyDevicePerfMode()` while we are at it. Starting the servo ability should be as close as possible to the enabling perf mode.

Code after:
def run_test(test_fn, test_name: str, use_mitmproxy: MitmProxyRunType = MitmProxyRunType.FORWARD):
    # if os.environ.get("CI") and use_mitmproxy == MitmProxyRunType.NOPROXY:
    # if we are in CI and nobody overrode our mitmproxy type we want to replay.
    #    print("Setting mitmproxy replay")
    #    use_mitmproxy = MitmProxyRunType.REPLAY

    dump_file = pathlib.Path("/tmp/mitmdump-current")
    if use_mitmproxy == MitmProxyRunType.REPLAY and not dump_file.is_file():
        print(f"Dump file {dump_file} did not exist. We will abort")
        return
    hdc = HarmonyDeviceConnector()
    try:
        print("Stopping potential old servo instance ...")
        stop_servo()

        setup_hdc_forward()
        with MitmProxy(use_mitmproxy, dump_file, MITMPROXY_PORT):
            print("Starting new servo instance...")
            time.sleep(5)
            cmd_str = f"aa start -a EntryAbility -b org.servo.servo -U {ABOUT_BLANK} --psn=--webdriver"
            if use_mitmproxy.should_servo_proxy():
                cmd_str += f" --psn=--pref=network_https_proxy_uri=http://127.0.0.1:{MITMPROXY_PORT} --psn=--pref=network_http_proxy_uri=http://127.0.0.1:{MITMPROXY_PORT} --psn=--ignore-certificate-errors"
            hdc.cmd(
                cmd_str,
                timeout=10,
            )
            with HarmonyDevicePerfMode():
                close_usb_popup(hdc)
                test_fn()
    except Exception as e:
        print(f"Scenario test `{test_name}` failed with error: {e} (exception: {type(e)})")
        hdc.screenshot(f"servo_scenario_{test_name}_error.jpg")
        stop_servo()
        sys.exit(1)
    print("\033[32mTest Succeeded.\033[0m")
    stop_servo()