Skip to content

Commit

Permalink
Boost test speed by pytest-xdist
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Jan 3, 2018
1 parent 72254a7 commit a8827a5
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 31 deletions.
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re
import sanic


def pytest_collection_modifyitems(session, config, items):
base_port = sanic.testing.PORT

worker_id = getattr(config, 'slaveinput', {}).get('slaveid', 'master')
m = re.search(r'[0-9]+', worker_id)
if m:
num_id = int(m.group(0)) + 1
else:
num_id = 0
new_port = base_port + num_id

def new_test_client(app, port=new_port):
return sanic.testing.SanicTestClient(app, port)

sanic.Sanic.test_port = new_port
sanic.Sanic.test_client = property(new_test_client)

app = sanic.Sanic()
assert app.test_client.port == new_port
13 changes: 7 additions & 6 deletions tests/test_keep_alive_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sanic import server
import aiohttp
from aiohttp import TCPConnector
from sanic.testing import SanicTestClient, HOST, PORT
from sanic.testing import SanicTestClient, HOST


class ReuseableTCPConnector(TCPConnector):
Expand All @@ -30,7 +30,7 @@ def connect(self, req):

class ReuseableSanicTestClient(SanicTestClient):
def __init__(self, app, loop=None):
super(ReuseableSanicTestClient, self).__init__(app)
super().__init__(app, port=app.test_port)
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
Expand Down Expand Up @@ -68,13 +68,14 @@ async def _collect_response(loop):
import traceback
traceback.print_tb(e2.__traceback__)
exceptions.append(e2)
#Don't stop here! self.app.stop()
# Don't stop here! self.app.stop()

if self._server is not None:
_server = self._server
else:
_server_co = self.app.create_server(host=HOST, debug=debug,
port=PORT, **server_kwargs)
port=self.app.test_port,
**server_kwargs)

server.trigger_events(
self.app.listeners['before_server_start'], loop)
Expand All @@ -88,7 +89,7 @@ async def _collect_response(loop):
raise e1
self._server = _server = http_server
server.trigger_events(
self.app.listeners['after_server_start'], loop)
self.app.listeners['after_server_start'], loop)
self.app.listeners['after_server_start'].pop()

if do_kill_server:
Expand Down Expand Up @@ -133,7 +134,7 @@ async def _local_request(self, method, uri, cookies=None, *args,
url = uri
else:
url = 'http://{host}:{port}{uri}'.format(
host=HOST, port=PORT, uri=uri)
host=HOST, port=self.port, uri=uri)
do_kill_session = kwargs.pop('end_session', False)
if self._session:
session = self._session
Expand Down
3 changes: 1 addition & 2 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ async def conn_lost(request):
log = stream.getvalue()

if debug:
assert log.startswith(
'Connection lost before response written @')
assert 'Connection lost before response written @' in log
else:
assert 'Connection lost before response written @' not in log
4 changes: 2 additions & 2 deletions tests/test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import signal

from sanic import Sanic
from sanic.testing import HOST, PORT
from sanic.testing import HOST


def test_multiprocessing():
Expand All @@ -20,7 +20,7 @@ def stop_on_alarm(*args):

signal.signal(signal.SIGALRM, stop_on_alarm)
signal.alarm(1)
app.run(HOST, PORT, workers=num_workers)
app.run(HOST, app.test_port, workers=num_workers)

assert len(process_list) == num_workers

6 changes: 3 additions & 3 deletions tests/test_request_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sanic.config import Config
import aiohttp
from aiohttp import TCPConnector
from sanic.testing import SanicTestClient, HOST, PORT
from sanic.testing import SanicTestClient, HOST


class DelayableTCPConnector(TCPConnector):
Expand Down Expand Up @@ -96,7 +96,7 @@ def connect(self, req):

class DelayableSanicTestClient(SanicTestClient):
def __init__(self, app, loop, request_delay=1):
super(DelayableSanicTestClient, self).__init__(app)
super(DelayableSanicTestClient, self).__init__(app, port=app.test_port)
self._request_delay = request_delay
self._loop = None

