66
77import unittest
88
9- from crossbench .plt .port_manager import PortForwardException
9+ from crossbench .plt .port_manager import PortForwardException , PortManager
1010from tests import test_helper
1111from tests .crossbench .mock_helper import LinuxMockPlatform
1212
@@ -49,33 +49,35 @@ class PortManagerTestCase(unittest.TestCase):
4949 def setUp (self ):
5050 super ().setUp ()
5151 self .platform = FakePortLinuxMockPlatform ()
52- self .port_manager = self .platform .ports
53- self .port_manager . assert_is_active ()
52+ self .port_scope = self .platform .ports
53+ self .port_manager : PortManager = self . platform . port_manager
5454
5555 def tearDown (self ):
5656 self .assertFalse (self .platform .forwarded_ports )
5757 self .assertFalse (self .platform .reverse_forwarded_ports )
58- self .assertTrue (self .port_manager .is_empty )
58+ self .assertTrue (self .port_scope .is_empty )
5959 super ().tearDown ()
6060
6161 def test_default (self ):
62- self .assertTrue (self .port_manager .is_empty )
62+ self .assertTrue (self .port_scope .is_empty )
6363 self .assertFalse (self .port_manager .has_nested_scopes )
64- self .port_manager .assert_is_active ()
6564
6665 def test_nested (self ):
67- with self .port_manager .nested ():
66+ self .assertTrue (self .port_scope .is_empty )
67+ with self .port_scope .nested () as scope :
6868 self .assertFalse (self .port_manager .is_empty )
69+ self .assertTrue (self .port_scope .is_empty )
70+ self .assertTrue (scope .is_empty )
6971 self .assertTrue (self .port_manager .has_nested_scopes )
70- self .port_manager . assert_is_active ( )
72+ self .assertTrue ( self . port_scope . is_empty )
7173
7274 def test_stop (self ):
7375 self .port_manager .stop ()
74- self .assertTrue (self .port_manager .is_empty )
76+ self .assertTrue (self .port_scope .is_empty )
7577 self .assertFalse (self .port_manager .has_nested_scopes )
7678
7779 def test_forward_port (self ):
78- with self .port_manager .nested () as port_scope :
80+ with self .port_scope .nested () as port_scope :
7981 returned_local_port = port_scope .forward (12345 , 8080 )
8082 self .assertEqual (returned_local_port , 12345 )
8183 self .assertIn (12345 , self .platform .forwarded_ports )
@@ -85,90 +87,90 @@ def test_forward_port(self):
8587 self .assertTrue (port_scope .is_empty )
8688
8789 def test_forward_port_auto_assign (self ):
88- with self .port_manager .nested () as port_scope :
90+ with self .port_scope .nested () as port_scope :
8991 returned_local_port = port_scope .forward (0 , 8080 )
9092 self .assertEqual (returned_local_port , 60001 )
9193 self .assertIn (60001 , self .platform .forwarded_ports )
9294
9395 def test_stop_forward_port (self ):
94- with self .port_manager .nested () as port_scope :
96+ with self .port_scope .nested () as port_scope :
9597 port_scope .forward (12345 , 8080 )
9698 port_scope .stop_forward (12345 )
9799 self .assertNotIn (12345 , self .platform .forwarded_ports )
98100
99101 def test_forward_port_conflict (self ):
100- with self .port_manager .nested () as port_scope :
102+ with self .port_scope .nested () as port_scope :
101103 port_scope .forward (12345 , 8080 )
102104 with self .assertRaises (PortForwardException ):
103105 # Try to forward same local port
104106 port_scope .forward (12345 , 8081 )
105107
106108 def test_stop_forward_port_not_forwarded (self ):
107- with self .port_manager .nested () as port_scope :
109+ with self .port_scope .nested () as port_scope :
108110 with self .assertRaises (PortForwardException ):
109111 port_scope .stop_forward (12345 )
110112
111113 def test_reverse_forward_port (self ):
112- with self .port_manager .nested () as port_scope :
114+ with self .port_scope .nested () as port_scope :
113115 returned_remote_port = port_scope .reverse_forward (54321 , 8081 )
114116 self .assertEqual (returned_remote_port , 54321 )
115117 self .assertIn (54321 , self .platform .reverse_forwarded_ports )
116118 self .assertEqual (self .platform .reverse_forwarded_ports [54321 ], 8081 )
117119 self .assertFalse (port_scope .is_empty )
118120
119121 def test_reverse_forward_port_auto_assign (self ):
120- with self .port_manager .nested () as port_scope :
122+ with self .port_scope .nested () as port_scope :
121123 returned_remote_port = port_scope .reverse_forward (0 , 8081 )
122124 self .assertEqual (returned_remote_port , 60001 )
123125 self .assertIn (60001 , self .platform .reverse_forwarded_ports )
124126
125127 def test_stop_reverse_forward_port (self ):
126- with self .port_manager .nested () as port_scope :
128+ with self .port_scope .nested () as port_scope :
127129 port_scope .reverse_forward (54321 , 8081 )
128130 port_scope .stop_reverse_forward (54321 )
129131 self .assertNotIn (54321 , self .platform .reverse_forwarded_ports )
130132
131133 def test_reverse_forward_port_conflict (self ):
132- with self .port_manager .nested () as port_scope :
134+ with self .port_scope .nested () as port_scope :
133135 port_scope .reverse_forward (54321 , 8081 )
134136 with self .assertRaises (PortForwardException ):
135137 # Try to reverse forward same remote port
136138 port_scope .reverse_forward (54321 , 8082 )
137139
138140 def test_stop_reverse_forward_port_not_forwarded (self ):
139- with self .port_manager .nested () as port_scope :
141+ with self .port_scope .nested () as port_scope :
140142 with self .assertRaises (PortForwardException ):
141143 port_scope .stop_reverse_forward (54321 )
142144
143145 def test_nested_cleanup (self ):
144- self .port_manager .forward (1111 , 2222 )
145- with self .port_manager .nested () as port_scope :
146+ self .port_scope .forward (1111 , 2222 )
147+ with self .port_scope .nested () as port_scope :
146148 port_scope .forward (3333 , 4444 )
147149 self .assertIn (1111 , self .platform .forwarded_ports )
148150 self .assertNotIn (3333 , self .platform .forwarded_ports )
149151 self .assertFalse (self .port_manager .has_nested_scopes )
150152 self .port_manager .stop ()
151- self .assertTrue (self .port_manager .is_empty )
153+ self .assertTrue (self .port_scope .is_empty )
152154 self .assertNotIn (1111 , self .platform .forwarded_ports )
153155 self .assertNotIn (3333 , self .platform .forwarded_ports )
154156
155157 def test_forward_nested_cleanup_stop_outer (self ):
156- self .port_manager .forward (1111 , 2222 )
157- with self .port_manager .nested () as port_scope :
158+ self .port_scope .forward (1111 , 2222 )
159+ with self .port_scope .nested () as port_scope :
158160 port_scope .forward (3333 , 4444 )
159161 port_scope .stop_forward (3333 )
160162 with self .assertRaisesRegex (PortForwardException , "1111" ):
161163 port_scope .stop_forward (1111 )
162- self .port_manager .stop_forward (1111 )
164+ self .port_scope .stop_forward (1111 )
163165
164166 def test_reverse_forward_nested_cleanup_stop_outer (self ):
165- self .port_manager .reverse_forward (1111 , 2222 )
166- with self .port_manager .nested () as port_scope :
167+ self .port_scope .reverse_forward (1111 , 2222 )
168+ with self .port_scope .nested () as port_scope :
167169 port_scope .reverse_forward (3333 , 4444 )
168170 port_scope .stop_reverse_forward (3333 )
169171 with self .assertRaisesRegex (PortForwardException , "1111" ):
170172 port_scope .stop_reverse_forward (1111 )
171- self .port_manager .stop_reverse_forward (1111 )
173+ self .port_scope .stop_reverse_forward (1111 )
172174
173175
174176if __name__ == "__main__" :
0 commit comments