-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathtest_parentpoller.py
56 lines (38 loc) · 1.55 KB
/
test_parentpoller.py
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()