Skip to content

Commit

Permalink
Refactor Ray Serve app startup in the tests
Browse files Browse the repository at this point in the history
to use fixed port and route prefix
  • Loading branch information
movchan74 committed Nov 14, 2023
1 parent 052bd06 commit 12f5898
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
8 changes: 4 additions & 4 deletions aana/tests/deployments/test_hf_blip2_deployment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from importlib import resources
import random
import pytest
import ray
from ray import serve
Expand All @@ -13,9 +12,10 @@ def ray_setup(deployment):
# Setup ray environment and serve
ray.init(ignore_reinit_error=True)
app = deployment.bind()
# random port from 30000 to 40000
port = random.randint(30000, 40000)
handle = serve.run(app, port=port)
port = 34422
test_name = deployment.name
route_prefix = f"/{test_name}"
handle = serve.run(app, port=port, name=test_name, route_prefix=route_prefix)
return handle


Expand Down
8 changes: 4 additions & 4 deletions aana/tests/deployments/test_vllm_deployment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
import pytest
import ray
from ray import serve
Expand Down Expand Up @@ -27,9 +26,10 @@ def ray_setup(deployment):
# Setup ray environment and serve
ray.init(ignore_reinit_error=True)
app = deployment.bind()
# random port from 30000 to 40000
port = random.randint(30000, 40000)
handle = serve.run(app, port=port)
port = 34422
test_name = deployment.name
route_prefix = f"/{test_name}"
handle = serve.run(app, port=port, name=test_name, route_prefix=route_prefix)
return handle


Expand Down
19 changes: 10 additions & 9 deletions aana/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import random
import pytest
import ray
from ray import serve
Expand Down Expand Up @@ -69,7 +68,7 @@ async def lower(self, text: str) -> dict:


@pytest.fixture(scope="session")
def ray_setup():
def ray_setup(request):
"""
Setup the Ray environment and serve the endpoints.
Expand All @@ -79,27 +78,29 @@ def ray_setup():
"""
ray.init(ignore_reinit_error=True)
server = RequestHandler.bind(endpoints, nodes, context)
# random port from 30000 to 40000
port = random.randint(30000, 40000)
handle = serve.run(server, port=port)
return handle, port
port = 34422
test_name = request.node.name
route_prefix = f"/{test_name}"
handle = serve.run(server, port=port, name=test_name, route_prefix=route_prefix)
return handle, port, route_prefix


def test_app(ray_setup):
"""
Test the Ray Serve app.
"""
handle, port = ray_setup
handle, port, route_prefix = ray_setup

# Check that the server is ready
response = requests.get(f"http://localhost:{port}/api/ready")
response = requests.get(f"http://localhost:{port}{route_prefix}/api/ready")
assert response.status_code == 200
assert response.json() == {"ready": True}

# Test lowercase endpoint
data = {"text": ["Hello World!", "This is a test."]}
response = requests.post(
f"http://localhost:{port}/lowercase", data={"body": json.dumps(data)}
f"http://localhost:{port}{route_prefix}/lowercase",
data={"body": json.dumps(data)},
)
assert response.status_code == 200
lowercase_text = response.json().get("lowercase_text")
Expand Down
20 changes: 11 additions & 9 deletions aana/tests/test_app_streaming.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import random
from sys import prefix
import time
from typing import AsyncGenerator
import pytest
import ray
Expand Down Expand Up @@ -77,7 +78,7 @@ async def lower_stream(self, text: str) -> AsyncGenerator[dict, None]:


@pytest.fixture(scope="session")
def ray_setup():
def ray_setup(request):
"""
Setup the Ray environment and serve the endpoints.
Expand All @@ -87,29 +88,30 @@ def ray_setup():
"""
ray.init(ignore_reinit_error=True)
server = RequestHandler.bind(endpoints, nodes, context)
# random port from 30000 to 40000
port = random.randint(30000, 40000)
handle = serve.run(server, port=port)
return handle, port
port = 34422
test_name = request.node.name
route_prefix = f"/{test_name}"
handle = serve.run(server, port=port, name=test_name, route_prefix=route_prefix)
return handle, port, route_prefix


def test_app_streaming(ray_setup):
"""
Test the Ray Serve app with streaming enabled.
"""

handle, port = ray_setup
handle, port, route_prefix = ray_setup

# Check that the server is ready
response = requests.get(f"http://localhost:{port}/api/ready")
response = requests.get(f"http://localhost:{port}{route_prefix}/api/ready")
assert response.status_code == 200
assert response.json() == {"ready": True}

# Test lowercase endpoint
text = "Hello World, this is a test."
data = {"text": text}
response = requests.post(
f"http://localhost:{port}/lowercase",
f"http://localhost:{port}{route_prefix}/lowercase",
data={"body": json.dumps(data)},
stream=True,
)
Expand Down

0 comments on commit 12f5898

Please sign in to comment.