Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: run uwsgi tests in virtual environment #191

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ pycparser
# Support tests
psutil
requests
uwsgi; sys_platform != 'win32'
1 change: 1 addition & 0 deletions test/support/requirements-uwsgi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uwsgi; sys_platform != 'win32'
71 changes: 51 additions & 20 deletions test/support/test_uwsgi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import os
import sys
from contextlib import contextmanager
from pathlib import Path
from subprocess import PIPE
from subprocess import Popen
from subprocess import check_output
from tempfile import TemporaryDirectory
from test.utils import allpythons
from test.utils import austin
from test.utils import compress
from test.utils import has_pattern
from test.utils import requires_sudo
from test.utils import run_python
from test.utils import threads
from threading import Thread
from time import sleep
Expand All @@ -24,7 +29,7 @@


@contextmanager
def uwsgi(app="app.py", port=9090, args=[]):
def uwsgi(app="app.py", port=9090, args=[], env=None):
with Popen(
[
"uwsgi",
Expand All @@ -36,6 +41,7 @@ def uwsgi(app="app.py", port=9090, args=[]):
],
stdout=PIPE,
stderr=PIPE,
env=env or os.environ,
) as uw:
sleep(0.5)

Expand All @@ -55,35 +61,60 @@ def uwsgi(app="app.py", port=9090, args=[]):
proc.wait()


@contextmanager
def venv(py, reqs):
with TemporaryDirectory() as tmp_path:
venv_path = Path(tmp_path) / ".venv"
p = run_python(py, "-m", "venv", str(venv_path))
p.wait(120)
assert 0 == p.returncode, "Virtual environment was created successfully"

env = os.environ.copy()
env["LD_LIBRARY_PATH"] = str(venv_path / "lib")
env["PATH"] = str(venv_path / "bin") + os.pathsep + env["PATH"]

check_output(
["python", "-m", "pip", "install", "-r", reqs, "--use-pep517"], env=env
)

yield env


@requires_sudo
def test_uwsgi():
@allpythons()
def test_uwsgi(py):
request_thread = Thread(target=get, args=("http://localhost:9090",))

with uwsgi() as uw:
request_thread.start()
with venv(py, reqs=Path(__file__).parent / "requirements-uwsgi.txt") as env:
with uwsgi(env=env) as uw:
request_thread.start()

result = austin("-x", "2", "-Cp", str(uw.pid))
assert has_pattern(result.stdout, "app.py:application:5"), compress(
result.stdout
)
result = austin("-x", "2", "-Cp", str(uw.pid))
assert has_pattern(result.stdout, "app.py:application:5"), compress(
result.stdout
)

request_thread.join()
request_thread.join()


@requires_sudo
def test_uwsgi_multiprocess():
@allpythons()
def test_uwsgi_multiprocess(py):
request_thread = Thread(target=get, args=("http://localhost:9091",))

with uwsgi(port=9091, args=["--processes", "2", "--threads", "2"]) as uw:
request_thread.start()
with venv(py, reqs=Path(__file__).parent / "requirements-uwsgi.txt") as env:
with uwsgi(
port=9091, args=["--processes", "2", "--threads", "2"], env=env
) as uw:
request_thread.start()

result = austin("-x", "2", "-Cp", str(uw.pid))
assert has_pattern(result.stdout, "app.py:application:5"), compress(
result.stdout
)
result = austin("-x", "2", "-Cp", str(uw.pid))
assert has_pattern(result.stdout, "app.py:application:5"), compress(
result.stdout
)

ts = threads(result.stdout)
assert len(ts) >= 4, ts
assert len({p for p, _ in ts}) >= 2, ts
ts = threads(result.stdout)
assert len(ts) >= 4, ts
assert len({p for p, _ in ts}) >= 2, ts

request_thread.join()
request_thread.join()
Loading