Skip to content

Commit

Permalink
Merge pull request #191 from P403n1x87/tests/uwsgi-venv
Browse files Browse the repository at this point in the history
tests: run uwsgi tests in virtual environment
  • Loading branch information
P403n1x87 authored Sep 4, 2023
2 parents 55f56fb + 7a628a9 commit 7815662
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
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()

0 comments on commit 7815662

Please sign in to comment.