fix hang build

This commit is contained in:
Yun Xu 2018-03-15 21:28:52 -07:00
parent 31cf83f10b
commit d42cb7ddb3
11 changed files with 24 additions and 161 deletions

View File

@ -1,23 +0,0 @@
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

View File

@ -1,108 +0,0 @@
import os
import sys
import subprocess
import signal
from threading import Thread
from time import sleep
from json.decoder import JSONDecodeError
import aiohttp
import asyncio
import async_timeout
sanic_project_content_one = '''
from sanic import Sanic
from sanic import response
app = Sanic(__name__)
@app.route("/")
async def test(request):
return response.json({"test": 1})
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8000, auto_reload=True)
'''
sanic_project_content_two = '''
from sanic import Sanic
from sanic import response
app = Sanic(__name__)
@app.route("/")
async def test(request):
return response.json({"test": 2})
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8000, auto_reload=True)
'''
process_id = None
def execute_cmd(command):
process = subprocess.Popen(command, shell=True)
global process_id
process_id = process.pid
process.communicate()
class TestAutoReloading:
def check_response(self, url, response):
"""Send http request and tries to take it's response as json.
Returns a dictionary.
"""
async def req(url, excepted_response):
async with aiohttp.ClientSession() as session:
with async_timeout.timeout(10):
async with session.get(url) as response:
try:
result = await response.json()
except JSONDecodeError:
result = {}
return result == excepted_response
loop = asyncio.get_event_loop()
return loop.run_until_complete(req(url, response))
def test_reloading_after_change_file(self, capsys):
if os.name != 'posix':
return
with capsys.disabled():
pass
sanic_app_file_path = "simple_sanic_app.py"
with open(sanic_app_file_path, "w") as _file:
_file.write(sanic_project_content_one)
cmd = ' '.join([sys.executable, sanic_app_file_path])
thread = Thread(target=execute_cmd, args=(cmd,))
thread.start()
sleep(2) # wait for completing server start process
assert self.check_response("http://127.0.0.1:8000/", {"test": 1})
with open(sanic_app_file_path, "w") as _file:
_file.write(sanic_project_content_two)
sleep(2) # wait for completing server start process
assert self.check_response("http://127.0.0.1:8000/", {"test": 2})
thread.join(1)
os.remove(sanic_app_file_path)
def teardown_method(self, method):
if process_id:
root_proc_path = \
"/proc/{pid}/task/{pid}/children".format(pid=process_id)
if not os.path.isfile(root_proc_path):
return
with open(root_proc_path) as children_list_file:
children_list_pid = children_list_file.read().split()
for child_pid in children_list_pid:
os.kill(int(child_pid), signal.SIGTERM)

View File

@ -7,7 +7,7 @@ from sanic.config import Config
from sanic import server from sanic import server
import aiohttp import aiohttp
from aiohttp import TCPConnector from aiohttp import TCPConnector
from sanic.testing import SanicTestClient, HOST from sanic.testing import SanicTestClient, HOST, PORT
class ReuseableTCPConnector(TCPConnector): class ReuseableTCPConnector(TCPConnector):
@ -43,7 +43,7 @@ class ReuseableTCPConnector(TCPConnector):
class ReuseableSanicTestClient(SanicTestClient): class ReuseableSanicTestClient(SanicTestClient):
def __init__(self, app, loop=None): def __init__(self, app, loop=None):
super().__init__(app, port=app.test_port) super(ReuseableSanicTestClient, self).__init__(app)
if loop is None: if loop is None:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
self._loop = loop self._loop = loop
@ -87,8 +87,7 @@ class ReuseableSanicTestClient(SanicTestClient):
_server = self._server _server = self._server
else: else:
_server_co = self.app.create_server(host=HOST, debug=debug, _server_co = self.app.create_server(host=HOST, debug=debug,
port=self.app.test_port, port=PORT, **server_kwargs)
**server_kwargs)
server.trigger_events( server.trigger_events(
self.app.listeners['before_server_start'], loop) self.app.listeners['before_server_start'], loop)

View File

