Skip to content

Commit

Permalink
fix unit tests that use app url
Browse files Browse the repository at this point in the history
  • Loading branch information
paulineribeyre committed Jan 28, 2025
1 parent 38986e2 commit ab5e934
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"filename": "tests/conftest.py",
"hashed_secret": "0dd78d9147bb410f0cb0199c5037da36594f77d8",
"is_verified": false,
"line_number": 226
"line_number": 228
}
],
"tests/migrations/test_migration_e1886270d9d2.py": [
Expand All @@ -209,5 +209,5 @@
}
]
},
"generated_at": "2025-01-27T18:55:41Z"
"generated_at": "2025-01-28T17:07:00Z"
}
36 changes: 26 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
See https://github.com/uc-cdis/gen3-user-data-library/blob/main/tests/conftest.py#L1
"""

import contextlib
from datetime import datetime
from dateutil.tz import tzutc
import json
import os
import time
from unittest.mock import MagicMock, patch
from urllib.parse import parse_qsl, urlparse

Expand Down Expand Up @@ -274,6 +276,25 @@ async def reset_requests_mocks():
mock_arborist_request.reset_mock()


class UvicornServer(uvicorn.Server):
"""
Server that can be stopped when the unit test completes. Used for tests that need to hit
the app URL directly.
"""

@contextlib.contextmanager
def run_in_thread(self):
thread = Thread(target=self.run)
thread.start()
try:
while not self.started:
time.sleep(1e-3)
yield
finally:
self.should_exit = True
thread.join()


@pytest_asyncio.fixture(scope="session")
async def client(request):
"""
Expand Down Expand Up @@ -343,16 +364,11 @@ async def handle_request(request: Request):
if get_url: # for tests that need to hit the app URL directly
host = "0.0.0.0"
port = 8080

def run_uvicorn():
uvicorn.run(app, host=host, port=port)

# start the app in a separate thread
thread = Thread(target=run_uvicorn)
thread.daemon = True # ensures the thread ends when the test ends
thread.start()

yield f"http://{host}:{port}" # URL to use in the tests
server = UvicornServer(
config=uvicorn.Config(app, host=host, port=port, log_level="debug")
)
with server.run_in_thread():
yield f"http://{host}:{port}"
else:
# the tests use a real httpx client that forwards requests to the app
async with httpx.AsyncClient(
Expand Down
7 changes: 0 additions & 7 deletions tests/test_s3_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ def test_s3_endpoint_no_token(s3_client):
s3_client.list_objects(Bucket=f"gen3wf-{config['HOSTNAME']}-{TEST_USER_ID}")


# This test currently doesn't work because the client generated when `get_url` is True is not
# stopped properly, so generating a different client (with `authorized=False` param) triggers an
# error:
# > OSError: [Errno 48] error while attempting to bind on address ('0.0.0.0', 8080): address already
# in use
# TODO fix that
@pytest.mark.skip(reason="doesn't work")
@pytest.mark.parametrize(
"client", [{"get_url": True, "authorized": False}], indirect=True
)
Expand Down

0 comments on commit ab5e934

Please sign in to comment.