diff --git a/src/viser/infra/_infra.py b/src/viser/infra/_infra.py index c542ee7e..39a8b567 100644 --- a/src/viser/infra/_infra.py +++ b/src/viser/infra/_infra.py @@ -11,7 +11,7 @@ import queue import threading from asyncio.events import AbstractEventLoop -from collections.abc import Awaitable, Coroutine +from collections.abc import Coroutine from pathlib import Path from typing import Any, Callable, Generator, NewType, TypeVar @@ -22,7 +22,7 @@ import websockets.exceptions from typing_extensions import Literal, assert_never, override from websockets import Headers -from websockets.asyncio.server import Server, ServerConnection +from websockets.asyncio.server import ServerConnection from websockets.http11 import Request, Response from ._async_message_buffer import AsyncMessageBuffer diff --git a/tests/test_server_stop.py b/tests/test_server_stop.py new file mode 100644 index 00000000..2832b6c9 --- /dev/null +++ b/tests/test_server_stop.py @@ -0,0 +1,21 @@ +import socket + +import viser + + +def test_server_port_is_freed(): + server = viser.ViserServer() + original_port = server.get_port() + + # Assert that the port is not free. + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + result = sock.connect_ex(("localhost", original_port)) + assert result == 0 + sock.close() + + server.stop() + + # Assert that the port is now free. + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + result = sock.connect_ex(("localhost", original_port)) + assert result != 0