@ -3,7 +3,7 @@ import random
import signal import signal
from sanic import Sanic from sanic import Sanic
from sanic.testing import HOST from sanic.testing import HOST, PORT
def test_multiprocessing(): def test_multiprocessing():
@ -20,6 +20,6 @@ def test_multiprocessing():
signal.signal(signal.SIGALRM, stop_on_alarm) signal.signal(signal.SIGALRM, stop_on_alarm)
signal.alarm(1) signal.alarm(1)
app.run(HOST, app.test_port, workers=num_workers) app.run(HOST, PORT, workers=num_workers)
assert len(process_list) == num_workers assert len(process_list) == num_workers

View File

@ -6,7 +6,7 @@ from sanic.response import text
from sanic.config import Config from sanic.config import Config
import aiohttp import aiohttp
from aiohttp import TCPConnector from aiohttp import TCPConnector
from sanic.testing import SanicTestClient, HOST from sanic.testing import SanicTestClient, HOST, PORT
class DelayableTCPConnector(TCPConnector): class DelayableTCPConnector(TCPConnector):
@ -108,7 +108,7 @@ class DelayableTCPConnector(TCPConnector):
class DelayableSanicTestClient(SanicTestClient): class DelayableSanicTestClient(SanicTestClient):
def __init__(self, app, loop, request_delay=1): def __init__(self, app, loop, request_delay=1):
super(DelayableSanicTestClient, self).__init__(app, port=app.test_port) super(DelayableSanicTestClient, self).__init__(app)
self._request_delay = request_delay self._request_delay = request_delay
self._loop = None self._loop = None

View File

@ -9,7 +9,7 @@ from sanic import Sanic
from sanic.exceptions import ServerError from sanic.exceptions import ServerError
from sanic.response import json, text from sanic.response import json, text
from sanic.request import DEFAULT_HTTP_CONTENT_TYPE from sanic.request import DEFAULT_HTTP_CONTENT_TYPE
from sanic.testing import HOST from sanic.testing import HOST, PORT
# ------------------------------------------------------------ # # ------------------------------------------------------------ #
@ -340,7 +340,7 @@ def test_url_attributes_no_ssl(path, query, expected_url):
app.add_route(handler, path) app.add_route(handler, path)
request, response = app.test_client.get(path + '?{}'.format(query)) request, response = app.test_client.get(path + '?{}'.format(query))
assert request.url == expected_url.format(HOST, app.test_port) assert request.url == expected_url.format(HOST, PORT)
parsed = urlparse(request.url) parsed = urlparse(request.url)
@ -371,9 +371,9 @@ def test_url_attributes_with_ssl(path, query, expected_url):
app.add_route(handler, path) app.add_route(handler, path)
request, response = app.test_client.get( request, response = app.test_client.get(
'https://{}:{}'.format(HOST, app.test_port) + path + '?{}'.format(query), 'https://{}:{}'.format(HOST, PORT) + path + '?{}'.format(query),
server_kwargs={'ssl': context}) server_kwargs={'ssl': context})
assert request.url == expected_url.format(HOST, app.test_port) assert request.url == expected_url.format(HOST, PORT)
parsed = urlparse(request.url) parsed = urlparse(request.url)

View File

@ -10,7 +10,7 @@ from random import choice
from sanic import Sanic from sanic import Sanic
from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream, json from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream, json
from sanic.testing import HOST from sanic.testing import HOST, PORT
from unittest.mock import MagicMock from unittest.mock import MagicMock
JSON_DATA = {'ok': True} JSON_DATA = {'ok': True}
@ -187,7 +187,7 @@ def test_stream_response_writes_correct_content_to_transport(streaming_app):
app.stop() app.stop()
streaming_app.run(host=HOST, port=streaming_app.test_port) streaming_app.run(host=HOST, port=PORT)
@pytest.fixture @pytest.fixture

View File

