[Python-modules-commits] [pytest-tornado] 01/02: Import pytest-tornado_0.4.5.orig.tar.gz

Ondřej Nový onovy-guest at moszumanska.debian.org
Sun Mar 6 20:54:08 UTC 2016


This is an automated email from the git hooks/post-receive script.

onovy-guest pushed a commit to branch master
in repository pytest-tornado.

commit 82fc256d8ace82efa1b72028ad2b011d0540c151
Author: Ondřej Nový <novy at ondrej.org>
Date:   Sun Mar 6 21:21:31 2016 +0100

    Import pytest-tornado_0.4.5.orig.tar.gz
---
 .travis.yml                             | 15 +++++++++
 Makefile                                | 15 +++++++++
 README.rst                              |  9 ++++++
 pytest_tornado/plugin.py                | 56 +++++++++++++++++++++++++++++----
 pytest_tornado/test/conftest.py         |  6 ++++
 pytest_tornado/test/test_async.py       |  7 +++++
 pytest_tornado/test/test_async_await.py | 18 +++++++++++
 setup.py                                |  4 +--
 8 files changed, 122 insertions(+), 8 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index e3f0f6a..645681d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,7 +2,10 @@ language: python
 python:
     - 2.7
     - 3.4
+    - 3.5
 env:
+    - TORNADO_VERSION=4.3 PYTEST_VERSION=2.8.6
+    - TORNADO_VERSION=4.1 PYTEST_VERSION=2.8.6
     - TORNADO_VERSION=4.1 PYTEST_VERSION=2.7.0
     - TORNADO_VERSION=4.1 PYTEST_VERSION=2.6.4
     - TORNADO_VERSION=3.2.2 PYTEST_VERSION=2.6.4
@@ -11,6 +14,18 @@ install:
     - pip install -q tornado==$TORNADO_VERSION pytest==$PYTEST_VERSION
     - python setup.py install
     - pip install pytest-cov coveralls
