File: test_parentpoller.py

package info (click to toggle)
ipykernel 7.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,128 kB
  • sloc: python: 9,700; makefile: 165; sh: 8
file content (56 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import sys
import warnings
from unittest import mock

import pytest

from ipykernel.parentpoller import ParentPollerUnix, ParentPollerWindows


@pytest.mark.skipif(os.name == "nt", reason="only works on posix")
def test_parent_poller_unix_to_pid1():
    poller = ParentPollerUnix()
    with mock.patch("os.getppid", lambda: 1):  # noqa: PT008

        def exit_mock(*args):
            sys.exit(1)

        with mock.patch("os._exit", exit_mock), pytest.raises(SystemExit):
            poller.run()

    def mock_getppid():
        msg = "hi"
        raise ValueError(msg)

    with mock.patch("os.getppid", mock_getppid), pytest.raises(ValueError):  # noqa: PT011
        poller.run()


@pytest.mark.skipif(os.name == "nt", reason="only works on posix")
def test_parent_poller_unix_reparent_not_pid1():
    parent_pid = 221
    parent_pids = iter([parent_pid, parent_pid - 1])

    poller = ParentPollerUnix(parent_pid=parent_pid)

    with mock.patch("os.getppid", lambda: next(parent_pids)):  # noqa: PT008

        def exit_mock(*args):
            sys.exit(1)

        with mock.patch("os._exit", exit_mock), pytest.raises(SystemExit):
            poller.run()


@pytest.mark.skipif(os.name != "nt", reason="only works on windows")
def test_parent_poller_windows():
    poller = ParentPollerWindows(interrupt_handle=1)

    def mock_wait(*args, **kwargs):
        return -1

    with mock.patch("ctypes.windll.kernel32.WaitForMultipleObjects", mock_wait):  # noqa
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            poller.run()