@@ -188,26 +188,32 @@ public void RoundTrip_Tcp_McuAsServer_CompanionConnects()
188188 server . Bind ( new IPEndPoint ( IPAddress . Any , mcuListenPort ) ) ;
189189 server . Listen ( 1 ) ;
190190
191- using ( var companion = new CompanionClient ( ) )
191+ try
192192 {
193- Assert . IsTrue ( companion . Ping ( ) , "Companion not reachable" ) ;
194-
195- // Resolve the MCU's own IP - the companion must connect back to it.
196- string mcuIp = GetLocalIp ( ) ;
197- Assert . IsTrue ( companion . ConnectTo ( mcuIp , mcuListenPort ) , "ConnectTo command failed" ) ;
193+ using ( var companion = new CompanionClient ( ) )
194+ {
195+ Assert . IsTrue ( companion . Ping ( ) , "Companion not reachable" ) ;
198196
199- // ConnectTo is synchronous on the companion side: by the time we get ok,
200- // the connection is already sitting in the listen backlog.
201- // Poll with 10 s timeout (microseconds) to avoid blocking forever.
202- Assert . IsTrue ( server . Poll ( 10 * 1000 * 1000 , SelectMode . SelectRead ) , "No connection received from companion within timeout" ) ;
203- Socket accepted = server . Accept ( ) ;
197+ string mcuIp = GetLocalIp ( ) ;
198+ Assert . IsTrue ( companion . ConnectTo ( mcuIp , mcuListenPort ) , "ConnectTo command failed" ) ;
204199
205- Assert . IsNotNull ( accepted , "Accept returned null" ) ;
200+ Assert . IsTrue ( server . Poll ( 10 * 1000 * 1000 , SelectMode . SelectRead ) , "No connection received from companion within timeout" ) ;
201+ Socket accepted = server . Accept ( ) ;
206202
207- accepted . Close ( ) ;
203+ try
204+ {
205+ Assert . IsNotNull ( accepted , "Accept returned null" ) ;
206+ }
207+ finally
208+ {
209+ accepted . Close ( ) ;
210+ }
211+ }
212+ }
213+ finally
214+ {
215+ server . Close ( ) ;
208216 }
209-
210- server . Close ( ) ;
211217 }
212218
213219 private static string GetLocalIp ( )
0 commit comments