Expand All @@ -108,7 +108,7 @@ async def _local_request(self, method, uri, cookies=None, *args,
url = uri
else:
url = 'http://{host}:{port}{uri}'.format(
host=HOST, port=PORT, uri=uri)
host=HOST, port=self.port, uri=uri)
conn = DelayableTCPConnector(pre_request_delay=self._request_delay,
verify_ssl=False, loop=self._loop)
async with aiohttp.ClientSession(cookies=cookies, connector=conn,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sanic.exceptions import ServerError
from sanic.response import json, text
from sanic.request import DEFAULT_HTTP_CONTENT_TYPE
from sanic.testing import HOST, PORT
from sanic.testing import HOST


# ------------------------------------------------------------ #
Expand Down Expand Up @@ -338,7 +338,7 @@ async def handler(request):
app.add_route(handler, path)

request, response = app.test_client.get(path + '?{}'.format(query))
assert request.url == expected_url.format(HOST, PORT)
assert request.url == expected_url.format(HOST, app.test_port)

parsed = urlparse(request.url)

Expand Down Expand Up @@ -369,9 +369,9 @@ async def handler(request):
app.add_route(handler, path)

request, response = app.test_client.get(
'https://{}:{}'.format(HOST, PORT) + path + '?{}'.format(query),
'https://{}:{}'.format(HOST, app.test_port) + path + '?{}'.format(query),
server_kwargs={'ssl': context})
assert request.url == expected_url.format(HOST, PORT)
assert request.url == expected_url.format(HOST, app.test_port)

parsed = urlparse(request.url)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from sanic import Sanic
from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream, json
from sanic.testing import HOST, PORT
from sanic.testing import HOST
from unittest.mock import MagicMock

JSON_DATA = {'ok': True}
Expand Down Expand Up @@ -139,7 +139,7 @@ async def run_stream(app, loop):

app.stop()

streaming_app.run(host=HOST, port=PORT)
streaming_app.run(host=HOST, port=streaming_app.test_port)


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/test_server_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from sanic import Sanic
from sanic.testing import HOST, PORT
from sanic.testing import HOST

AVAILABLE_LISTENERS = [
'before_server_start',
Expand All @@ -31,7 +31,7 @@ def stop_on_alarm(signum, frame):
signal.signal(signal.SIGALRM, stop_on_alarm)
signal.alarm(1)
try:
random_name_app.run(HOST, PORT, **run_kwargs)
random_name_app.run(HOST, random_name_app.test_port, **run_kwargs)
except KeyboardInterrupt:
pass

Expand Down
7 changes: 3 additions & 4 deletions tests/test_signal_handlers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from sanic import Sanic
from sanic.response import HTTPResponse
from sanic.testing import HOST, PORT
from sanic.testing import HOST
from unittest.mock import MagicMock
import pytest
import asyncio
from queue import Queue

Expand Down Expand Up @@ -31,7 +30,7 @@ async def hello_route(request):
app.listener('before_server_start')(set_loop)
app.listener('after_server_stop')(after)

app.run(HOST, PORT)
app.run(HOST, app.test_port)
assert calledq.get() == True


Expand All @@ -47,5 +46,5 @@ async def hello_route(request):
app.listener('before_server_start')(set_loop)
app.listener('after_server_stop')(after)

app.run(HOST, PORT, register_sys_signals=False)
app.run(HOST, app.test_port, register_sys_signals=False)
assert calledq.get() == False
14 changes: 9 additions & 5 deletions tests/test_url_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sanic.response import text
from sanic.views import HTTPMethodView
from sanic.blueprints import Blueprint
from sanic.testing import PORT as test_port, HOST as test_host
from sanic.testing import HOST as test_host
from sanic.exceptions import URLBuildError

import string
Expand All @@ -15,11 +15,11 @@
URL_FOR_ARGS2 = dict(arg1=['v1', 'v2'], _anchor='anchor')
URL_FOR_VALUE2 = '/myurl?arg1=v1&arg1=v2#anchor'
URL_FOR_ARGS3 = dict(arg1='v1', _anchor='anchor', _scheme='http',
_server='{}:{}'.format(test_host, test_port), _external=True)
URL_FOR_VALUE3 = 'http://{}:{}/myurl?arg1=v1#anchor'.format(test_host, test_port)
_server='{}:PORT_PLACEHOLDER'.format(test_host), _external=True)
URL_FOR_VALUE3 = 'http://{}:PORT_PLACEHOLDER/myurl?arg1=v1#anchor'.format(test_host)
URL_FOR_ARGS4 = dict(arg1='v1', _anchor='anchor', _external=True,
_server='http://{}:{}'.format(test_host, test_port),)
URL_FOR_VALUE4 = 'http://{}:{}/myurl?arg1=v1#anchor'.format(test_host, test_port)
_server='http://{}:PORT_PLACEHOLDER'.format(test_host),)
URL_FOR_VALUE4 = 'http://{}:PORT_PLACEHOLDER/myurl?arg1=v1#anchor'.format(test_host)


def _generate_handlers_from_names(app, l):
Expand Down Expand Up @@ -61,6 +61,10 @@ def test_simple_url_for_getting_with_more_params(args, url):
def passes(request):
return text('this should pass')

if '_server' in args:
args['_server'] = args['_server'].replace(
'PORT_PLACEHOLDER', str(app.test_port))
url = url.replace('PORT_PLACEHOLDER', str(app.test_port))
assert url == app.url_for('passes', **args)
request, response = app.test_client.get(url)
assert response.status == 200
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ deps =
pytest-cov
pytest-sanic
pytest-sugar
pytest-xdist
aiohttp==1.3.5
chardet<=2.3.0
beautifulsoup4
gunicorn
commands =
pytest tests --cov sanic --cov-report= {posargs}
pytest tests -n 4 --cov sanic --cov-report= {posargs}
- coverage combine --append
coverage report -m

Expand Down

0 comments on commit a8827a5

Please sign in to comment.