Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add web_server decorator container test #2738

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions test/container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import modal
from modal import Client, Queue, Volume, is_local
from modal._container_entrypoint import UserException, main
from modal._runtime import asgi
from modal._runtime.container_io_manager import (
ContainerIOManager,
InputSlots,
Expand Down Expand Up @@ -196,12 +197,16 @@ def _container_args(
),
app_id: str = "ap-1",
app_layout: api_pb2.AppLayout = DEFAULT_APP_LAYOUT,
web_server_port: Optional[int] = None,
web_server_startup_timeout: Optional[float] = None,
):
if webhook_type:
webhook_config = api_pb2.WebhookConfig(
type=webhook_type,
method="GET",
async_mode=api_pb2.WEBHOOK_ASYNC_MODE_AUTO,
web_server_port=web_server_port,
web_server_startup_timeout=web_server_startup_timeout,
)
else:
webhook_config = None
Expand Down Expand Up @@ -270,6 +275,8 @@ def _run_container(
format=api_pb2.ClassParameterInfo.PARAM_SERIALIZATION_FORMAT_UNSPECIFIED, schema=[]
),
app_layout=DEFAULT_APP_LAYOUT,
web_server_port: Optional[int] = None,
web_server_startup_timeout: Optional[float] = None,
) -> ContainerResult:
container_args = _container_args(
module_name,
Expand All @@ -292,6 +299,8 @@ def _run_container(
is_class=is_class,
class_parameter_info=class_parameter_info,
app_layout=app_layout,
web_server_port=web_server_port,
web_server_startup_timeout=web_server_startup_timeout,
)
with Client(servicer.container_addr, api_pb2.CLIENT_TYPE_CONTAINER, None) as client:
if inputs is None:
Expand Down Expand Up @@ -684,6 +693,33 @@ def test_asgi(servicer):
assert json.loads(second_message["body"]) == {"hello": "space"}


@skip_github_non_linux
def test_non_blocking_web_server(servicer, monkeypatch):
get_ip_address = MagicMock(wraps=asgi.get_ip_address)
get_ip_address.return_value = "127.0.0.1"
monkeypatch.setattr(asgi, "get_ip_address", get_ip_address)

inputs = _get_web_inputs(path="/")
_put_web_body(servicer, b"")
ret = _run_container(
servicer,
"test.supports.functions",
"non_blocking_web_server",
inputs=inputs,
webhook_type=api_pb2.WEBHOOK_TYPE_WEB_SERVER,
web_server_port=8765,
web_server_startup_timeout=1,
)
first_message, second_message, _ = _unwrap_asgi(ret)

# Check the headers
assert first_message["status"] == 200
headers = dict(first_message["headers"])
assert headers[b"Content-Type"] == b"text/html; charset=utf-8"

assert b"Directory listing" in second_message["body"]


@skip_github_non_linux
def test_asgi_lifespan(servicer):
inputs = _get_web_inputs(path="/")
Expand Down Expand Up @@ -844,9 +880,6 @@ def test_non_lifespan_asgi(servicer):
assert headers[b"content-type"] == b"application/json"

# Check body
print("\n#########################")
print(f"second_message: {second_message['body']}")
print("#########################\n")
assert json.loads(second_message["body"]) == "foo"


Expand Down
9 changes: 9 additions & 0 deletions test/supports/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
is_local,
method,
web_endpoint,
web_server,
wsgi_app,
)
from modal._utils.deprecation import deprecation_warning
Expand Down Expand Up @@ -158,6 +159,14 @@ async def foo(arg="world"):
return web_app


@app.function()
@web_server(8765, startup_timeout=1)
def non_blocking_web_server():
import subprocess

subprocess.Popen(["python", "-m", "http.server", "-b", "0.0.0.0", "8765"])


lifespan_global_asgi_app_func: list[str] = []


Expand Down
Loading