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 .utilities .port_manager  import  (
26+     PortManager ,
27+     find_free_network_port ,
28+     get_port_manager ,
29+ )
2530
2631# ============================================================================= 
2732# Fixtures 
@@ -143,7 +148,7 @@ def allocate_ports():
143148    assert  len (set (ports )) ==  100 , f"Expected 100 unique ports, got { len (set (ports ))}  
144149
145150    # Check for any duplicates 
146-     counts  =  Counter (ports )
151+     counts  =  collections . Counter (ports )
147152    duplicates  =  {port : count  for  port , count  in  counts .items () if  count  >  1 }
148153    assert  not  duplicates , f"Found duplicate ports: { duplicates }  
149154
@@ -714,9 +719,14 @@ def test_port_manager_recently_released_prevents_immediate_reuse():
714719    manager .release_port (new_port )
715720
716721
717- def  test_port_manager_recently_released_queue_cycles ():
722+ def  _set_recently_released_limit (monkeypatch , value : int ) ->  None :
723+     monkeypatch .setattr (port_manager_module , "_RECENTLY_RELEASED_PORTS_MAXLEN" , value , raising = True )
724+     monkeypatch .setattr (port_state_module , "_RECENTLY_RELEASED_MAX_LEN" , value , raising = True )
725+ 
726+ 
727+ def  test_port_manager_recently_released_queue_cycles (monkeypatch ):
718728    """Test that recently_released queue cycles after maxlen allocations.""" 
719-     from   lightning . fabric . utilities . port_manager   import   _RECENTLY_RELEASED_PORTS_MAXLEN 
729+     _set_recently_released_limit ( monkeypatch ,  64 ) 
720730
721731    manager  =  PortManager ()
722732
@@ -727,8 +737,10 @@ def test_port_manager_recently_released_queue_cycles():
727737    # Port should be in recently_released queue 
728738    assert  first_port  in  manager ._recently_released 
729739
740+     queue_limit  =  port_manager_module ._RECENTLY_RELEASED_PORTS_MAXLEN 
741+ 
730742    # Allocate and release many ports to fill the queue beyond maxlen 
731-     for  _  in  range (_RECENTLY_RELEASED_PORTS_MAXLEN  +  10 ):
743+     for  _  in  range (queue_limit  +  10 ):
732744        port  =  manager .allocate_port ()
733745        manager .release_port (port )
734746
@@ -755,14 +767,20 @@ def test_port_manager_reserve_clears_recently_released():
755767    manager .release_port (port )
756768
757769
758- def  test_port_manager_high_queue_utilization_warning (caplog ):
770+ def  test_port_manager_high_queue_utilization_warning (monkeypatch ,  caplog ):
759771    """Test that warning is logged when queue utilization exceeds 80%.""" 
760772    import  logging 
761773
774+     _set_recently_released_limit (monkeypatch , 64 )
775+ 
776+     queue_limit  =  port_manager_module ._RECENTLY_RELEASED_PORTS_MAXLEN 
777+     trigger_count  =  int (queue_limit  *  0.8 ) +  1   # Just over 80% 
778+     expected_pct  =  (trigger_count  /  queue_limit ) *  100 
779+ 
762780    manager  =  PortManager ()
763781
764-     # Fill queue to >80% (821/1024 = 80.2%)  
765-     for  _  in  range (821 ):
782+     # Fill queue to just over 80%  
783+     for  _  in  range (trigger_count ):
766784        port  =  manager .allocate_port ()
767785        manager .release_port (port )
768786
@@ -773,4 +791,4 @@ def test_port_manager_high_queue_utilization_warning(caplog):
773791
774792    # Verify warning was logged 
775793    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% 
794+     assert  any (f" { expected_pct :.1f } % "in  record .message  for  record  in  caplog .records )
0 commit comments