diff --git a/src/litserve/python_client.py b/src/litserve/python_client.py new file mode 100644 index 00000000..cb7b4006 --- /dev/null +++ b/src/litserve/python_client.py @@ -0,0 +1,4 @@ +import requests + +response = requests.post("http://127.0.0.1:8000/predict", json={"input": 4.0}) +print(f"Status: {response.status_code}\nResponse:\n {response.text}") diff --git a/tests/simple_server.py b/tests/simple_server.py new file mode 100644 index 00000000..5819a010 --- /dev/null +++ b/tests/simple_server.py @@ -0,0 +1,22 @@ +from litserve.server import LitServer +from litserve.api import LitAPI +from fastapi import Request, Response + + +class SimpleLitAPI(LitAPI): + def setup(self, device): + self.model = lambda x: x**2 + + def decode_request(self, request: Request): + return request["input"] + + def predict(self, x): + return self.model(x) + + def encode_response(self, output) -> Response: + return {"output": output} + + +if __name__ == "__main__": + server = LitServer(SimpleLitAPI(), accelerator="cpu", devices=1, timeout=10) + server.run() diff --git a/tests/test_lit_server.py b/tests/test_lit_server.py index 90921e58..625b8b84 100644 --- a/tests/test_lit_server.py +++ b/tests/test_lit_server.py @@ -1,4 +1,10 @@ +import subprocess +import time from multiprocessing import Pipe, Manager + + +import os + from unittest.mock import patch, MagicMock from litserve.server import inference_worker, run_single_loop from litserve.server import LitServer @@ -84,3 +90,18 @@ def test_single_loop(simple_litapi, loop_args): with pytest.raises(StopIteration, match="exit loop"): run_single_loop(lit_api_mock, requests_queue, request_buffer) + + +def test_run(): + subprocess.Popen( + ["python", "tests/simple_server.py"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + stdin=subprocess.DEVNULL, + ) + + time.sleep(5) + assert os.path.exists("client.py") + output = subprocess.run("python client.py", shell=True, capture_output=True, text=True).stdout + assert '{"output":16.0}' in output + os.remove("client.py")