Skip to content

Commit ec13a36

Browse files
committed
unitests: use home brewed timeout for tests
1 parent d05fa12 commit ec13a36

8 files changed

Lines changed: 99 additions & 71 deletions

File tree

tests/base.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
import os
2323
import sys
24+
import signal
25+
import traceback
26+
27+
from nose.tools import make_decorator, TimeExpired
28+
2429
sys.path.append('.')
2530

2631
import json
@@ -54,3 +59,33 @@ def is_dict(dictionary):
5459
return True
5560

5661
return False
62+
63+
64+
class TestTimedOut(AssertionError):
65+
66+
def __init__(self, time_limit, frame):
67+
super(TestTimedOut, self).__init__('time_limit={0}\n{1}'.format(
68+
time_limit, ''.join(traceback.format_stack(frame))))
69+
self.time_limit = time_limit
70+
self.frame = frame
71+
72+
73+
def timeout(time_limit):
74+
def decorator(func):
75+
def timed_out(_signum, frame):
76+
raise TestTimedOut(time_limit, frame)
77+
78+
def newfunc(*args, **kwargs):
79+
orig_handler = signal.signal(signal.SIGALRM, timed_out)
80+
signal.alarm(time_limit)
81+
try:
82+
rc = func(*args, **kwargs)
83+
finally:
84+
signal.alarm(0)
85+
signal.signal(signal.SIGALRM, orig_handler)
86+
return rc
87+
88+
newfunc = make_decorator(func)(newfunc)
89+
return newfunc
90+
91+
return decorator

tests/test_audio.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
import os
2323
import unittest
2424
import sys
25-
from nose.tools import timed
2625
from flaky import flaky
2726

2827
sys.path.append('.')
2928

3029
import telegram
31-
from tests.base import BaseTest
30+
from tests.base import BaseTest, timeout
3231

3332

3433
class AudioTest(BaseTest, unittest.TestCase):
@@ -54,7 +53,7 @@ def setUp(self):
5453
}
5554

5655
@flaky
57-
@timed(10)
56+
@timeout(10)
5857
def test_send_audio_required_args_only(self):
5958
"""Test telegram.Bot sendAudio method"""
6059
print('Testing bot.sendAudio - With required arguments only')
@@ -73,7 +72,7 @@ def test_send_audio_required_args_only(self):
7372
self.assertEqual(audio.file_size, self.file_size)
7473

7574
@flaky
76-
@timed(10)
75+
@timeout(10)
7776
def test_send_audio_all_args(self):
7877
"""Test telegram.Bot sendAudio method"""
7978
print('Testing bot.sendAudio - With all arguments')
@@ -97,7 +96,7 @@ def test_send_audio_all_args(self):
9796
self.assertEqual(audio.file_size, self.file_size)
9897

9998
@flaky
100-
@timed(10)
99+
@timeout(10)
101100
def test_send_audio_mp3_file(self):
102101
"""Test telegram.Bot sendAudio method"""
103102
print('Testing bot.sendAudio - MP3 File')
@@ -119,7 +118,7 @@ def test_send_audio_mp3_file(self):
119118
self.assertEqual(audio.file_size, self.file_size)
120119

121120
@flaky
122-
@timed(10)
121+
@timeout(10)
123122
def test_send_audio_mp3_file_custom_filename(self):
124123
"""Test telegram.Bot sendAudio method"""
125124
print('Testing bot.sendAudio - MP3 File with custom filename')
@@ -142,7 +141,7 @@ def test_send_audio_mp3_file_custom_filename(self):
142141
self.assertEqual(audio.file_size, self.file_size)
143142

144143
@flaky
145-
@timed(10)
144+
@timeout(10)
146145
def test_send_audio_mp3_url_file(self):
147146
"""Test telegram.Bot sendAudio method"""
148147
print('Testing bot.sendAudio - MP3 File by URL')
@@ -164,7 +163,7 @@ def test_send_audio_mp3_url_file(self):
164163
self.assertEqual(audio.file_size, self.file_size)
165164

