Repository navigation
Expand file tree
/
Copy pathtest_timeouts.py
More file actions
179 lines (136 loc) · 6.39 KB
/
Copy pathtest_timeouts.py
File metadata and controls
179 lines (136 loc) · 6.39 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
import unittest
import sys
import time
import os
from robot.errors import TimeoutError
from robot.running.timeouts import TestTimeout, KeywordTimeout
from robot.utils.asserts import (assert_equal, assert_false, assert_true,
assert_raises, assert_raises_with_msg)
from robot.utils import JYTHON
# thread_resources is here
sys.path.append(os.path.join(os.path.dirname(__file__),'..','utils'))
from thread_resources import passing, failing, sleeping, returning, MyException
class VariableMock(object):
def replace_string(self, string):
return string
class TestInit(unittest.TestCase):
def test_no_params(self):
self._verify_tout(TestTimeout())
def test_timeout_string(self):
for tout_str, exp_str, exp_secs in [ ('1s', '1 second', 1),
('10 sec', '10 seconds', 10),
('2h 1minute', '2 hours 1 minute', 7260),
('42', '42 seconds', 42) ]:
self._verify_tout(TestTimeout(tout_str), exp_str, exp_secs)
def test_invalid_timeout_string(self):
for inv in ['invalid', '1s 1']:
err = "Setting test timeout failed: Invalid time string '%s'."
self._verify_tout(TestTimeout(inv), str=inv, secs=0.000001,
err=err % inv)
def _verify_tout(self, tout, str='', secs=-1, err=None):
tout.replace_variables(VariableMock())
assert_equal(tout.string, str)
assert_equal(tout.secs, secs)
assert_equal(tout.error, err)
class TestTimer(unittest.TestCase):
def test_time_left(self):
tout = TestTimeout('1s', variables=VariableMock())
tout.start()
assert_true(tout.time_left() > 0.9)
time.sleep(0.2)
assert_true(tout.time_left() < 0.9)
def test_timed_out_with_no_timeout(self):
tout = TestTimeout(variables=VariableMock())
tout.start()
time.sleep(0.01)
assert_false(tout.timed_out())
def test_timed_out_with_non_exceeded_timeout(self):
tout = TestTimeout('10s', variables=VariableMock())
tout.start()
time.sleep(0.01)
assert_false(tout.timed_out())
def test_timed_out_with_exceeded_timeout(self):
tout = TestTimeout('1ms', variables=VariableMock())
tout.start()
time.sleep(0.02)
assert_true(tout.timed_out())
class TestComparisons(unittest.TestCase):
def test_compare_when_none_timeouted(self):
touts = self._create_timeouts([''] * 10)
assert_equal(min(touts).string, '')
assert_equal(max(touts).string, '')
def test_compare_when_all_timeouted(self):
touts = self._create_timeouts(['1min','42seconds','43','1h1min','99'])
assert_equal(min(touts).string, '42 seconds')
assert_equal(max(touts).string, '1 hour 1 minute')
def test_compare_with_timeouted_and_non_timeouted(self):
touts = self._create_timeouts(['','1min','42sec','','43','1h1m','99',''])
assert_equal(min(touts).string, '42 seconds')
assert_equal(max(touts).string, '')
def test_that_compare_uses_starttime(self):
touts = self._create_timeouts(['1min','42seconds','43','1h1min','99'])
touts[2].starttime -= 2
assert_equal(min(touts).string, '43 seconds')
assert_equal(max(touts).string, '1 hour 1 minute')
def _create_timeouts(self, tout_strs):
touts = []
for tout_str in tout_strs:
touts.append(TestTimeout(tout_str, variables=VariableMock()))
touts[-1].start()
return touts
class TestRun(unittest.TestCase):
def setUp(self):
self.tout = TestTimeout('1s', variables=VariableMock())
self.tout.start()
def test_passing(self):
assert_equal(self.tout.run(passing), None)
def test_returning(self):
for arg in [ 10, 'hello', ['l','i','s','t'], unittest]:
ret = self.tout.run(returning, args=(arg,))
assert_equal(ret, arg)
def test_failing(self):
assert_raises_with_msg(MyException, 'hello world',
self.tout.run, failing, ('hello world',))
if JYTHON:
def test_java_failing(self):
from java.lang import Error
from thread_resources import java_failing
assert_raises_with_msg(Error, 'java.lang.Error: hi tellus',
self.tout.run, java_failing, ('hi tellus',))
def test_sleeping(self):
assert_equal(self.tout.run(sleeping, args=(0.01,)), 0.01)
def test_method_executed_normally_if_no_timeout(self):
os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
self.tout.run(sleeping, (0.05,))
assert_equal(os.environ['ROBOT_THREAD_TESTING'], '0.05')
def test_method_stopped_if_timeout(self):
os.environ['ROBOT_THREAD_TESTING'] = 'initial value'
self.tout.secs = 0.001
# PyThreadState_SetAsyncExc thrown exceptions are not guaranteed
# to occur in a specific timeframe ,, thus the actual Timeout exception
# maybe thrown too late in Windows.
# This is why we need to have an action that really will take some time (sleep 5 secs)
# to (almost) ensure that the 'ROBOT_THREAD_TESTING' setting is not executed before
# timeout exception occurs
assert_raises_with_msg(TimeoutError, 'Test timeout 1 second exceeded.',
self.tout.run, sleeping, (5,))
assert_equal(os.environ['ROBOT_THREAD_TESTING'], 'initial value')
def test_zero_and_negative_timeout(self):
for tout in [0, 0.0, -0.01, -1, -1000]:
self.tout.time_left = lambda: tout
assert_raises(TimeoutError, self.tout.run, sleeping, (10,))
class TestMessage(unittest.TestCase):
def test_non_active(self):
assert_equal(TestTimeout().get_message(), 'Test timeout not active.')
def test_active(self):
tout = KeywordTimeout('42s', variables=VariableMock())
tout.start()
msg = tout.get_message()
assert_true(msg.startswith('Keyword timeout 42 seconds active.'), msg)
assert_true(msg.endswith('seconds left.'), msg)
def test_failed_default(self):
tout = TestTimeout('1s', variables=VariableMock())
tout.starttime = time.time() - 2
assert_equal(tout.get_message(), 'Test timeout 1 second exceeded.')
if __name__ == '__main__':
unittest.main()