@ -6,7 +6,7 @@ import signal
import pytest import pytest
from sanic import Sanic from sanic import Sanic
from sanic.testing import HOST from sanic.testing import HOST, PORT
AVAILABLE_LISTENERS = [ AVAILABLE_LISTENERS = [
'before_server_start', 'before_server_start',
@ -31,7 +31,7 @@ def start_stop_app(random_name_app, **run_kwargs):
signal.signal(signal.SIGALRM, stop_on_alarm) signal.signal(signal.SIGALRM, stop_on_alarm)
signal.alarm(1) signal.alarm(1)
try: try:
random_name_app.run(HOST, random_name_app.test_port, **run_kwargs) random_name_app.run(HOST, PORT, **run_kwargs)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass

View File

@ -1,6 +1,6 @@
from sanic import Sanic from sanic import Sanic
from sanic.response import HTTPResponse from sanic.response import HTTPResponse
from sanic.testing import HOST from sanic.testing import HOST, PORT
from unittest.mock import MagicMock from unittest.mock import MagicMock
import asyncio import asyncio
from queue import Queue from queue import Queue
@ -30,7 +30,7 @@ def test_register_system_signals():
app.listener('before_server_start')(set_loop) app.listener('before_server_start')(set_loop)
app.listener('after_server_stop')(after) app.listener('after_server_stop')(after)
app.run(HOST, app.test_port) app.run(HOST, PORT)
assert calledq.get() == True assert calledq.get() == True
@ -46,5 +46,5 @@ def test_dont_register_system_signals():
app.listener('before_server_start')(set_loop) app.listener('before_server_start')(set_loop)
app.listener('after_server_stop')(after) app.listener('after_server_stop')(after)
app.run(HOST, app.test_port, register_sys_signals=False) app.run(HOST, PORT, register_sys_signals=False)
assert calledq.get() == False assert calledq.get() == False

View File

@ -5,7 +5,7 @@ from sanic import Sanic
from sanic.response import text from sanic.response import text
from sanic.views import HTTPMethodView from sanic.views import HTTPMethodView
from sanic.blueprints import Blueprint from sanic.blueprints import Blueprint
from sanic.testing import HOST as test_host from sanic.testing import PORT as test_port, HOST as test_host
from sanic.exceptions import URLBuildError from sanic.exceptions import URLBuildError
import string import string
@ -15,11 +15,11 @@ URL_FOR_VALUE1 = '/myurl?arg1=v1&arg1=v2'
URL_FOR_ARGS2 = dict(arg1=['v1', 'v2'], _anchor='anchor') URL_FOR_ARGS2 = dict(arg1=['v1', 'v2'], _anchor='anchor')
URL_FOR_VALUE2 = '/myurl?arg1=v1&arg1=v2#anchor' URL_FOR_VALUE2 = '/myurl?arg1=v1&arg1=v2#anchor'
URL_FOR_ARGS3 = dict(arg1='v1', _anchor='anchor', _scheme='http', URL_FOR_ARGS3 = dict(arg1='v1', _anchor='anchor', _scheme='http',
_server='{}:PORT_PLACEHOLDER'.format(test_host), _external=True) _server='{}:{}'.format(test_host, test_port), _external=True)
URL_FOR_VALUE3 = 'http://{}:PORT_PLACEHOLDER/myurl?arg1=v1#anchor'.format(test_host) URL_FOR_VALUE3 = 'http://{}:{}/myurl?arg1=v1#anchor'.format(test_host, test_port)
URL_FOR_ARGS4 = dict(arg1='v1', _anchor='anchor', _external=True, URL_FOR_ARGS4 = dict(arg1='v1', _anchor='anchor', _external=True,
_server='http://{}:PORT_PLACEHOLDER'.format(test_host),) _server='http://{}:{}'.format(test_host, test_port))
URL_FOR_VALUE4 = 'http://{}:PORT_PLACEHOLDER/myurl?arg1=v1#anchor'.format(test_host) URL_FOR_VALUE4 = 'http://{}:{}/myurl?arg1=v1#anchor'.format(test_host, test_port)
def _generate_handlers_from_names(app, l): def _generate_handlers_from_names(app, l):
@ -61,10 +61,6 @@ def test_simple_url_for_getting_with_more_params(args, url):
def passes(request): def passes(request):
return text('this should pass') 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) assert url == app.url_for('passes', **args)
request, response = app.test_client.get(url) request, response = app.test_client.get(url)
assert response.status == 200 assert response.status == 200

View File

@ -12,13 +12,12 @@ deps =
pytest-cov pytest-cov
pytest-sanic pytest-sanic
pytest-sugar pytest-sugar
pytest-xdist
aiohttp>=2.3 aiohttp>=2.3
chardet<=2.3.0 chardet<=2.3.0
beautifulsoup4 beautifulsoup4
gunicorn gunicorn
commands = commands =
pytest tests -n 4 --cov sanic --cov-report= {posargs} pytest tests --cov sanic --cov-report= {posargs}
- coverage combine --append - coverage combine --append
coverage report -m coverage report -m