166165
@flaky
167-
@timed(10)
166+
@timeout(10)
168167
def test_send_audio_resend(self):
169168
"""Test telegram.Bot sendAudio method"""
170169
print('Testing bot.sendAudio - Resend by file_id')
@@ -219,7 +218,7 @@ def test_audio_to_dict(self):
219218
self.assertEqual(audio['file_size'], self.file_size)
220219

221220
@flaky
222-
@timed(10)
221+
@timeout(10)
223222
def test_error_send_audio_empty_file(self):
224223
print('Testing bot.sendAudio - Null file')
225224

@@ -233,7 +232,7 @@ def test_error_send_audio_empty_file(self):
233232
**json_dict))
234233

235234
@flaky
236-
@timed(10)
235+
@timeout(10)
237236
def test_error_send_audio_empty_file_id(self):
238237
print('Testing bot.sendAudio - Empty file_id')
239238

@@ -247,7 +246,7 @@ def test_error_send_audio_empty_file_id(self):
247246
**json_dict))
248247

249248
@flaky
250-
@timed(10)
249+
@timeout(10)
251250
def test_error_audio_without_required_args(self):
252251
print('Testing bot.sendAudio - Without required arguments')
253252

tests/test_bot.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@
3333
sys.path.append('.')
3434

3535
import telegram
36-
from tests.base import BaseTest
37-
from nose.tools import timed
36+
from tests.base import BaseTest, timeout
3837

3938

4039
class BotTest(BaseTest, unittest.TestCase):
4140
"""This object represents Tests for Telegram Bot."""
4241

4342
@flaky
44-
@timed(10)
43+
@timeout(10)
4544
def testGetMe(self):
4645
'''Test the telegram.Bot getMe method'''
4746
print('Testing getMe')
@@ -55,7 +54,7 @@ def testGetMe(self):
5554
self.assertEqual(bot.name, '@PythonTelegramBot')
5655

5756
@flaky
58-
@timed(10)
57+
@timeout(10)
5958
def testSendMessage(self):
6059
'''Test the telegram.Bot sendMessage method'''
6160
print('Testing sendMessage')
@@ -67,7 +66,7 @@ def testSendMessage(self):
6766
self.assertTrue(isinstance(message.date, datetime))
6867

6968
@flaky
70-
@timed(10)
69+
@timeout(10)
7170
def testGetUpdates(self):
7271
'''Test the telegram.Bot getUpdates method'''
7372
print('Testing getUpdates')
@@ -78,7 +77,7 @@ def testGetUpdates(self):
7877
self.assertTrue(isinstance(updates[0], telegram.Update))
7978

8079
@flaky
81-
@timed(10)
80+
@timeout(10)
8281
def testForwardMessage(self):
8382
'''Test the telegram.Bot forwardMessage method'''
8483
print('Testing forwardMessage')
@@ -92,7 +91,7 @@ def testForwardMessage(self):
9291
self.assertTrue(isinstance(message.forward_date, datetime))
9392

9493
@flaky
95-
@timed(10)
94+
@timeout(10)
9695
def testSendPhoto(self):
9796
'''Test the telegram.Bot sendPhoto method'''
9897
print('Testing sendPhoto - File')
@@ -105,7 +104,7 @@ def testSendPhoto(self):
105104
self.assertEqual(message.caption, 'testSendPhoto')
106105

107106
@flaky
108-
@timed(10)
107+
@timeout(10)
109108
def testResendPhoto(self):
110109
'''Test the telegram.Bot sendPhoto method'''
111110
print('Testing sendPhoto - Resend')
@@ -116,7 +115,7 @@ def testResendPhoto(self):
116115
self.assertEqual(message.photo[0].file_id, 'AgADAQADyKcxGx8j9Qdp6d-gpUsw4Gja1i8ABEVJsVqQk8LfJ3wAAgI')
117116

118117
@flaky
119-
@timed(10)
118+
@timeout(10)
120119
def testSendJPGURLPhoto(self):
121120
'''Test the telegram.Bot sendPhoto method'''
122121
print('Testing testSendJPGURLPhoto - URL')
@@ -127,7 +126,7 @@ def testSendJPGURLPhoto(self):
127126
self.assertEqual(message.photo[0].file_size, 822)
128127

