Commit 7f0cda6e authored by Denis Bilenko's avatar Denis Bilenko

test__examples.py: add minimal time check

parent d59b9103
......@@ -20,6 +20,7 @@ from gevent.server import DatagramServer, StreamServer
examples_directory = normpath(join(dirname(abspath(__file__)), '..', 'examples'))
examples = [basename(x) for x in glob.glob(examples_directory + '/*.py')]
simple_examples = []
default_time_range = (0.0, 10.0)
for example in examples:
......@@ -27,6 +28,18 @@ for example in examples:
simple_examples.append(example)
class TestCase(unittest.TestCase):
def run_script(self, *args, **kwargs):
cmd = [sys.executable, join(examples_directory, self.path)] + list(args)
start = time()
subprocess.check_call(cmd, **kwargs)
took = time() - start
min_time, max_time = getattr(self, 'time_range', default_time_range)
assert took >= min_time, '%s exited too quickly %s %s' % (self.path, took, min_time)
assert took <= max_time, '%s takes way too long %s %s' % (self.path, took, max_time)
def make_test(path):
if sys.platform == 'win32' and os.path.basename(path) in ('geventsendfile.py', 'processes.py'):
......@@ -36,25 +49,17 @@ def make_test(path):
if ' ' in path:
path = '"%s"' % path
class Test(unittest.TestCase):
class Test(TestCase):
def test(self):
run_script(self.path)
self.run_script()
Test.__name__ = 'Test_' + basename(path).split('.')[0]
assert Test.__name__ not in globals(), Test.__name__
Test.path = path
return Test
def run_script(path, *args):
cmd = [sys.executable, join(examples_directory, path)] + list(args)
popen = subprocess.Popen(cmd)
if popen.wait() != 0:
raise AssertionError('%r failed with code %s' % (cmd, popen.wait()))
def kill(popen):
try:
popen.kill()
......@@ -221,7 +226,7 @@ class Test_echoserver(BaseTestServer):
gevent.joinall([client1, client2], raise_error=True)
class Test_udp_client(unittest.TestCase):
class Test_udp_client(TestCase):
path = 'udp_client.py'
......@@ -233,7 +238,7 @@ class Test_udp_client(unittest.TestCase):
server = DatagramServer('127.0.0.1:9000', handle)
server.start()
try:
run_script(self.path, 'Test_udp_client')
self.run_script('Test_udp_client')
finally:
server.close()
self.assertEqual(log, ['Test_udp_client'])
......@@ -306,6 +311,10 @@ for example in simple_examples:
del test
Test_psycopg2_pool.time_range = (2.0, 2.5)
Test_threadpool.time_range = (2.0, 3.0)
class TestAllTested(unittest.TestCase):
def test(self):
......
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