Skip to content

Commit

Permalink
fix set_profile method
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Nov 21, 2023
1 parent 0b08aca commit b3cba07
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* JSONResponse Encoder arguments
* Generator responses can be partial
* Special PartialResponse class
* Fix set_profile method

==== 2.5.0 ====
* fix file callback on files smaller than 1000B
Expand Down
16 changes: 14 additions & 2 deletions examples/simple_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

# pylint: disable=import-error, wrong-import-position
from poorwsgi import Application, state # noqa
from poorwsgi.response import JSONResponse, JSONGeneratorResponse # noqa
from poorwsgi.request import parse_json_request # noqa
from poorwsgi.response import JSONResponse, JSONGeneratorResponse # noqa
from poorwsgi.request import parse_json_request # noqa

try:
import uwsgi # type: ignore
Expand All @@ -32,6 +32,12 @@
app = application = Application("JSON")
app.debug = True

PROFILE = os.environ.get("PROFILE", None)

if PROFILE is not None:
import cProfile
app.set_profile(cProfile.runctx, 'req')


@app.route('/test/json', method=state.METHOD_GET_POST)
def test_json(req):
Expand Down Expand Up @@ -61,6 +67,12 @@ def test_json_generator(req):
request=req.json)


@app.route('/profile')
def get_profile(req):
"""Returun PROFILE env variable"""
return JSONResponse(PROFILE=PROFILE)


@app.route('/timestamp')
def get_timestamp(req):
"""Return simple json with req.start_time timestamp"""
Expand Down
2 changes: 1 addition & 1 deletion poorwsgi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ def wrapper(rval):
uri_dump = (self.__dump + env.get('PATH_INFO').replace('/', '_') +
'.profile')
log.info('Generate %s', uri_dump)
self.__runctx('wrapper(rv)', globals(), locals(), filename=uri_dump)
self.__runctx('wrapper(rval)', globals(), locals(), filename=uri_dump)
return rval[0]
# enddef

Expand Down
13 changes: 7 additions & 6 deletions tests_integrity/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from time import sleep
from subprocess import Popen
from socket import socket, error as SocketError
from itertools import chain

from requests import Request, Session
from requests.exceptions import RequestException
Expand All @@ -14,25 +15,25 @@
from openapi_core.templating.paths.exceptions import PathNotFound


from . openapi import OpenAPIRequest, OpenAPIResponse


class TestError(RuntimeError):
"""Support exception."""


def start_server(request, example):
def start_server(request, example, env=None):
"""Start web server with example."""

process = None
print("Starting wsgi application...")
if request.config.getoption("--with-uwsgi"):
env = env or {}
env = [["--env", "=".join(items)] for items in env.items()]
env = list(chain.from_iterable(env))
process = Popen(["uwsgi", "--plugin", "python3",
"--http-socket", "localhost:8080", "--wsgi-file",
example])
example] + env)
else:
# pylint: disable=consider-using-with
process = Popen([executable, example])
process = Popen([executable, example], env=env)

assert process is not None
connect = False
Expand Down
38 changes: 38 additions & 0 deletions tests_integrity/test_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Integrity test for profiling JSON test/example application."""
from os import environ
from os.path import dirname, join, pardir

from pytest import fixture

from . support import start_server, check_url

# pylint: disable=missing-function-docstring
# pylint: disable=inconsistent-return-statements
# pylint: disable=redefined-outer-name
# pylint: disable=no-self-use
# pylint: disable=consider-using-f-string


@fixture(scope="module")
def server(request):
value = environ.get("TEST_SIMPLE_URL", "").strip('/')
if value:
return value

process = start_server(
request,
join(dirname(__file__), pardir, 'examples/simple_json.py'),
{"PROFILE": "1"})

yield "http://localhost:8080" # server is running
process.kill()
process.wait()


class TestRequest:
"""Request has some attributes."""
# pylint: disable=too-few-public-methods

def test_profile(self, server):
res = check_url(server+"/profile")
assert res.json()["PROFILE"] == "1"

0 comments on commit b3cba07

Please sign in to comment.