server: create fixture for starting the server, and always close server even if one test fail

This commit is contained in:
2023-10-13 14:51:57 +02:00
committed by Mathieu Virbel
parent 0e4ef90e62
commit 4e40cc511a

View File

@@ -31,28 +31,43 @@ class ThreadedUvicorn:
continue continue
@pytest.mark.asyncio @pytest.fixture
async def test_transcript_rtc_and_websocket( async def appserver(tmpdir):
tmpdir, dummy_llm, dummy_transcript, dummy_processors, ensure_casing
):
# goal: start the server, exchange RTC, receive websocket events
# because of that, we need to start the server in a thread
# to be able to connect with aiortc
from reflector.settings import settings from reflector.settings import settings
from reflector.app import app from reflector.app import app
DATA_DIR = settings.DATA_DIR
settings.DATA_DIR = Path(tmpdir) settings.DATA_DIR = Path(tmpdir)
# start server # start server
host = "127.0.0.1" host = "127.0.0.1"
port = 1255 port = 1255
base_url = f"http://{host}:{port}/v1"
config = Config(app=app, host=host, port=port) config = Config(app=app, host=host, port=port)
server = ThreadedUvicorn(config) server = ThreadedUvicorn(config)
await server.start() await server.start()
yield (server, host, port)
server.stop()
settings.DATA_DIR = DATA_DIR
@pytest.mark.asyncio
async def test_transcript_rtc_and_websocket(
tmpdir,
dummy_llm,
dummy_transcript,
dummy_processors,
ensure_casing,
appserver,
):
# goal: start the server, exchange RTC, receive websocket events
# because of that, we need to start the server in a thread
# to be able to connect with aiortc
server, host, port = appserver
# create a transcript # create a transcript
base_url = f"http://{host}:{port}/v1"
ac = AsyncClient(base_url=base_url) ac = AsyncClient(base_url=base_url)
response = await ac.post("/transcripts", json={"name": "Test RTC"}) response = await ac.post("/transcripts", json={"name": "Test RTC"})
assert response.status_code == 200 assert response.status_code == 200
@@ -169,33 +184,24 @@ async def test_transcript_rtc_and_websocket(
assert resp.status_code == 200 assert resp.status_code == 200
assert resp.headers["Content-Type"] == "audio/mpeg" assert resp.headers["Content-Type"] == "audio/mpeg"
# stop server
server.stop()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_transcript_rtc_and_websocket_and_fr( async def test_transcript_rtc_and_websocket_and_fr(
tmpdir, dummy_llm, dummy_transcript, dummy_processors, ensure_casing tmpdir,
dummy_llm,
dummy_transcript,
dummy_processors,
ensure_casing,
appserver,
): ):
# goal: start the server, exchange RTC, receive websocket events # goal: start the server, exchange RTC, receive websocket events
# because of that, we need to start the server in a thread # because of that, we need to start the server in a thread
# to be able to connect with aiortc # to be able to connect with aiortc
# with target french language # with target french language
server, host, port = appserver
from reflector.settings import settings
from reflector.app import app
settings.DATA_DIR = Path(tmpdir)
# start server
host = "127.0.0.1"
port = 1255
base_url = f"http://{host}:{port}/v1"
config = Config(app=app, host=host, port=port)
server = ThreadedUvicorn(config)
await server.start()
# create a transcript # create a transcript
base_url = f"http://{host}:{port}/v1"
ac = AsyncClient(base_url=base_url) ac = AsyncClient(base_url=base_url)
response = await ac.post( response = await ac.post(
"/transcripts", json={"name": "Test RTC", "target_language": "fr"} "/transcripts", json={"name": "Test RTC", "target_language": "fr"}
@@ -303,6 +309,3 @@ async def test_transcript_rtc_and_websocket_and_fr(
# ensure the last event received is ended # ensure the last event received is ended
assert events[-1]["event"] == "STATUS" assert events[-1]["event"] == "STATUS"
assert events[-1]["data"]["value"] == "ended" assert events[-1]["data"]["value"] == "ended"
# stop server
server.stop()