Skip to content

Commit 50f2600

Browse files
committed
- Add tests for both initialization methods of VLLMClient
- Verify correct behavior of base_url attribute - Update tests to include tensor parallelism scenarios - Ensure proper cleanup of client resources
1 parent 38c9c8e commit 50f2600

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

tests/test_vllm_client_server.py

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,32 @@ def setUpClass(cls):
4343
["trl", "vllm-serve", "--model", cls.model_id], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
4444
)
4545

46-
# Initialize the client
47-
cls.client = VLLMClient(connection_timeout=120)
46+
# Initialize the clients using both initialization methods
47+
cls.client = VLLMClient(connection_timeout=120) # Default host and port
48+
cls.client_base_url = VLLMClient(base_url="http://0.0.0.0:8000", connection_timeout=120) # Using base_url
49+
50+
def test_initialization_methods(self):
51+
"""Test that both initialization methods work correctly."""
52+
# Test generation with default client (host+port)
53+
prompts = ["Test initialization 1"]
54+
outputs_default = self.client.generate(prompts)
55+
self.assertIsInstance(outputs_default, list)
56+
self.assertEqual(len(outputs_default), len(prompts))
57+
58+
# Test generation with base_url client
59+
outputs_base_url = self.client_base_url.generate(prompts)
60+
self.assertIsInstance(outputs_base_url, list)
61+
self.assertEqual(len(outputs_base_url), len(prompts))
62+
63+
def test_base_url_attribute(self):
64+
"""Test that both initialization methods set the base_url attribute correctly."""
65+
# Both clients should have the same base_url
66+
self.assertEqual(self.client.base_url, "http://0.0.0.0:8000")
67+
self.assertEqual(self.client_base_url.base_url, "http://0.0.0.0:8000")
68+
69+
# Verify the client doesn't store host/port when base_url is provided
70+
self.assertTrue(not hasattr(self.client_base_url, 'host') or self.client_base_url.host is None)
71+
self.assertTrue(not hasattr(self.client_base_url, 'server_port') or self.client_base_url.server_port is None)
4872

4973
def test_generate(self):
5074
prompts = ["Hello, AI!", "Tell me a joke"]
@@ -90,8 +114,9 @@ def test_reset_prefix_cache(self):
90114
def tearDownClass(cls):
91115
super().tearDownClass()
92116

93-
# Close the client
117+
# Close the clients
94118
cls.client.close_communicator()
119+
cls.client_base_url.close_communicator()
95120

96121
# vLLM x pytest (or Popen) seems not to handle process termination well. To avoid zombie processes, we need to
97122
# kill the server process and its children explicitly.
@@ -122,8 +147,32 @@ def setUpClass(cls):
122147
env=env,
123148
)
124149

125-
# Initialize the client
126-
cls.client = VLLMClient(connection_timeout=120)
150+
# Initialize the clients using both initialization methods
151+
cls.client = VLLMClient(connection_timeout=120) # Default host and port
152+
cls.client_base_url = VLLMClient(base_url="http://0.0.0.0:8000", connection_timeout=120) # Using base_url
153+
154+
def test_initialization_methods(self):
155+
"""Test that both initialization methods work correctly with tensor parallelism enabled."""
156+
# Test generation with default client (host+port)
157+
prompts = ["Test TP initialization 1"]
158+
outputs_default = self.client.generate(prompts)
159+
self.assertIsInstance(outputs_default, list)
160+
self.assertEqual(len(outputs_default), len(prompts))
161+
162+
# Test generation with base_url client
163+
outputs_base_url = self.client_base_url.generate(prompts)
164+
self.assertIsInstance(outputs_base_url, list)
165+
self.assertEqual(len(outputs_base_url), len(prompts))
166+
167+
def test_base_url_attribute(self):
168+
"""Test that both initialization methods set the base_url attribute correctly."""
169+
# Both clients should have the same base_url
170+
self.assertEqual(self.client.base_url, "http://0.0.0.0:8000")
171+
self.assertEqual(self.client_base_url.base_url, "http://0.0.0.0:8000")
172+
173+
# Verify the client doesn't store host/port when base_url is provided
174+
self.assertTrue(not hasattr(self.client_base_url, 'host') or self.client_base_url.host is None)
175+
self.assertTrue(not hasattr(self.client_base_url, 'server_port') or self.client_base_url.server_port is None)
127176

128177
def test_generate(self):
129178
prompts = ["Hello, AI!", "Tell me a joke"]
@@ -151,8 +200,9 @@ def test_reset_prefix_cache(self):
151200
def tearDownClass(cls):
152201
super().tearDownClass()
153202

154-
# Close the client
203+
# Close the clients
155204
cls.client.close_communicator()
205+
cls.client_base_url.close_communicator()
156206

157207
# vLLM x pytest (or Popen) seems not to handle process termination well. To avoid zombie processes, we need to
158208
# kill the server process and its children explicitly.

0 commit comments

Comments
 (0)