-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pto.py
executable file
·73 lines (54 loc) · 1.66 KB
/
test_pto.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for PTO."""
import sys
if sys.version_info[0] >= 3:
import unittest
else:
import unittest2 as unittest
import pto
import time
_TIMEOUT = 5
_FUZZ_FACTOR = 1
class SlowClass(object):
@pto.timeout(_TIMEOUT)
def slow_instance_method(self):
cut_off = time.time() + _TIMEOUT
while time.time() < cut_off + _FUZZ_FACTOR:
pass
return True
@classmethod
@pto.timeout(_TIMEOUT)
def slow_class_method(cls):
cut_off = time.time() + _TIMEOUT
while time.time() < cut_off + _FUZZ_FACTOR:
pass
return True
@staticmethod
@pto.timeout(_TIMEOUT)
def slow_static_method():
cut_off = time.time() + _TIMEOUT
while time.time() < cut_off + _FUZZ_FACTOR:
pass
return True
class PtoTestCase(unittest.TestCase):
def setUp(self):
self.slowInstance = SlowClass()
def tearDown(self):
pass
def test_function(self):
@pto.timeout(_TIMEOUT)
def slow_func():
cut_off = time.time() + _TIMEOUT
while time.time() < cut_off + _FUZZ_FACTOR:
pass
return True
self.assertRaises(pto.TimedOutException, slow_func)
def test_instance_method(self):
self.assertRaises(pto.TimedOutException, self.slowInstance.slow_instance_method)
def test_class_method(self):
self.assertRaises(pto.TimedOutException, self.slowInstance.slow_class_method)
def test_static_method(self):
self.assertRaises(pto.TimedOutException, SlowClass.slow_static_method)
if __name__ == '__main__':
unittest.main()