1313# limitations under the License.
1414"""Tests for the PortManager utility and port allocation integration."""
1515
16+ import collections
1617import os
1718import socket
1819import threading
19- from collections import Counter
2020
2121import pytest
2222
23- from lightning .fabric .plugins .environments .lightning import find_free_network_port
24- from lightning .fabric .utilities .port_manager import PortManager , get_port_manager
23+ import lightning .fabric .utilities .port_manager as port_manager_module
24+ import lightning .fabric .utilities .port_state as port_state_module
25+ from lightning .fabric .plugins .environments .lightning import (
26+ find_free_network_port as env_find_free_network_port ,
27+ )
28+ from lightning .fabric .utilities .port_manager import (
29+ PortManager ,
30+ find_free_network_port ,
31+ get_port_manager ,
32+ )
2533
2634# =============================================================================
2735# Fixtures
@@ -143,7 +151,7 @@ def allocate_ports():
143151 assert len (set (ports )) == 100 , f"Expected 100 unique ports, got { len (set (ports ))} "
144152
145153 # Check for any duplicates
146- counts = Counter (ports )
154+ counts = collections . Counter (ports )
147155 duplicates = {port : count for port , count in counts .items () if count > 1 }
148156 assert not duplicates , f"Found duplicate ports: { duplicates } "
149157
@@ -495,6 +503,20 @@ def allocate():
495503 manager .release_port (port )
496504
497505
506+ def test_find_free_network_port_alias (monkeypatch ):
507+ """Legacy environment alias should reuse the port manager backed implementation."""
508+
509+ manager = get_port_manager ()
510+ manager .release_all ()
511+
512+ port = env_find_free_network_port ()
513+
514+ try :
515+ assert port in manager ._allocated_ports
516+ finally :
517+ manager .release_port (port )
518+
519+
498520def test_port_allocation_simulates_distributed_test_lifecycle ():
499521 """Simulate the lifecycle of a distributed test with port allocation and release."""
500522 manager = get_port_manager ()
@@ -714,9 +736,14 @@ def test_port_manager_recently_released_prevents_immediate_reuse():
714736 manager .release_port (new_port )
715737
716738
717- def test_port_manager_recently_released_queue_cycles ():
739+ def _set_recently_released_limit (monkeypatch , value : int ) -> None :
740+ monkeypatch .setattr (port_manager_module , "_RECENTLY_RELEASED_PORTS_MAXLEN" , value , raising = True )
741+ monkeypatch .setattr (port_state_module , "_RECENTLY_RELEASED_MAX_LEN" , value , raising = True )
742+
743+
744+ def test_port_manager_recently_released_queue_cycles (monkeypatch ):
718745 """Test that recently_released queue cycles after maxlen allocations."""
719- from lightning . fabric . utilities . port_manager import _RECENTLY_RELEASED_PORTS_MAXLEN
746+ _set_recently_released_limit ( monkeypatch , 64 )
720747
721748 manager = PortManager ()
722749
@@ -727,8 +754,10 @@ def test_port_manager_recently_released_queue_cycles():
727754 # Port should be in recently_released queue
728755 assert first_port in manager ._recently_released
729756
757+ queue_limit = port_manager_module ._RECENTLY_RELEASED_PORTS_MAXLEN
758+
730759 # Allocate and release many ports to fill the queue beyond maxlen
731- for _ in range (_RECENTLY_RELEASED_PORTS_MAXLEN + 10 ):
760+ for _ in range (queue_limit + 10 ):
732761 port = manager .allocate_port ()
733762 manager .release_port (port )
734763
@@ -755,14 +784,20 @@ def test_port_manager_reserve_clears_recently_released():
755784 manager .release_port (port )
756785
757786
758- def test_port_manager_high_queue_utilization_warning (caplog ):
787+ def test_port_manager_high_queue_utilization_warning (monkeypatch , caplog ):
759788 """Test that warning is logged when queue utilization exceeds 80%."""
760789 import logging
761790
791+ _set_recently_released_limit (monkeypatch , 64 )
792+
793+ queue_limit = port_manager_module ._RECENTLY_RELEASED_PORTS_MAXLEN
794+ trigger_count = int (queue_limit * 0.8 ) + 1 # Just over 80%
795+ expected_pct = (trigger_count / queue_limit ) * 100
796+
762797 manager = PortManager ()
763798
764- # Fill queue to >80% (821/1024 = 80.2%)
765- for _ in range (821 ):
799+ # Fill queue to just over 80%
800+ for _ in range (trigger_count ):
766801 port = manager .allocate_port ()
767802 manager .release_port (port )
768803
@@ -773,4 +808,4 @@ def test_port_manager_high_queue_utilization_warning(caplog):
773808
774809 # Verify warning was logged
775810 assert any ("Port queue utilization high" in record .message for record in caplog .records )
776- assert any ("80. " in record .message for record in caplog .records ) # Should show 80.x%
811+ assert any (f" { expected_pct :.1f } % " in record .message for record in caplog .records )
0 commit comments