+matrix:
+  exclude:
+    - python: 3.5
+      env: TORNADO_VERSION=3.2.2 PYTEST_VERSION=2.6.4
+    - python: 3.5
+      env: TORNADO_VERSION=3.2.2 PYTEST_VERSION=2.5.2
+    - python: 3.5
+      env: TORNADO_VERSION=4.1 PYTEST_VERSION=2.6.4
+    - python: 3.5
+      env: TORNADO_VERSION=4.1 PYTEST_VERSION=2.5.2
+    - python: 3.5
+      env: TORNADO_VERSION=4.1 PYTEST_VERSION=2.7.0
 script:
     - py.test --strict --cov=pytest_tornado/plugin.py --cov-report=term-missing
 after_success:
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..abccc9d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,15 @@
+.PHONY: build clean upload test-upload
+
+default: build
+
+build:
+	python setup.py sdist bdist_wheel
+
+clean:
+	rm -rf build dist *.egg-info
+
+upload: clean build
+	twine upload dist/*
+
+test-upload: clean build
+	twine upload -r pypitest dist/*
diff --git a/README.rst b/README.rst
index 16b925b..9af2af7 100644
--- a/README.rst
+++ b/README.rst
@@ -102,6 +102,15 @@ setting an ``ASYNC_TEST_TIMEOUT`` environment variable,
     def test_tornado(http_client):
         yield http_client.fetch('http://www.tornadoweb.org/')
 
+The mark can also receive a run_sync flag, which if turned off will, instead of running the test synchronously, will add it as a coroutine and run the IOLoop (until the timeout). For instance, this allows to test things on both a client and a server at the same time. 
+
+.. code-block:: python
+
+    @pytest.mark.gen_test(run_sync=False)
+    def test_tornado(http_server, http_client):
+        response = yield http_client.fetch('http://localhost:5555/my_local_server_test/')
+        assert response.body == 'Run on the same IOLoop!'
+
 
 Show markers provided by the plugin::
 
diff --git a/pytest_tornado/plugin.py b/pytest_tornado/plugin.py
index 21a6f62..8c1bd49 100644
--- a/pytest_tornado/plugin.py
+++ b/pytest_tornado/plugin.py
@@ -1,7 +1,8 @@
 import os
+import sys
 import types
 import inspect
-import functools
+import datetime
 import pytest
 import tornado
 import tornado.gen
@@ -9,6 +10,31 @@ import tornado.testing
 import tornado.httpserver
 import tornado.httpclient
 
+if sys.version_info[:2] >= (3, 5):
+    iscoroutinefunction = inspect.iscoroutinefunction
+else:
+    iscoroutinefunction = lambda f: False
+
+try:
+    with_timeout = tornado.gen.with_timeout
+except AttributeError:
+    from tornado.ioloop import IOLoop
+    from tornado.concurrent import Future, chain_future
+
+    # simplified version of 'with_timeout' from tornado 4.0
+    # to work with tornado 3
+    def with_timeout(timeout, future, io_loop=None):
+        result = Future()
+        chain_future(future, result)
+        if io_loop is None:
+            io_loop = IOLoop.current()
+        timeout_handle = io_loop.add_timeout(
+            timeout,
+            lambda: result.set_exception(TimeoutError("Timeout")))
+        future.add_done_callback(
+            lambda future: io_loop.remove_timeout(timeout_handle))
+        return result
+
 
 def _get_async_test_timeout():
     try:
@@ -66,15 +92,33 @@ def pytest_runtest_setup(item):
 
 @pytest.mark.tryfirst
 def pytest_pyfunc_call(pyfuncitem):
-    if 'gen_test' in pyfuncitem.keywords:
+    gen_test_mark = pyfuncitem.keywords.get('gen_test')
+    if gen_test_mark:
         io_loop = pyfuncitem.funcargs.get('io_loop')
+        run_sync = gen_test_mark.kwargs.get('run_sync', True)
 
         funcargs = dict((arg, pyfuncitem.funcargs[arg])
                         for arg in _argnames(pyfuncitem.obj))
-
-        coroutine = tornado.gen.coroutine(pyfuncitem.obj)
-        io_loop.run_sync(functools.partial(coroutine, **funcargs),
-                         timeout=_timeout(pyfuncitem))
+        if iscoroutinefunction(pyfuncitem.obj):
+            coroutine = pyfuncitem.obj
+            future = tornado.gen.convert_yielded(coroutine(**funcargs))
+        else:
+            coroutine = tornado.gen.coroutine(pyfuncitem.obj)
+            future = coroutine(**funcargs)
+        if run_sync:
+            io_loop.run_sync(lambda: future, timeout=_timeout(pyfuncitem))
+        else:
+            # Run this test function as a coroutine, until the timeout. When completed, stop the IOLoop
+            # and reraise any exceptions
+
+            future_with_timeout = with_timeout(
+                    datetime.timedelta(seconds=_timeout(pyfuncitem)),
+                    future)
+            io_loop.add_future(future_with_timeout, lambda f: io_loop.stop())
+            io_loop.start()
+
+            # This will reraise any exceptions that occurred.
+            future_with_timeout.result()
 
         # prevent other pyfunc calls from executing
         return True
diff --git a/pytest_tornado/test/conftest.py b/pytest_tornado/test/conftest.py
new file mode 100644
index 0000000..9599c8f
--- /dev/null
+++ b/pytest_tornado/test/conftest.py
@@ -0,0 +1,6 @@
+import sys
+import tornado
+
+collect_ignore = []
+if sys.version_info[:2] < (3, 5) or tornado.version_info[:2] < (4, 3):
+    collect_ignore.append("test_async_await.py")
diff --git a/pytest_tornado/test/test_async.py b/pytest_tornado/test/test_async.py
index cd70534..53a8e60 100644
--- a/pytest_tornado/test/test_async.py
+++ b/pytest_tornado/test/test_async.py
@@ -1,5 +1,6 @@
 import functools
 import pytest
+import tornado
 from tornado import gen
 from tornado.ioloop import TimeoutError
 
@@ -34,6 +35,12 @@ def test_gen_test(io_loop):
     assert result
 
 
+ at pytest.mark.gen_test(run_sync=False)
+def test_gen_test_run_sync_false(io_loop):
+    result = yield dummy_coroutine(io_loop)
+    assert result
+
+
 @pytest.mark.gen_test
 def test_gen_test_swallows_exceptions(io_loop):
     with pytest.raises(ZeroDivisionError):
diff --git a/pytest_tornado/test/test_async_await.py b/pytest_tornado/test/test_async_await.py
new file mode 100644
index 0000000..61e7dde
--- /dev/null
+++ b/pytest_tornado/test/test_async_await.py
@@ -0,0 +1,18 @@
+import pytest
+from tornado import gen
+
+async def dummy_native_coroutine(io_loop):
+    await gen.Task(io_loop.add_callback)
+    return True
+
+
+ at pytest.mark.gen_test
+async def test_native_coroutine_gen_test(io_loop):
+    result = await dummy_native_coroutine(io_loop)
+    assert result
+
+
+ at pytest.mark.gen_test(run_sync=False)
+async def test_native_coroutine_run_sync_false(io_loop):
+    result = await dummy_native_coroutine(io_loop)
+    assert result
diff --git a/setup.py b/setup.py
index 6e3561c..2c30f92 100644
--- a/setup.py
+++ b/setup.py
@@ -11,7 +11,7 @@ with io.open(os.path.join(cwd, 'README.rst'), encoding='utf-8') as fd:
 
 setup(
     name='pytest-tornado',
-    version='0.4.4',
+    version='0.4.5',
     description=('A py.test plugin providing fixtures and markers '
                  'to simplify testing of asynchronous tornado applications.'),
     long_description=long_description,
@@ -20,7 +20,7 @@ setup(
     author_email='burump at gmail.com',
     license='Apache License, Version 2.0',
     classifiers=[
-        'Development Status :: 4 - Beta',
+        'Development Status :: 5 - Production/Stable',
         'Environment :: Plugins',
         'Intended Audience :: Developers',
         'License :: OSI Approved :: Apache Software License',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/pytest-tornado.git



More information about the Python-modules-commits mailing list