I have an ray ingress app which uses FastAPI as below and am trying to unit test it but i am facing the “RayServeException”
Source code :
from typing import Dict
from fastapi import FastAPI, HTTPException
import pytest
from ray import serve
import requests
app = FastAPI()
@serve.deployment
@serve.ingress(app)
class MyDeployment:
@app.post("/enpoint1")
async def root(self, payload: Dict) -> Dict:
response_json = {
"text": "undefined"
}
if payload["text"] == "enpoint1":
response_json["text"] = "yes endpoin1"
return response_json
@app.post("/endpoint2")
async def root(self, payload: Dict) -> Dict:
response_json = {
"text": "undefined"
}
if payload["text"]== "endpoint2":
response_json["text"] = "yes endpoint2"
return response_json
driver = MyDeployment.bind()
The unit test file looks this:
import unittest
import ray
from ray import serve
from fastapi.testclient import TestClient
from test_serve_deployment import app
class TestMyDeployment(unittest.TestCase):
def setUp(self):
ray.init(num_cpus = 2,num_gpus = 0)
def tearDown(self):
ray.shutdown()
def test_number1(self):
client = TestClient(app)
response = client.post("/detect",json={"text":"detect"})
print(response)
if __name__ == '__main__':
unittest.main()
Error seen as below:
2-08_13-49-33_712362_16455/logs/gcs_server.out' mode='a' encoding='utf-8'>
self.start_gcs_server()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1353: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/gcs_server.err' mode='a' encoding='utf-8'>
self.start_gcs_server()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1358: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/monitor.out' mode='a' encoding='utf-8'>
self.start_monitor()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1358: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/monitor.err' mode='a' encoding='utf-8'>
self.start_monitor()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1369: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/dashboard.err' mode='a' encoding='utf-8'>
self.start_api_server(
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1411: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/raylet.out' mode='a' encoding='utf-8'>
self.start_raylet(plasma_directory, object_store_memory)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1411: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/raylet.err' mode='a' encoding='utf-8'>
self.start_raylet(plasma_directory, object_store_memory)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/home/ec2-user/.local/lib/python3.11/site-packages/ray/_private/node.py:1413: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/ray/session_2024-12-08_13-49-33_712362_16455/logs/log_monitor.err' mode='a' encoding='utf-8'>
self.start_log_monitor()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
2024-12-08 13:49:36,480 INFO worker.py:1772 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265
(ProxyActor pid=16807) INFO 2024-12-08 13:49:40,941 proxy 10.88.244.57 proxy.py:1179 - Proxy starting on node c2f7167fe90ea69f6b40d3dfaf1b7717538c2956466b3edec67073bd (HTTP port: 8000).
E/usr/local/lib/python3.11/subprocess.py:1127: ResourceWarning: subprocess 16510 is still running
_warn("subprocess %s is still running" % self.pid,
ResourceWarning: Enable tracemalloc to get the object allocation traceback
======================================================================
ERROR: test_number1 (__main__.TestMyDeployment.test_number1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ec2-user/wip/heron/escam_orchestrator/testexample.py", line 18, in test_number1
response = client.post("/enpoint1",json={"text":"detect"})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/testclient.py", line 633, in post
return super().post(
^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/httpx/_client.py", line 1145, in post
return self.request(
^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/testclient.py", line 516, in request
return super().request(
^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/httpx/_client.py", line 827, in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/httpx/_client.py", line 914, in send
response = self._send_handling_auth(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/httpx/_client.py", line 942, in _send_handling_auth
response = self._send_handling_redirects(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/httpx/_client.py", line 979, in _send_handling_redirects
response = self._send_single_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/httpx/_client.py", line 1015, in _send_single_request
response = transport.handle_request(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/testclient.py", line 398, in handle_request
raise exc
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/testclient.py", line 395, in handle_request
portal.call(self.app, scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/anyio/from_thread.py", line 287, in call
return cast(T_Retval, self.start_task_soon(func, *args).result())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/home/ec2-user/.local/lib/python3.11/site-packages/anyio/from_thread.py", line 218, in _call_func
retval = await retval_or_awaitable
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
raise exc
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
raise exc
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/routing.py", line 776, in app
await route.handle(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle
await self.app(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
raise exc
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/routing.py", line 72, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/fastapi/routing.py", line 269, in app
solved_result = await solve_dependencies(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 602, in solve_dependencies
solved = await run_in_threadpool(call, **sub_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/starlette/concurrency.py", line 42, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/anyio/to_thread.py", line 56, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2177, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 859, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/ray/serve/_private/http_util.py", line 320, in get_current_servable_instance
return serve.get_replica_context().servable_object
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ec2-user/.local/lib/python3.11/site-packages/ray/serve/api.py", line 159, in get_replica_context
raise RayServeException(
ray.serve.exceptions.RayServeException: `serve.get_replica_context()` may only be called from within a Ray Serve deployment.
----------------------------------------------------------------------
Ran 1 test in 9.074s
FAILED (errors=1)