1 # package is named tests, not test, so it won't be confused with test in stdlib
2 from __future__ import print_function
20 from nose.plugins.skip import SkipTest
23 from eventlet import tpool
26 # convenience for importers
30 @contextlib.contextmanager
31 def assert_raises(exc_type):
39 name = exc_type.__name__
40 except AttributeError:
42 assert False, 'Expected exception {0}'.format(name)
45 def skipped(func, *decorator_args):
46 """Decorator that marks a function as skipped.
48 @functools.wraps(func)
50 raise SkipTest(*decorator_args)
55 def skip_if(condition):
56 """ Decorator that skips a test if the *condition* evaluates True.
57 *condition* can be a boolean or a callable that accepts one argument.
58 The callable will be called with the function to be decorated, and
59 should return True to skip the test.
61 def skipped_wrapper(func):
62 @functools.wraps(func)
63 def wrapped(*a, **kw):
64 if isinstance(condition, bool):
67 result = condition(func)
73 return skipped_wrapper
76 def skip_unless(condition):
77 """ Decorator that skips a test if the *condition* does not return True.
78 *condition* can be a boolean or a callable that accepts one argument.
79 The callable will be called with the function to be decorated, and
80 should return True if the condition is satisfied.
82 def skipped_wrapper(func):
83 @functools.wraps(func)
84 def wrapped(*a, **kw):
85 if isinstance(condition, bool):
88 result = condition(func)
94 return skipped_wrapper
97 def using_pyevent(_f):
98 from eventlet.hubs import get_hub
99 return 'pyevent' in type(get_hub()).__module__
102 def skip_with_pyevent(func):
103 """ Decorator that skips a test if we're using the pyevent hub."""
104 return skip_if(using_pyevent)(func)
107 def skip_on_windows(func):
108 """ Decorator that skips a test on Windows."""
109 return skip_if(sys.platform.startswith('win'))(func)
112 def skip_if_no_itimer(func):
113 """ Decorator that skips a test if the `itimer` module isn't found """
120 return skip_unless(has_itimer)(func)
123 def skip_if_no_ssl(func):
124 """ Decorator that skips a test if SSL is not available."""
126 import eventlet.green.ssl
130 import eventlet.green.OpenSSL
136 class TestIsTakingTooLong(Exception):
137 """ Custom exception class to be raised when a test's runtime exceeds a limit. """
141 class LimitedTestCase(unittest.TestCase):
142 """ Unittest subclass that adds a timeout to all tests. Subclasses must
143 be sure to call the LimitedTestCase setUp and tearDown methods. The default
144 timeout is 1 second, change it by setting TEST_TIMEOUT to the desired
150 self.previous_alarm = None
151 self.timer = eventlet.Timeout(self.TEST_TIMEOUT,
152 TestIsTakingTooLong(self.TEST_TIMEOUT))
154 def reset_timeout(self, new_timeout):
155 """Changes the timeout duration; only has effect during one test.
156 `new_timeout` can be int or float.
159 self.timer = eventlet.Timeout(new_timeout,
160 TestIsTakingTooLong(new_timeout))
162 def set_alarm(self, new_timeout):
163 """Call this in the beginning of your test if you expect busy loops.
164 Only has effect during one test.
165 `new_timeout` must be int.
167 def sig_alarm_handler(sig, frame):
168 # Could arm previous alarm but test is failed anyway
169 # seems to be no point in restoring previous state.
170 raise TestIsTakingTooLong(new_timeout)
172 self.previous_alarm = (
173 signal.signal(signal.SIGALRM, sig_alarm_handler),
174 signal.alarm(new_timeout),
179 if self.previous_alarm:
180 signal.signal(signal.SIGALRM, self.previous_alarm[0])
181 signal.alarm(self.previous_alarm[1])
188 def assert_less_than(self, a, b, msg=None):
189 msg = msg or "%s not less than %s" % (a, b)
192 assertLessThan = assert_less_than
194 def assert_less_than_equal(self, a, b, msg=None):
195 msg = msg or "%s not less than or equal to %s" % (a, b)
198 assertLessThanEqual = assert_less_than_equal
201 def check_idle_cpu_usage(duration, allowed_part):
203 # TODO: use https://code.google.com/p/psutil/
204 from nose.plugins.skip import SkipTest
205 raise SkipTest('CPU usage testing not supported (`import resource` failed)')
207 r1 = resource.getrusage(resource.RUSAGE_SELF)
208 eventlet.sleep(duration)
209 r2 = resource.getrusage(resource.RUSAGE_SELF)
210 utime = r2.ru_utime - r1.ru_utime
211 stime = r2.ru_stime - r1.ru_stime
212 assert utime + stime < duration * allowed_part, \
213 "CPU usage over limit: user %.0f%% sys %.0f%% allowed %.0f%%" % (
214 utime / duration * 100, stime / duration * 100,
218 def verify_hub_empty():
220 def format_listener(listener):
221 return 'Listener %r for greenlet %r with run callback %r' % (
222 listener, listener.greenlet, getattr(listener.greenlet, 'run', None))
224 from eventlet import hubs
226 readers = hub.get_readers()
227 writers = hub.get_writers()
228 num_readers = len(readers)
229 num_writers = len(writers)
230 num_timers = hub.get_timers_count()
231 assert num_readers == 0 and num_writers == 0, \
232 "Readers: %s (%d) Writers: %s (%d)" % (
233 ', '.join(map(format_listener, readers)), num_readers,
234 ', '.join(map(format_listener, writers)), num_writers,
238 def find_command(command):
239 for dir in os.getenv('PATH', '/usr/bin:/usr/sbin').split(os.pathsep):
240 p = os.path.join(dir, command)
241 if os.access(p, os.X_OK):
243 raise IOError(errno.ENOENT, 'Command not found: %r' % command)
246 def silence_warnings(func):
247 def wrapper(*args, **kw):
248 warnings.simplefilter('ignore', DeprecationWarning)
250 return func(*args, **kw)
252 warnings.simplefilter('default', DeprecationWarning)
253 wrapper.__name__ = func.__name__
257 def get_database_auth():
258 """Retrieves a dict of connection parameters for connecting to test databases.
260 Authentication parameters are highly-machine specific, so
261 get_database_auth gets its information from either environment
262 variables or a config file. The environment variable is
263 "EVENTLET_DB_TEST_AUTH" and it should contain a json object. If
264 this environment variable is present, it's used and config files
265 are ignored. If it's not present, it looks in the local directory
266 (tests) and in the user's home directory for a file named
267 ".test_dbauth", which contains a json map of parameters to the
271 'MySQLdb': {'host': 'localhost', 'user': 'root', 'passwd': ''},
272 'psycopg2': {'user': 'test'},
275 if 'EVENTLET_DB_TEST_AUTH' in os.environ:
276 return json.loads(os.environ.get('EVENTLET_DB_TEST_AUTH'))
278 files = [os.path.join(os.path.dirname(__file__), '.test_dbauth'),
279 os.path.join(os.path.expanduser('~'), '.test_dbauth')]
282 auth_utf8 = json.load(open(f))
283 # Have to convert unicode objects to str objects because
284 # mysqldb is dumb. Using a doubly-nested list comprehension
285 # because we know that the structure is a two-level dict.
287 [(str(modname), dict(
288 [(str(k), str(v)) for k, v in connectargs.items()]))
289 for modname, connectargs in auth_utf8.items()])
295 def run_python(path):
296 if not path.endswith('.py'):
298 path = os.path.abspath(path)
299 src_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
300 new_env = os.environ.copy()
301 new_env['PYTHONPATH'] = os.pathsep.join(sys.path + [src_dir])
302 p = subprocess.Popen(
303 [sys.executable, path],
305 stderr=subprocess.STDOUT,
306 stdin=subprocess.PIPE,
307 stdout=subprocess.PIPE,
309 output, _ = p.communicate()
313 def run_isolated(path, prefix='tests/isolated/'):
314 output = run_python(prefix + path).rstrip()
315 if output.startswith(b'skip'):
316 parts = output.split(b':', 1)
319 skip_args.append(parts[1])
320 raise SkipTest(*skip_args)
321 assert output == b'pass', output
324 certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt')
325 private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key')