Skip to content

Commit 93346ba

Browse files
committed
DEFAULT_HOST and PORT
1 parent 8e8dfe1 commit 93346ba

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

interpreter/core/async_core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ async def chat_completion(request: ChatCompletionRequest):
701701

702702

703703
class Server:
704+
DEFAULT_HOST = "127.0.0.1"
705+
DEFAULT_PORT = 8000
706+
704707
def __init__(self, async_interpreter, host=None, port=None):
705708
self.app = FastAPI()
706709
router = create_router(async_interpreter)
@@ -720,7 +723,9 @@ async def validate_api_key(request: Request, call_next):
720723
)
721724

722725
self.app.include_router(router)
723-
self.config = uvicorn.Config(app=self.app, host=host or os.getenv("HOST", "127.0.0.1"), port=port or int(os.getenv("PORT", "8000")))
726+
h = host or os.getenv("HOST", Server.DEFAULT_HOST)
727+
p = port or int(os.getenv("PORT", Server.DEFAULT_PORT))
728+
self.config = uvicorn.Config(app=self.app, host=h, port=p)
724729
self.uvicorn_server = uvicorn.Server(self.config)
725730

726731
@property

tests/core/test_async_core.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,42 @@
55

66

77
class TestServerConstruction(TestCase):
8-
def test_host_and_port_from_env_1(self):
9-
fake_host = "fake_host"
10-
fake_port = 1234
8+
"""
9+
Tests to make sure that the underlying server is configured correctly when constructing
10+
the Server object.
11+
"""
1112

12-
with mock.patch.dict(os.environ, {"HOST": fake_host, "PORT": str(fake_port)}):
13+
def test_host_and_port_defaults(self):
14+
"""
15+
Tests that a Server object takes on the default host and port when
16+
a) no host and port are passed in, and
17+
b) no HOST and PORT are set.
18+
"""
19+
with mock.patch.dict(os.environ, {}):
1320
s = Server(AsyncInterpreter())
14-
self.assertEqual(s.host, fake_host)
15-
self.assertEqual(s.port, fake_port)
21+
self.assertEqual(s.host, Server.DEFAULT_HOST)
22+
self.assertEqual(s.port, Server.DEFAULT_PORT)
23+
24+
def test_host_and_port_passed_in(self):
25+
"""
26+
Tests that a Server object takes on the passed-in host and port when they are passed-in,
27+
ignoring the surrounding HOST and PORT env vars.
28+
"""
29+
host = "the-really-real-host"
30+
port = 2222
1631

17-
def test_host_and_port_from_env_2(self):
18-
fake_host = "some-other-fake-host"
19-
fake_port = 4321
32+
with mock.patch.dict(os.environ, {"HOST": "this-is-supes-fake", "PORT": "9876"}):
33+
sboth = Server(AsyncInterpreter(), host, port)
34+
self.assertEqual(sboth.host, host)
35+
self.assertEqual(sboth.port, port)
36+
37+
def test_host_and_port_from_env_1(self):
38+
"""
39+
Tests that the Server object takes on the HOST and PORT env vars as host and port when
40+
nothing has been passed in.
41+
"""
42+
fake_host = "fake_host"
43+
fake_port = 1234
2044

2145
with mock.patch.dict(os.environ, {"HOST": fake_host, "PORT": str(fake_port)}):
2246
s = Server(AsyncInterpreter())

0 commit comments

Comments
 (0)