This repository was archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsupport.py
146 lines (122 loc) · 4.48 KB
/
support.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
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
import os
import signal
import socket
import threading
import time
__all__ = [
"get_time",
"resource",
"socketpair",
"AlarmMixin",
"TimerMixin"
]
# Tolerance values for timer/speed fluctuations.
TOLERANCE = 0.5
# Detect whether we're running on Travis or AppVeyor. This
# is used to skip some verification points inside of tests to
# not randomly fail our CI due to wild timer/speed differences.
TRAVIS_CI = "TRAVIS" in os.environ
APPVEYOR = "APPVEYOR" in os.environ
try: # Python 2.x doesn't define time.perf_counter.
from time import perf_counter as get_time
except ImportError:
from time import time as get_time
try: # Python 2.6 doesn't have the resource module.
import resource
except ImportError:
resource = None
if hasattr(socket, 'socketpair'):
# Since Python 3.5, socket.socketpair() is now also available on Windows
socketpair = socket.socketpair
else:
# Replacement for socket.socketpair()
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
"""A socket pair usable as a self-pipe, for Windows.
Origin: https://gist.github.com/4325783, by Geert Jansen.
Public domain.
"""
if family == socket.AF_INET:
host = '127.0.0.1'
elif family == socket.AF_INET6:
host = '::1'
else:
raise ValueError("Only AF_INET and AF_INET6 socket address "
"families are supported")
if type != socket.SOCK_STREAM:
raise ValueError("Only SOCK_STREAM socket type is supported")
if proto != 0:
raise ValueError("Only protocol zero is supported")
# We create a connected TCP socket. Note the trick with setblocking(0)
# that prevents us from having to create a thread.
lsock = socket.socket(family, type, proto)
try:
lsock.bind((host, 0))
lsock.listen(1)
# On IPv6, ignore flow_info and scope_id
addr, port = lsock.getsockname()[:2]
csock = socket.socket(family, type, proto)
try:
csock.setblocking(False)
try:
csock.connect((addr, port))
except (OSError, socket.error):
pass
csock.setblocking(True)
ssock, _ = lsock.accept()
except:
csock.close()
raise
finally:
lsock.close()
return ssock, csock
class AlarmThread(threading.Thread):
def __init__(self, timeout):
super(AlarmThread, self).__init__(group=None)
self.setDaemon(True)
self.timeout = timeout
self.canceled = False
def cancel(self):
self.canceled = True
def run(self):
time.sleep(self.timeout)
if not self.canceled:
os.kill(os.getpid(), signal.SIGALRM)
class AlarmMixin(object):
alarm_thread = None
def _begin_alarm_thread(self, timeout):
if not hasattr(signal, "SIGALRM"):
self.skipTest("Platform doesn't have signal.SIGALRM")
self.addCleanup(self._cancel_alarm_thread)
self.alarm_thread = AlarmThread(timeout)
self.alarm_thread.start()
def _cancel_alarm_thread(self):
if self.alarm_thread is not None:
self.alarm_thread.cancel()
self.alarm_thread.join(0.0)
self.alarm_thread = None
def set_alarm(self, duration, handler):
sigalrm_handler = signal.signal(signal.SIGALRM, handler)
self.addCleanup(signal.signal, signal.SIGALRM, sigalrm_handler)
self._begin_alarm_thread(duration)
class TimerContext(object):
def __init__(self, testcase, lower=None, upper=None):
self.testcase = testcase
self.lower = lower
self.upper = upper
self.start_time = None
self.end_time = None
def __enter__(self):
self.start_time = get_time()
def __exit__(self, *args, **kwargs):
self.end_time = get_time()
total_time = self.end_time - self.start_time
# Skip timing on CI due to flakiness.
if TRAVIS_CI or APPVEYOR:
return
if self.lower is not None:
self.testcase.assertGreaterEqual(total_time, self.lower * (1.0 - TOLERANCE))
if self.upper is not None:
self.testcase.assertLessEqual(total_time, self.upper * (1.0 + TOLERANCE))
class TimerMixin(object):
def assertTakesTime(self, lower=None, upper=None):
return TimerContext(self, lower=lower, upper=upper)