129128
@flaky
130-
@timed(10)
129+
@timeout(10)
131130
def testSendPNGURLPhoto(self):
132131
'''Test the telegram.Bot sendPhoto method'''
133132
print('Testing testSendPNGURLPhoto - URL')
@@ -138,7 +137,7 @@ def testSendPNGURLPhoto(self):
138137
self.assertEqual(message.photo[0].file_size, 684)
139138

140139
@flaky
141-
@timed(10)
140+
@timeout(10)
142141
def testSendGIFURLPhoto(self):
143142
'''Test the telegram.Bot sendPhoto method'''
144143
print('Testing testSendGIFURLPhoto - URL')
@@ -149,7 +148,7 @@ def testSendGIFURLPhoto(self):
149148
self.assertEqual(message.photo[0].file_size, 684)
150149

151150
@flaky
152-
@timed(10)
151+
@timeout(10)
153152
def testSendChatAction(self):
154153
'''Test the telegram.Bot sendChatAction method'''
155154
print('Testing sendChatAction - ChatAction.TYPING')
@@ -158,7 +157,7 @@ def testSendChatAction(self):
158157
chat_id=self._chat_id)
159158

160159
@flaky
161-
@timed(10)
160+
@timeout(10)
162161
def testGetUserProfilePhotos(self):
163162
'''Test the telegram.Bot getUserProfilePhotos method'''
164163
print('Testing getUserProfilePhotos')

tests/test_document.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
import os
2323
import unittest
2424
import sys
25-
from nose.tools import timed
2625
from flaky import flaky
2726

2827
sys.path.append('.')
2928

3029
import telegram
31-
from tests.base import BaseTest
30+
from tests.base import BaseTest, timeout
3231

3332

3433
class DocumentTest(BaseTest, unittest.TestCase):
@@ -55,7 +54,7 @@ def setUp(self):
5554
}
5655

5756
@flaky
58-
@timed(10)
57+
@timeout(10)
5958
def test_send_document_png_file(self):
6059
"""Test telegram.Bot sendDocument method"""
6160
print('Testing bot.sendDocument - PNG File')
@@ -73,7 +72,7 @@ def test_send_document_png_file(self):
7372
self.assertEqual(document.file_size, self.file_size)
7473

7574
@flaky
76-
@timed(10)
75+
@timeout(10)
7776
def test_send_document_png_file_with_custom_file_name(self):
7877
"""Test telegram.Bot sendDocument method"""
7978
print('Testing bot.sendDocument - PNG File with custom filename')
@@ -92,7 +91,7 @@ def test_send_document_png_file_with_custom_file_name(self):
9291
self.assertEqual(document.file_size, self.file_size)
9392

9493
@flaky
95-
@timed(10)
94+
@timeout(10)
9695
def test_send_document_url_gif_file(self):
9796
"""Test telegram.Bot sendDocument method"""
9897
print('Testing bot.sendDocument - GIF File by URL')
@@ -110,7 +109,7 @@ def test_send_document_url_gif_file(self):
110109
self.assertEqual(document.file_size, 3878)
111110

112111
@flaky
113-
@timed(10)
112+
@timeout(10)
114113
def test_send_document_resend(self):
115114
"""Test telegram.Bot sendDocument method"""
116115
print('Testing bot.sendDocument - Resend by file_id')
@@ -159,7 +158,7 @@ def test_document_to_dict(self):
159158
self.assertEqual(document['file_size'], self.file_size)
160159

161160
@flaky
162-
@timed(10)
161+
@timeout(10)
163162
def test_error_send_document_empty_file(self):
164163
print('Testing bot.sendDocument - Null file')
165164

@@ -173,7 +172,7 @@ def test_error_send_document_empty_file(self):
173172
**json_dict))
174173

175174
@flaky
176-
@timed(10)
175+
@timeout(10)
177176
def test_error_send_document_empty_file_id(self):
178177
print('Testing bot.sendDocument - Empty file_id')
179178

@@ -187,7 +186,7 @@ def test_error_send_document_empty_file_id(self):
187186
**json_dict))
188187

189188
@flaky
190-
@timed(10)
189+
@timeout(10)
191190
def test_error_document_without_required_args(self):
192191
print('Testing bot.sendDocument - Without required arguments')
193192

0 commit comments

Comments
 (0)