Skip to content

Commit

Permalink
fix: check dependency close method (#5082) (#5084)
Browse files Browse the repository at this point in the history
* fix: check dependency close method

* import annotations

* return string only

---------

Co-authored-by: YuXuan Tay <tay.yuxuan@gt.tech.gov.sg>
  • Loading branch information
yxtay and daip-yxtay authored Nov 13, 2024
1 parent 1b3ea7e commit a29f799
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/_bentoml_sdk/service/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def __getattr__(self, name: str) -> t.Any:
async def close(self) -> None:
if self._resolved is None:
return
await t.cast("RemoteProxy[t.Any]", self._resolved).close()

remote_proxy = t.cast("RemoteProxy[t.Any]", self._resolved)
if asyncio.iscoroutinefunction(getattr(remote_proxy, "close", None)):
await remote_proxy.close()


@t.overload
Expand Down
39 changes: 39 additions & 0 deletions tests/e2e/bento_new_sdk/test_asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

from starlette.testclient import TestClient

import bentoml


@bentoml.service(metrics={"enabled": False})
class Model:
model = {"hello": "world"}

@bentoml.api()
def get(self, key: str) -> str:
return self.model.get(key, "")


@bentoml.service(metrics={"enabled": False})
class Service:
model = bentoml.depends(Model)

@bentoml.api()
def get(self, key: str) -> dict[str, str]:
return {"value": self.model.get(key)}


def test_simple_service():
with TestClient(app=Model.to_asgi()) as client:
resp = client.post("/get", json={"key": "hello"})

assert resp.status_code == 200
assert resp.text == "world"


def test_composed_service():
with TestClient(app=Service.to_asgi()) as client:
resp = client.post("/get", json={"key": "hello"})

assert resp.status_code == 200
assert resp.json() == {"value": "world"}

0 comments on commit a29f799

Please sign in to comment.