Commit c34abdc0 authored by Jason Madden's avatar Jason Madden

Let tests run without having gevent installed, just on pythonpath. Fixes #1409

parent c0c91a5d
...@@ -117,7 +117,10 @@ class BaseServer(object): ...@@ -117,7 +117,10 @@ class BaseServer(object):
# XXX: FIXME: Subclasses rely on the presence or absence of the # XXX: FIXME: Subclasses rely on the presence or absence of the
# `socket` attribute to determine whether we are open/should be opened. # `socket` attribute to determine whether we are open/should be opened.
# Instead, have it be None. # Instead, have it be None.
self.pool = None # XXX: In general, the state management here is confusing. Lots of stuff is
# deferred until the various ``set_`` methods are called, and it's not documented
# when it's safe to call those
self.pool = None # can be set from ``spawn``; overrides self.full()
try: try:
self.set_listener(listener) self.set_listener(listener)
self.set_spawn(spawn) self.set_spawn(spawn)
...@@ -250,9 +253,9 @@ class BaseServer(object): ...@@ -250,9 +253,9 @@ class BaseServer(object):
self.delay = min(self.max_delay, self.delay * 2) self.delay = min(self.max_delay, self.delay * 2)
break break
def full(self): def full(self): # pylint: disable=method-hidden
# copied from self.pool # If a Pool is given for to ``set_spawn`` (the *spawn* argument
# pylint: disable=method-hidden # of the constructor) it will replace this method.
return False return False
def __repr__(self): def __repr__(self):
......
This diff is collapsed.
...@@ -6,7 +6,7 @@ from gevent.testing import util ...@@ -6,7 +6,7 @@ from gevent.testing import util
from gevent.testing import params from gevent.testing import params
class Test(util.TestServer): class Test(util.TestServer):
server = 'echoserver.py' example = 'echoserver.py'
def _run_all_tests(self): def _run_all_tests(self):
def test_client(message): def test_client(message):
......
...@@ -13,9 +13,9 @@ from gevent.testing import util ...@@ -13,9 +13,9 @@ from gevent.testing import util
@greentest.skipOnLibuvOnCIOnPyPy("Timing issues sometimes lead to connection refused") @greentest.skipOnLibuvOnCIOnPyPy("Timing issues sometimes lead to connection refused")
class Test(util.TestServer): class Test(util.TestServer):
server = 'portforwarder.py' example = 'portforwarder.py'
# [listen on, forward to] # [listen on, forward to]
args = ['127.0.0.1:10011', '127.0.0.1:10012'] example_args = ['127.0.0.1:10011', '127.0.0.1:10012']
if greentest.WIN: if greentest.WIN:
from subprocess import CREATE_NEW_PROCESS_GROUP from subprocess import CREATE_NEW_PROCESS_GROUP
...@@ -40,7 +40,7 @@ class Test(util.TestServer): ...@@ -40,7 +40,7 @@ class Test(util.TestServer):
break break
log.append(data) log.append(data)
server = StreamServer(self.args[1], handle) server = StreamServer(self.example_args[1], handle)
server.start() server.start()
try: try:
conn = socket.create_connection(('127.0.0.1', 10011)) conn = socket.create_connection(('127.0.0.1', 10011))
......
from gevent import monkey from gevent import monkey
monkey.patch_all(subprocess=True) monkey.patch_all(subprocess=True)
import sys
from gevent.server import DatagramServer from gevent.server import DatagramServer
from gevent.testing.util import run
from gevent.testing import util from gevent.testing import util
from gevent.testing import main from gevent.testing import main
class Test_udp_client(util.TestServer): class Test_udp_client(util.TestServer):
start_kwargs = {'timeout': 10}
example = 'udp_client.py'
example_args = ['Test_udp_client']
def test(self): def test(self):
log = [] log = []
...@@ -20,8 +23,7 @@ class Test_udp_client(util.TestServer): ...@@ -20,8 +23,7 @@ class Test_udp_client(util.TestServer):
server = DatagramServer('127.0.0.1:9001', handle) server = DatagramServer('127.0.0.1:9001', handle)
server.start() server.start()
try: try:
run([sys.executable, '-W', 'ignore', '-u', 'udp_client.py', 'Test_udp_client'], self.run_example()
timeout=10, cwd=self.cwd)
finally: finally:
server.close() server.close()
self.assertEqual(log, [b'Test_udp_client']) self.assertEqual(log, [b'Test_udp_client'])
......
...@@ -5,7 +5,7 @@ from gevent.testing import main ...@@ -5,7 +5,7 @@ from gevent.testing import main
class Test(util.TestServer): class Test(util.TestServer):
server = 'udp_server.py' example = 'udp_server.py'
def _run_all_tests(self): def _run_all_tests(self):
sock = socket.socket(type=socket.SOCK_DGRAM) sock = socket.socket(type=socket.SOCK_DGRAM)
......
...@@ -9,7 +9,7 @@ from . import test__example_wsgiserver ...@@ -9,7 +9,7 @@ from . import test__example_wsgiserver
@greentest.skipOnCI("Timing issues sometimes lead to a connection refused") @greentest.skipOnCI("Timing issues sometimes lead to a connection refused")
@greentest.skipWithoutExternalNetwork("Tries to reach google.com") @greentest.skipWithoutExternalNetwork("Tries to reach google.com")
class Test_webproxy(test__example_wsgiserver.Test_wsgiserver): class Test_webproxy(test__example_wsgiserver.Test_wsgiserver):
server = 'webproxy.py' example = 'webproxy.py'
def _run_all_tests(self): def _run_all_tests(self):
status, data = self.read('/') status, data = self.read('/')
......
...@@ -16,7 +16,7 @@ from gevent.testing import params ...@@ -16,7 +16,7 @@ from gevent.testing import params
@greentest.skipOnCI("Timing issues sometimes lead to a connection refused") @greentest.skipOnCI("Timing issues sometimes lead to a connection refused")
class Test_wsgiserver(util.TestServer): class Test_wsgiserver(util.TestServer):
server = 'wsgiserver.py' example = 'wsgiserver.py'
URL = 'http://%s:8088' % (params.DEFAULT_LOCAL_HOST_ADDR,) URL = 'http://%s:8088' % (params.DEFAULT_LOCAL_HOST_ADDR,)
PORT = 8088 PORT = 8088
not_found_message = b'<h1>Not Found</h1>' not_found_message = b'<h1>Not Found</h1>'
......
...@@ -9,7 +9,7 @@ from . import test__example_wsgiserver ...@@ -9,7 +9,7 @@ from . import test__example_wsgiserver
@greentest.skipOnCI("Timing issues sometimes lead to a connection refused") @greentest.skipOnCI("Timing issues sometimes lead to a connection refused")
class Test_wsgiserver_ssl(test__example_wsgiserver.Test_wsgiserver): class Test_wsgiserver_ssl(test__example_wsgiserver.Test_wsgiserver):
server = 'wsgiserver_ssl.py' example = 'wsgiserver_ssl.py'
URL = 'https://%s:8443' % (params.DEFAULT_LOCAL_HOST_ADDR,) URL = 'https://%s:8443' % (params.DEFAULT_LOCAL_HOST_ADDR,)
PORT = 8443 PORT = 8443
_use_ssl = True _use_ssl = True
......
...@@ -11,7 +11,6 @@ most commonly the resource will be ``network``. You can use this technique to sp ...@@ -11,7 +11,6 @@ most commonly the resource will be ``network``. You can use this technique to sp
non-existant resources for things that should never be tested. non-existant resources for things that should never be tested.
""" """
import re import re
import sys
import os import os
import glob import glob
import time import time
...@@ -45,12 +44,12 @@ time_ranges = { ...@@ -45,12 +44,12 @@ time_ranges = {
class _AbstractTestMixin(util.ExampleMixin): class _AbstractTestMixin(util.ExampleMixin):
time_range = default_time_range time_range = default_time_range
filename = None example = None
def _check_resources(self): def _check_resources(self):
from gevent.testing import resources from gevent.testing import resources
with open(os.path.join(self.cwd, self.filename), 'r') as f: with open(os.path.join(self.cwd, self.example), 'r') as f:
contents = f.read() contents = f.read()
pattern = re.compile('^# gevent-test-requires-resource: (.*)$', re.MULTILINE) pattern = re.compile('^# gevent-test-requires-resource: (.*)$', re.MULTILINE)
...@@ -64,14 +63,15 @@ class _AbstractTestMixin(util.ExampleMixin): ...@@ -64,14 +63,15 @@ class _AbstractTestMixin(util.ExampleMixin):
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], self.start_kwargs = {
timeout=max_time, 'timeout': max_time,
cwd=self.cwd, 'quiet': True,
quiet=True, 'buffer_output': True,
buffer_output=True, 'nested': True,
nested=True, 'setenv': {'GEVENT_DEBUG': 'error'}
setenv={'GEVENT_DEBUG': 'error'}): }
self.fail("Failed example: " + self.filename) if not self.run_example():
self.fail("Failed example: " + self.example)
else: else:
took = time.time() - start took = time.time() - start
self.assertGreaterEqual(took, min_time) self.assertGreaterEqual(took, min_time)
...@@ -94,7 +94,7 @@ def _build_test_classes(): ...@@ -94,7 +94,7 @@ def _build_test_classes():
'Test_' + bn, 'Test_' + bn,
(_AbstractTestMixin, greentest.TestCase), (_AbstractTestMixin, greentest.TestCase),
{ {
'filename': bn, 'example': bn,
'time_range': time_ranges.get(bn, _AbstractTestMixin.time_range) 'time_range': time_ranges.get(bn, _AbstractTestMixin.time_range)
} }
) )
......
...@@ -13,15 +13,15 @@ import os ...@@ -13,15 +13,15 @@ import os
import os.path import os.path
import sys import sys
from subprocess import Popen
from subprocess import PIPE
from gevent import testing as greentest from gevent import testing as greentest
from gevent.testing.util import absolute_pythonpath
from gevent.testing.util import run
class TestRun(greentest.TestCase): class TestRun(greentest.TestCase):
maxDiff = None maxDiff = None
def setUp(self): def setUp(self):
self.abs_pythonpath = absolute_pythonpath() # before we cd
self.cwd = os.getcwd() self.cwd = os.getcwd()
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(__file__))
...@@ -31,29 +31,42 @@ class TestRun(greentest.TestCase): ...@@ -31,29 +31,42 @@ class TestRun(greentest.TestCase):
def _run(self, script, module=False): def _run(self, script, module=False):
env = os.environ.copy() env = os.environ.copy()
env['PYTHONWARNINGS'] = 'ignore' env['PYTHONWARNINGS'] = 'ignore'
if self.abs_pythonpath:
env['PYTHONPATH'] = self.abs_pythonpath
run_kwargs = dict(
buffer_output=True,
quiet=True,
nested=True,
env=env,
timeout=10,
)
args = [sys.executable, '-m', 'gevent.monkey'] args = [sys.executable, '-m', 'gevent.monkey']
if module: if module:
args.append('--module') args.append('--module')
args += [script, 'patched'] args += [script, 'patched']
p = Popen(args, stdout=PIPE, stderr=PIPE, env=env) monkey_result = run(
monkey_out, monkey_err = p.communicate() args,
self.assertEqual(0, p.returncode, (p.returncode, monkey_out, monkey_err)) **run_kwargs
)
self.assertTrue(monkey_result)
if module: if module:
args = [sys.executable, "-m", script, 'stdlib'] args = [sys.executable, "-m", script, 'stdlib']
else: else:
args = [sys.executable, script, 'stdlib'] args = [sys.executable, script, 'stdlib']
p = Popen(args, stdout=PIPE, stderr=PIPE) std_result = run(
args,
std_out, std_err = p.communicate() **run_kwargs
self.assertEqual(0, p.returncode, (p.returncode, std_out, std_err)) )
self.assertTrue(std_result)
monkey_out_lines = monkey_out.decode("utf-8").splitlines()
std_out_lines = std_out.decode('utf-8').splitlines() monkey_out_lines = monkey_result.output_lines
std_out_lines = std_result.output_lines
self.assertEqual(monkey_out_lines, std_out_lines) self.assertEqual(monkey_out_lines, std_out_lines)
self.assertEqual(monkey_err, std_err) self.assertEqual(monkey_result.error, std_result.error)
return monkey_out_lines, monkey_err return monkey_out_lines, monkey_result.error
def test_run_simple(self): def test_run_simple(self):
self._run(os.path.join('monkey_package', 'script.py')) self._run(os.path.join('monkey_package', 'script.py'))
...@@ -61,8 +74,8 @@ class TestRun(greentest.TestCase): ...@@ -61,8 +74,8 @@ class TestRun(greentest.TestCase):
def _run_package(self, module): def _run_package(self, module):
lines, _ = self._run('monkey_package', module=module) lines, _ = self._run('monkey_package', module=module)
self.assertTrue(lines[0].endswith('__main__.py'), lines[0]) self.assertTrue(lines[0].endswith(u'__main__.py'), lines[0])
self.assertEqual(lines[1], '__main__') self.assertEqual(lines[1].strip(), u'__main__')
def test_run_package(self): def test_run_package(self):
# Run a __main__ inside a package, even without specifying -m # Run a __main__ inside a package, even without specifying -m
...@@ -75,10 +88,10 @@ class TestRun(greentest.TestCase): ...@@ -75,10 +88,10 @@ class TestRun(greentest.TestCase):
def test_issue_302(self): def test_issue_302(self):
lines, _ = self._run(os.path.join('monkey_package', 'issue302monkey.py')) lines, _ = self._run(os.path.join('monkey_package', 'issue302monkey.py'))
self.assertEqual(lines[0], 'True') self.assertEqual(lines[0].strip(), u'True')
lines[1] = lines[1].replace('\\', '/') # windows path lines[1] = lines[1].replace(u'\\', u'/') # windows path
self.assertEqual(lines[1], 'monkey_package/issue302monkey.py') self.assertEqual(lines[1].strip(), u'monkey_package/issue302monkey.py')
self.assertEqual(lines[2], 'True', lines) self.assertEqual(lines[2].strip(), u'True', lines)
# These three tests all sometimes fail on Py2 on CI, writing # These three tests all sometimes fail on Py2 on CI, writing
# to stderr: # to stderr:
......
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