| |
| |
| import socket |
|
|
| import pytest |
| import zmq |
|
|
| from vllm.utils.network_utils import ( |
| get_open_port, |
| get_open_ports_list, |
| get_tcp_uri, |
| join_host_port, |
| make_zmq_path, |
| make_zmq_socket, |
| split_host_port, |
| split_zmq_path, |
| ) |
|
|
|
|
| def test_get_open_port(monkeypatch: pytest.MonkeyPatch): |
| with monkeypatch.context() as m: |
| m.setenv("VLLM_PORT", "5678") |
| |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s1: |
| s1.bind(("localhost", get_open_port())) |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s2: |
| s2.bind(("localhost", get_open_port())) |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s3: |
| s3.bind(("localhost", get_open_port())) |
|
|
|
|
| def test_get_open_ports_list_with_vllm_port(monkeypatch: pytest.MonkeyPatch): |
| with monkeypatch.context() as m: |
| m.setenv("VLLM_PORT", "5678") |
| ports = get_open_ports_list(5) |
| assert len(ports) == 5 |
| assert len(set(ports)) == 5, "ports must be unique" |
|
|
| |
| sockets = [] |
| try: |
| for p in ports: |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| s.bind(("localhost", p)) |
| sockets.append(s) |
| finally: |
| for s in sockets: |
| s.close() |
|
|
|
|
| @pytest.mark.parametrize( |
| "path,expected", |
| [ |
| ("ipc://some_path", ("ipc", "some_path", "")), |
| ("tcp://127.0.0.1:5555", ("tcp", "127.0.0.1", "5555")), |
| ("tcp://[::1]:5555", ("tcp", "::1", "5555")), |
| ("inproc://some_identifier", ("inproc", "some_identifier", "")), |
| ], |
| ) |
| def test_split_zmq_path(path, expected): |
| assert split_zmq_path(path) == expected |
|
|
|
|
| @pytest.mark.parametrize( |
| "invalid_path", |
| [ |
| "invalid_path", |
| "tcp://127.0.0.1", |
| "tcp://[::1]", |
| "tcp://:5555", |
| ], |
| ) |
| def test_split_zmq_path_invalid(invalid_path): |
| with pytest.raises(ValueError): |
| split_zmq_path(invalid_path) |
|
|
|
|
| def test_make_zmq_socket_ipv6(): |
| |
| try: |
| sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
| sock.close() |
| except OSError: |
| pytest.skip("IPv6 is not supported on this system") |
|
|
| ctx = zmq.Context() |
| ipv6_path = "tcp://[::]:5555" |
| socket_type = zmq.REP |
|
|
| |
| zsock: zmq.Socket = make_zmq_socket(ctx, ipv6_path, socket_type) |
|
|
| |
| assert zsock.getsockopt(zmq.IPV6) == 1, ( |
| "IPV6 option should be enabled for IPv6 addresses" |
| ) |
|
|
| |
| zsock.close() |
| ctx.term() |
|
|
|
|
| def test_make_zmq_path(): |
| assert make_zmq_path("tcp", "127.0.0.1", "5555") == "tcp://127.0.0.1:5555" |
| assert make_zmq_path("tcp", "::1", "5555") == "tcp://[::1]:5555" |
|
|
|
|
| def test_get_tcp_uri(): |
| assert get_tcp_uri("127.0.0.1", 5555) == "tcp://127.0.0.1:5555" |
| assert get_tcp_uri("::1", 5555) == "tcp://[::1]:5555" |
|
|
|
|
| def test_split_host_port(): |
| |
| assert split_host_port("127.0.0.1:5555") == ("127.0.0.1", 5555) |
| |
| with pytest.raises(ValueError): |
| |
| assert split_host_port("127.0.0.1::5555") |
| with pytest.raises(ValueError): |
| |
| assert split_host_port("127.0.0.1:5555:") |
| with pytest.raises(ValueError): |
| |
| assert split_host_port("127.0.0.15555") |
| with pytest.raises(ValueError): |
| |
| assert split_host_port("127.0.0.1:5555a") |
|
|
| |
| assert split_host_port("[::1]:5555") == ("::1", 5555) |
| |
| with pytest.raises(ValueError): |
| |
| assert split_host_port("[::1]::5555") |
| with pytest.raises(IndexError): |
| |
| assert split_host_port("[::1]5555") |
| with pytest.raises(ValueError): |
| |
| assert split_host_port("[::1]:5555a") |
|
|
|
|
| def test_join_host_port(): |
| assert join_host_port("127.0.0.1", 5555) == "127.0.0.1:5555" |
| assert join_host_port("::1", 5555) == "[::1]:5555" |
|
|