| 1 |
import os |
| 2 |
import sys |
| 3 |
import warnings |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from ipykernel.parentpoller import ParentPollerUnix, ParentPollerWindows |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.skipif(os.name == "nt", reason="only works on posix") |
| 12 |
def test_parent_poller_unix_to_pid1(): |
| 13 |
poller = ParentPollerUnix() |
| 14 |
with mock.patch("os.getppid", lambda: 1): # noqa: PT008 |
| 15 |
|
| 16 |
def exit_mock(*args): |
| 17 |
sys.exit(1) |
| 18 |
|
| 19 |
with mock.patch("os._exit", exit_mock), pytest.raises(SystemExit): |
| 20 |
poller.run() |
| 21 |
|
| 22 |
def mock_getppid(): |
| 23 |
msg = "hi" |
| 24 |
raise ValueError(msg) |
| 25 |
|
| 26 |
with mock.patch("os.getppid", mock_getppid), pytest.raises(ValueError): # noqa: PT011 |
| 27 |
poller.run() |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.skipif(os.name == "nt", reason="only works on posix") |
| 31 |
def test_parent_poller_unix_reparent_not_pid1(): |
| 32 |
parent_pid = 221 |
| 33 |
parent_pids = iter([parent_pid, parent_pid - 1]) |
| 34 |
|
| 35 |
poller = ParentPollerUnix(parent_pid=parent_pid) |
| 36 |
|
| 37 |
with mock.patch("os.getppid", lambda: next(parent_pids)): # noqa: PT008 |
| 38 |
|
| 39 |
def exit_mock(*args): |
| 40 |
sys.exit(1) |
| 41 |
|
| 42 |
with mock.patch("os._exit", exit_mock), pytest.raises(SystemExit): |
| 43 |
poller.run() |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.skipif(os.name != "nt", reason="only works on windows") |
| 47 |
def test_parent_poller_windows(): |
| 48 |
poller = ParentPollerWindows(interrupt_handle=1) |
| 49 |
|
| 50 |
def mock_wait(*args, **kwargs): |
| 51 |
return -1 |
| 52 |
|
| 53 |
with mock.patch("ctypes.windll.kernel32.WaitForMultipleObjects", mock_wait): # noqa |
| 54 |
with warnings.catch_warnings(): |
| 55 |
warnings.simplefilter("ignore") |
| 56 |
poller.run() |