-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_test.py
More file actions
executable file
·226 lines (185 loc) · 4.65 KB
/
run_test.py
File metadata and controls
executable file
·226 lines (185 loc) · 4.65 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/python
from thread_timeout import *
import time
from nose import run
def test1():
''' timeout is not stopping quick function
'''
@thread_timeout(2)
def func(delay):
time.sleep(delay)
func(1)
def test2():
''' ExecTimeout
'''
@thread_timeout(1)
def func(delay):
time.sleep(delay)
try:
func(3)
raise Exception("Test2 failed: timeout does not work")
except ExecTimeout as e:
pass
def test3():
''' function returns result
'''
@thread_timeout(1)
def func(x):
return x
assert func('OK') == 'OK'
def test4():
''' FailedKillExecTimeout
'''
@thread_timeout(1)
def looong():
time.sleep(3)
try:
looong()
raise Exception('FailedKillExecTimeout was expected')
except FailedKillExecTimeout:
pass
def test5():
''' NotKillExecTimeout
'''
@thread_timeout(1, kill=False)
def looong_and_unkillable():
time.sleep(2)
try:
looong_and_unkillable()
raise Exception('NotKillExecTimeout was expected')
except NotKillExecTimeout as e:
print("Test5 OK, got expected exception %s" % repr(e))
def test6():
'''KilledExecTimeout
'''
@thread_timeout(1, kill_wait=0.40)
def killme():
for a in range(0, 200):
time.sleep(0.01)
raise Exception("Not killed!")
try:
killme()
except KilledExecTimeout:
pass
def test7():
''' decorator is not changing python's into inspection
'''
from inspect import getargspec
def func(x, y=1, *args, **kwargs):
return vars()
func_with_timeout = thread_timeout(1)(func)
assert getargspec(func) == getargspec(func_with_timeout)
def test8():
''' Class methods
'''
class Class(object):
@thread_timeout(1)
def short(self, x):
return x
@thread_timeout(1, kill_wait=0.33)
def looong(self, x):
for x in range(0, 300):
time.sleep(0.01)
return x
obj = Class()
res = obj.short("OK")
assert res == 'OK'
try:
res = obj.looong('KO')
except KilledExecTimeout:
pass
def test9():
'''Check if exceptions are carried properly
'''
@thread_timeout(1, kill=False)
def exception(e):
raise e
exception_list = (
OverflowError,
ReferenceError,
SyntaxError,
ZeroDivisionError,
FloatingPointError,
BufferError,
LookupError,
AssertionError,
AttributeError,
TypeError,
EOFError,
IOError,
ImportError,
IndexError,
KeyError,
KeyboardInterrupt,
MemoryError,
NameError,
NotImplementedError,
OSError,
UnboundLocalError,
UnicodeError,
ValueError,
ExecTimeout
)
for exc in exception_list:
try:
exception(exc)
except exc as e:
pass
def test10():
'''Check if decorator stacking works (inner first)
'''
@thread_timeout(2)
def outer():
@thread_timeout(1)
def inner():
for x in range(0, 500):
time.sleep(0.01)
inner()
begin = time.time()
try:
outer()
except ExecTimeout as e:
pass
assert 1 < time.time() - begin < 2
def test11():
'''Check if decorator stacking works (outer first)
'''
@thread_timeout(1)
def outer():
@thread_timeout(2)
def inner():
for x in range(0, 500):
time.sleep(0.01)
inner()
begin = time.time()
try:
outer()
except ExecTimeout:
pass
assert 1 < time.time() - begin < 2
def test12():
'''Check if decorator waits before kill'''
@thread_timeout(3)
def outer():
@thread_timeout(3)
def inner():
for x in range(0, 100):
time.sleep(0.01)
inner()
begin = time.time()
outer()
assert 1 < time.time() - begin < 3
def test13():
'''check if we can instance methods with one kwarg (real bug case)'''
class Foo:
@thread_timeout(1)
def bar(self, arg=1):
pass
foo = Foo()
foo.bar(arg=0)
if __name__ == "__main__":
print("Running tests")
run(argv=[
'', __file__,
'-v'
])