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