Commit a9b61d6c authored by Jason Madden's avatar Jason Madden

Let the examples be skipped if resources aren't around.

parent 92812e46
#!/usr/bin/python #!/usr/bin/python
# Copyright (c) 2009 Denis Bilenko. See LICENSE for details. # Copyright (c) 2009 Denis Bilenko. See LICENSE for details.
# gevent-test-requires-resource: network
"""Spawn multiple workers and wait for them to complete""" """Spawn multiple workers and wait for them to complete"""
from __future__ import print_function from __future__ import print_function
import gevent import gevent
......
#!/usr/bin/python #!/usr/bin/python
# gevent-test-requires-resource: network
"""Resolve hostnames concurrently, exit after 2 seconds. """Resolve hostnames concurrently, exit after 2 seconds.
Under the hood, this might use an asynchronous resolver based on Under the hood, this might use an asynchronous resolver based on
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
[1] http://pypi.python.org/pypi/py-sendfile/ [1] http://pypi.python.org/pypi/py-sendfile/
""" """
# gevent-test-requires-resource: sendfile
# pylint:disable=import-error # pylint:disable=import-error
from errno import EAGAIN from errno import EAGAIN
from sendfile import sendfile as original_sendfile from sendfile import sendfile as original_sendfile
......
from __future__ import print_function from __future__ import print_function
# gevent-test-requires-resource: psycopg2
# pylint:disable=import-error,broad-except,bare-except # pylint:disable=import-error,broad-except,bare-except
import sys import sys
import contextlib import contextlib
......
from __future__ import print_function from __future__ import print_function
# gevent-test-requires-resource: unixsocket_server
import socket import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
......
# gevent-test-requires-resource: unixsocket_client
import os import os
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
from gevent import socket from gevent import socket
......
#!/usr/bin/python #!/usr/bin/python
# gevent-test-requires-resource: webpy
"""A web.py application powered by gevent""" """A web.py application powered by gevent"""
from __future__ import print_function from __future__ import print_function
......
...@@ -171,6 +171,15 @@ def setup_resources(resources=None): ...@@ -171,6 +171,15 @@ def setup_resources(resources=None):
return resources return resources
def ensure_setup_resources():
# Call when you don't know if resources have been setup and you want to
# get the environment variable if needed.
# Returns an object with `is_resource_enabled`.
from . import support
if not support.gevent_has_setup_resources:
setup_resources()
return support
def exit_without_resource(resource): def exit_without_resource(resource):
""" """
...@@ -178,15 +187,22 @@ def exit_without_resource(resource): ...@@ -178,15 +187,22 @@ def exit_without_resource(resource):
Exits with a status of 0 if the resource isn't enabled. Exits with a status of 0 if the resource isn't enabled.
""" """
from . import support
if not support.gevent_has_setup_resources:
setup_resources()
if not support.is_resource_enabled(resource): if not ensure_setup_resources().is_resource_enabled(resource):
print("Skipped: %r not enabled" % (resource,)) print("Skipped: %r not enabled" % (resource,))
import sys import sys
sys.exit(0) sys.exit(0)
def skip_without_resource(resource, reason=''):
requires = 'Requires resource %r' % (resource,)
if not reason:
reason = requires
else:
reason = reason + ' (' + requires + ')'
if not ensure_setup_resources().is_resource_enabled(resource):
import unittest
raise unittest.SkipTest(reason)
if __name__ == '__main__': if __name__ == '__main__':
print(setup_resources()) print(setup_resources())
...@@ -172,16 +172,11 @@ def skipWithoutResource(resource, reason=''): ...@@ -172,16 +172,11 @@ def skipWithoutResource(resource, reason=''):
else: else:
reason = reason + ' (' + requires + ')' reason = reason + ' (' + requires + ')'
# Defer until runtime; resources are established as part # Defer until runtime; resources are established as part
# of test startup. # of test startup.
def predicate(): # This is easily cached if needed def predicate(): # This is easily cached if needed
from . import support from . import resources
if not support.gevent_has_setup_resources: return resources.ensure_setup_resources().is_resource_enabled(resource)
# In this case, nothing to do but pull from the environment
from . import resources
resources.setup_resources()
return support.is_resource_enabled(resource)
return _make_runtime_skip_decorator(reason, predicate) return _make_runtime_skip_decorator(reason, predicate)
......
"""
Test the contents of the ``examples/`` directory.
If an existing test in *this* directory named ``test__example_<fn>.py`` exists,
where ``<fn>`` is the base filename of an example file, it will not be tested
here.
Examples can specify that they need particular test resources to be enabled
by commenting (one per line) ``# gevent-test-requires-resource: <resource>``;
most commonly the resource will be ``network``. You can use this technique to specify
non-existant resources for things that should never be tested.
"""
import re
import sys import sys
import os import os
import glob import glob
...@@ -9,23 +22,17 @@ from gevent.testing import util ...@@ -9,23 +22,17 @@ from gevent.testing import util
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
# XXX: Review these, check those that use external network resources
# and exclude if that resource is disabled.
def _find_files_to_ignore(): def _find_files_to_ignore():
old_dir = os.getcwd() old_dir = os.getcwd()
try: try:
os.chdir(this_dir) os.chdir(this_dir)
# These three are all tested with test___example_servers.
# TODO: Refactor those to regular test__example_*foo* files.
result = [ result = [
'wsgiserver.py', 'wsgiserver.py',
'wsgiserver_ssl.py', 'wsgiserver_ssl.py',
'webproxy.py', 'webproxy.py',
'webpy.py',
'unixsocket_server.py',
'unixsocket_client.py',
'psycopg2_pool.py',
'geventsendfile.py',
] ]
if greentest.PYPY and greentest.RUNNING_ON_APPVEYOR: if greentest.PYPY and greentest.RUNNING_ON_APPVEYOR:
# For some reason on Windows with PyPy, this times out, # For some reason on Windows with PyPy, this times out,
...@@ -48,7 +55,21 @@ class _AbstractTestMixin(util.ExampleMixin): ...@@ -48,7 +55,21 @@ class _AbstractTestMixin(util.ExampleMixin):
time_range = (2, 4) time_range = (2, 4)
filename = None filename = None
def _check_resources(self):
from gevent.testing import resources
with open(os.path.join(self.cwd, self.filename), 'r') as f:
contents = f.read()
pattern = re.compile('^# gevent-test-requires-resource: (.*)$', re.MULTILINE)
resources_needed = re.finditer(pattern, contents)
for match in resources_needed:
needed = contents[match.start(1):match.end(1)]
resources.skip_without_resource(needed)
def test_runs(self): def test_runs(self):
self._check_resources()
start = time.time() start = time.time()
min_time, max_time = self.time_range min_time, max_time = self.time_range
if not util.run([sys.executable, '-u', self.filename], if not util.run([sys.executable, '-u', self.filename],
...@@ -76,6 +97,7 @@ def _build_test_classes(): ...@@ -76,6 +97,7 @@ def _build_test_classes():
bn = os.path.basename(filename) bn = os.path.basename(filename)
if bn in ignore: if bn in ignore:
continue continue
tc = type( tc = type(
'Test_' + bn, 'Test_' + bn,
(_AbstractTestMixin, greentest.TestCase), (_AbstractTestMixin, greentest.TestCase),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment