Skip to content

Commit 103a1cf

Browse files
authored
migrate arguments from LitServe to LitAPI in tests (#576)
* refactor: update server initialization in E2E tests to pass API instances directly * Refactor E2E test files to create API instances with parameters before passing them to LitServer. * This change improves clarity and consistency across the test suite by ensuring that API configurations are set up prior to server initialization. * Adjustments made in multiple test files including default_async_streaming.py, default_batched_streaming.py, and others. * Apply suggestions from code review * refactor: enhance test fixtures and update test cases for LitServer * Added new fixtures `simple_litapi_cls` and `simple_stream_cls` to return class instances of `SimpleLitAPI` and `SimpleStreamAPI`. * Updated `test_litapi_with_stream` to utilize the new fixture for improved clarity. * Refactored several test cases to directly pass API instances to `LitServer`, ensuring better configuration management. * Adjusted assertions in context injection tests for consistency and clarity. * update * refactor: update test cases to pass API classes directly to LitServer * Modified test cases to instantiate API classes before passing them to LitServer, enhancing clarity and consistency. * Updated error handling in tests to ensure proper exception messages are raised. * Refactored several tests to streamline setup and improve readability. * fix * feat: add port fixture for dynamic port assignment in tests * Introduced a new pytest fixture `port` that generates a random port number between 10000 and 65535 for use in tests. * Updated `test_workers_setup_status` to utilize the new `port` fixture, enhancing test flexibility and avoiding port conflicts.
1 parent 62fa9f4 commit 103a1cf

17 files changed

+110
-89
lines changed

tests/conftest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import base64
15+
import random
1516
import time
1617
from typing import Generator
1718

@@ -104,9 +105,19 @@ def simple_litapi():
104105
return SimpleLitAPI()
105106

106107

108+
@pytest.fixture
109+
def simple_litapi_cls():
110+
return SimpleLitAPI
111+
112+
113+
@pytest.fixture
114+
def simple_stream_cls():
115+
return SimpleStreamAPI
116+
117+
107118
@pytest.fixture
108119
def simple_stream_api():
109-
return SimpleStreamAPI()
120+
return SimpleStreamAPI(stream=True)
110121

111122

112123
@pytest.fixture
@@ -382,3 +393,8 @@ def shutdown(self):
382393
pass
383394

384395
return MockManager()
396+
397+
398+
@pytest.fixture
399+
def port():
400+
return random.randint(10000, 65535)

tests/e2e/default_async_streaming.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ async def encode_response(self, output):
3131

3232

3333
if __name__ == "__main__":
34-
api = AsyncAPI(enable_async=True)
35-
server = ls.LitServer(api, stream=True)
34+
api = AsyncAPI(enable_async=True, stream=True)
35+
server = ls.LitServer(api)
3636
server.run(port=8000)

tests/e2e/default_batched_streaming.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ def encode_response(self, output_stream):
3333

3434

3535
if __name__ == "__main__":
36-
server = ls.LitServer(SimpleStreamAPI(), stream=True, max_batch_size=4, batch_timeout=0.2, fast_queue=True)
36+
api = SimpleStreamAPI(stream=True, max_batch_size=4, batch_timeout=0.2)
37+
server = ls.LitServer(api, fast_queue=True)
3738
server.run(port=8000)

tests/e2e/default_batching.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
import litserve as ls
1515

1616
if __name__ == "__main__":
17-
api = ls.test_examples.SimpleBatchedAPI()
18-
server = ls.LitServer(api, max_batch_size=4, batch_timeout=0.05)
17+
api = ls.test_examples.SimpleBatchedAPI(max_batch_size=4, batch_timeout=0.05)
18+
server = ls.LitServer(api)
1919
server.run(port=8000)

tests/e2e/default_openai_embedding_spec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from litserve.test_examples.openai_embedding_spec_example import TestEmbedAPI
44

55
if __name__ == "__main__":
6-
server = ls.LitServer(TestEmbedAPI(), spec=OpenAIEmbeddingSpec(), fast_queue=True)
6+
api = TestEmbedAPI(spec=OpenAIEmbeddingSpec())
7+
server = ls.LitServer(api, fast_queue=True)
78
server.run()

tests/e2e/default_openai_with_batching.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
from litserve.test_examples.openai_spec_example import OpenAIBatchContext
33

44
if __name__ == "__main__":
5-
api = OpenAIBatchContext()
6-
server = ls.LitServer(api, spec=ls.OpenAISpec(), max_batch_size=2, batch_timeout=0.5, fast_queue=True)
5+
api = OpenAIBatchContext(max_batch_size=2, batch_timeout=0.5, spec=ls.OpenAISpec())
6+
server = ls.LitServer(api, fast_queue=True)
77
server.run(port=8000)

tests/e2e/default_openaispec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from litserve.test_examples.openai_spec_example import TestAPI
44

55
if __name__ == "__main__":
6-
server = ls.LitServer(TestAPI(), spec=OpenAISpec())
6+
api = TestAPI(spec=OpenAISpec())
7+
server = ls.LitServer(api)
78
server.run()

tests/e2e/default_openaispec_response_format.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from litserve.test_examples.openai_spec_example import TestAPIWithStructuredOutput
44

55
if __name__ == "__main__":
6-
server = ls.LitServer(TestAPIWithStructuredOutput(), spec=OpenAISpec(), fast_queue=True)
6+
api = TestAPIWithStructuredOutput(spec=OpenAISpec())
7+
server = ls.LitServer(api, fast_queue=True)
78
server.run()

tests/e2e/default_openaispec_tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ def encode_response(self, output):
2020

2121

2222
if __name__ == "__main__":
23-
server = ls.LitServer(TestAPIWithToolCalls(), spec=OpenAISpec())
23+
api = TestAPIWithToolCalls(spec=OpenAISpec())
24+
server = ls.LitServer(api)
2425
server.run()

tests/e2e/default_single_streaming.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ def encode_response(self, output_stream):
1818

1919

2020
if __name__ == "__main__":
21-
api = SimpleStreamingAPI()
22-
server = LitServer(api, stream=True)
21+
api = SimpleStreamingAPI(stream=True)
22+
server = LitServer(api)
2323
server.run(port=8000)

0 commit comments

Comments
 (0)