Example #1
0
def test_interrupt_ipc_subprocess_grandchild():
    with ExitStack() as context_stack:
        (
            child_opened_sentinel,
            parent_interrupt_sentinel,
            child_started_sentinel,
            child_interrupt_sentinel,
        ) = [
            context_stack.enter_context(safe_tempfile_path()) for _ in range(4)
        ]
        child_process = open_ipc_subprocess([
            sys.executable,
            file_relative_path(__file__,
                               "parent_subprocess_with_interrupt_support.py"),
            child_opened_sentinel,
            parent_interrupt_sentinel,
            child_started_sentinel,
            child_interrupt_sentinel,
        ])
        wait_for_file(child_opened_sentinel)
        wait_for_file(child_started_sentinel)
        interrupt_ipc_subprocess(child_process)
        wait_for_file(child_interrupt_sentinel)
        with open(child_interrupt_sentinel, "r") as fd:
            assert fd.read().startswith("received_keyboard_interrupt")
        wait_for_file(parent_interrupt_sentinel)
        with open(parent_interrupt_sentinel, "r") as fd:
            assert fd.read().startswith("parent_received_keyboard_interrupt")
Example #2
0
def test_interrupt_compute_log_tail_child(
    windows_legacy_stdio_env,  # pylint: disable=redefined-outer-name, unused-argument
):
    with ExitStack() as context_stack:
        (stdout_pids_file, stderr_pids_file, opened_sentinel, interrupt_sentinel) = [
            context_stack.enter_context(safe_tempfile_path()) for _ in range(4)
        ]

        child_process = open_ipc_subprocess(
            [
                sys.executable,
                file_relative_path(__file__, "compute_log_subprocess.py"),
                stdout_pids_file,
                stderr_pids_file,
                opened_sentinel,
                interrupt_sentinel,
            ]
        )
        wait_for_file(opened_sentinel)
        wait_for_file(stdout_pids_file)
        wait_for_file(stderr_pids_file)

        with open(opened_sentinel, "r") as opened_sentinel_fd:
            assert opened_sentinel_fd.read().startswith("opened_compute_log_subprocess")

        with open(stdout_pids_file, "r") as stdout_pids_fd:
            stdout_pids_str = stdout_pids_fd.read()
            assert stdout_pids_str.startswith("stdout pids:")
            stdout_pids = list(
                map(
                    lambda x: int(x) if x != "None" else None,
                    [x.strip("(),") for x in stdout_pids_str.split(" ")[2:]],
                )
            )

        with open(stderr_pids_file, "r") as stderr_pids_fd:
            stderr_pids_str = stderr_pids_fd.read()
            assert stderr_pids_str.startswith("stderr pids:")
            stderr_pids = list(
                map(
                    lambda x: int(x) if x != "None" else None,
                    [x.strip("(),") for x in stderr_pids_str.split(" ")[2:]],
                )
            )

        interrupt_ipc_subprocess(child_process)

        for stdout_pid in stdout_pids:
            if stdout_pid is not None:
                wait_for_process(stdout_pid)

        for stderr_pid in stderr_pids:
            if stderr_pid is not None:
                wait_for_process(stderr_pid)

        wait_for_file(interrupt_sentinel)

        with open(interrupt_sentinel, "r") as fd:
            assert fd.read().startswith("compute_log_subprocess_interrupt")
Example #3
0
def test_interrupt_compute_log_tail_grandchild(
        windows_legacy_stdio_env,  # pylint: disable=redefined-outer-name, unused-argument
):
    with ExitStack() as context_stack:
        (
            child_opened_sentinel,
            parent_interrupt_sentinel,
            child_started_sentinel,
            stdout_pids_file,
            stderr_pids_file,
            child_interrupt_sentinel,
        ) = [
            context_stack.enter_context(safe_tempfile_path()) for _ in range(6)
        ]

        parent_process = open_ipc_subprocess([
            sys.executable,
            file_relative_path(__file__, 'parent_compute_log_subprocess.py'),
            child_opened_sentinel,
            parent_interrupt_sentinel,
            child_started_sentinel,
            stdout_pids_file,
            stderr_pids_file,
            child_interrupt_sentinel,
        ])

        wait_for_file(child_opened_sentinel)
        wait_for_file(child_started_sentinel)

        wait_for_file(stdout_pids_file)
        with open(stdout_pids_file, 'r') as stdout_pids_fd:
            stdout_pids_str = stdout_pids_fd.read()
            assert stdout_pids_str.startswith('stdout pids:')
            stdout_pids = list(
                map(
                    lambda x: int(x) if x != 'None' else None,
                    [x.strip('(),') for x in stdout_pids_str.split(' ')[2:]],
                ))

        wait_for_file(stderr_pids_file)
        with open(stderr_pids_file, 'r') as stderr_pids_fd:
            stderr_pids_str = stderr_pids_fd.read()
            assert stderr_pids_str.startswith('stderr pids:')
            stderr_pids = list(
                map(
                    lambda x: int(x) if x != 'None' else None,
                    [x.strip('(),') for x in stderr_pids_str.split(' ')[2:]],
                ))

        interrupt_ipc_subprocess(parent_process)

        wait_for_file(child_interrupt_sentinel)
        with open(child_interrupt_sentinel, 'r') as fd:
            assert fd.read().startswith('compute_log_subprocess_interrupt')

        wait_for_file(parent_interrupt_sentinel)
        with open(parent_interrupt_sentinel, 'r') as fd:
            assert fd.read().startswith('parent_received_keyboard_interrupt')

        for stdout_pid in stdout_pids:
            if stdout_pid is not None:
                wait_for_process(stdout_pid)

        for stderr_pid in stderr_pids:
            if stderr_pid is not None:
                wait_for_process(stderr_pid)