File: test_timeout.py

package info (click to toggle)
easyprocess 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: python: 774; sh: 25; makefile: 3
file content (156 lines) | stat: -rw-r--r-- 4,339 bytes parent folder | download | duplicates (2)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import sys
import time

import pytest

from easyprocess import EasyProcess

python = sys.executable


def test_timeout():
    p = EasyProcess("sleep 1").start()
    p.wait(0.2)
    assert p.is_alive()
    p.wait(0.2)
    assert p.is_alive()
    p.wait(2)
    assert not p.is_alive()

    assert EasyProcess("sleep 0.3").call().return_code == 0
    assert EasyProcess("sleep 0.3").call(timeout=0.1).return_code != 0
    assert EasyProcess("sleep 0.3").call(timeout=1).return_code == 0

    assert EasyProcess("sleep 0.3").call().timeout_happened is False
    assert EasyProcess("sleep 0.3").call(timeout=0.1).timeout_happened
    assert EasyProcess("sleep 0.3").call(timeout=1).timeout_happened is False


@pytest.mark.timeout(10)
def test_time_cli1():
    hdr = "import logging;logging.basicConfig(level=logging.DEBUG);from easyprocess import EasyProcess;"
    p = EasyProcess(
        [
            python,
            "-c",
            hdr + "EasyProcess('sleep 15').start()",
        ]
    )
    p.call()
    assert p.return_code == 0


@pytest.mark.timeout(10)
def test_time_cli2():
    hdr = "import logging;logging.basicConfig(level=logging.DEBUG);from easyprocess import EasyProcess;"
    p = EasyProcess(
        [
            python,
            "-c",
            hdr + "EasyProcess('sleep 15').call(timeout=0.5)",
        ]
    )
    p.call()
    assert p.return_code == 0


@pytest.mark.timeout(10)
def test_time2():
    p = EasyProcess("sleep 15").call(timeout=1)
    assert p.is_alive() is False
    assert p.timeout_happened
    assert p.return_code != 0
    assert p.stdout == ""


@pytest.mark.timeout(10)
def test_timeout_out():
    p = EasyProcess(
        [python, "-c", "import time;print( 'start');time.sleep(15);print( 'end')"]
    ).call(timeout=1)
    assert p.is_alive() is False
    assert p.timeout_happened
    assert p.return_code != 0
    assert p.stdout == ""


@pytest.mark.timeout(3)
def test_time3():
    EasyProcess("sleep 15").start()


ignore_term = """
import signal;
import time;
signal.signal(signal.SIGTERM, lambda *args: None);
while True:
    time.sleep(0.5);
"""


# @pytest.mark.timeout(10)
# def test_force_timeout():
#     proc = EasyProcess([python, "-c", ignore_term]).start()
#     # Calling stop() right away actually stops python before it
#     # has a change to actually compile and run the input code,
#     # meaning the signal handlers aren't registered yet. Give it
#     # a moment to setup
#     time.sleep(1)
#     proc.stop(kill_after=1)
#     assert proc.is_alive() is False
#     assert proc.return_code != 0


@pytest.mark.timeout(30)
def test_kill():
    proc = EasyProcess([python, "-c", ignore_term]).start()
    # Calling stop() right away actually stops python before it
    # has a change to actually compile and run the input code,
    # meaning the signal handlers aren't registered yet. Give it
    # a moment to setup
    time.sleep(3)
    proc.stop()
    assert proc.is_alive() is False
    assert proc.return_code != 0


# @pytest.mark.timeout(10)
# def test_force_0_timeout():
#     proc = EasyProcess([python, "-c", ignore_term]).start()
#     time.sleep(1)
#     proc.stop(kill_after=0)
#     assert proc.is_alive() is False
#     assert proc.return_code != 0


@pytest.mark.timeout(10)
def test_force_timeout2():
    proc = EasyProcess([python, "-c", ignore_term]).call(timeout=1)
    assert proc.is_alive() is False
    assert proc.return_code != 0


# @pytest.mark.timeout(10)
# def test_stop_wait():
#     proc = EasyProcess([python, "-c", ignore_term]).start()
#     time.sleep(1)
#     proc.sendstop().wait(timeout=1)
#     # On windows, Popen.terminate actually behaves like kill,
#     # so don't check that our hanging process code is actually hanging.
#     # The end result is still what we want. On other platforms, leave
#     # this assertion to make sure we are correctly testing the ability
#     # to stop a hung process
#     if not sys.platform.startswith("win"):
#         assert proc.is_alive() is True
#     proc.stop(kill_after=1)
#     assert proc.is_alive() is False
#     assert proc.return_code != 0


@pytest.mark.timeout(30)
def test_stop_wait():
    proc = EasyProcess([python, "-c", ignore_term]).start()
    time.sleep(3)
    proc.sendstop().wait(timeout=3)
    assert proc.is_alive() is False
    assert proc.return_code != 0