@@ -214,59 +214,46 @@ func TestAgent_Stats_Magic(t *testing.T) {
214
214
_ , b , _ , ok := runtime .Caller (0 )
215
215
require .True (t , ok )
216
216
dir := filepath .Join (filepath .Dir (b ), "../scripts/echoserver/main.go" )
217
+ echoServerCmd := exec .Command ("go" , "run" , dir ,
218
+ "-D" , agentssh .MagicProcessCmdlineJetBrains )
219
+ stdout , err := echoServerCmd .StdoutPipe ()
220
+ require .NoError (t , err )
221
+ err = echoServerCmd .Start ()
222
+ require .NoError (t , err )
223
+ defer echoServerCmd .Process .Kill ()
217
224
218
- spawnServer := func (network string ) (string , * exec.Cmd ) {
219
- echoServerCmd := exec .Command ("go" , "run" , dir ,
220
- network , "-D" , agentssh .MagicProcessCmdlineJetBrains )
221
- stdout , err := echoServerCmd .StdoutPipe ()
222
- require .NoError (t , err )
223
- err = echoServerCmd .Start ()
224
- require .NoError (t , err )
225
- t .Cleanup (func () {
226
- echoServerCmd .Process .Kill ()
227
- })
228
-
229
- // The echo server prints its port as the first line.
230
- sc := bufio .NewScanner (stdout )
231
- sc .Scan ()
232
- return sc .Text (), echoServerCmd
233
- }
234
-
235
- port4 , cmd4 := spawnServer ("tcp4" )
236
- port6 , cmd6 := spawnServer ("tcp6" )
225
+ // The echo server prints its port as the first line.
226
+ sc := bufio .NewScanner (stdout )
227
+ sc .Scan ()
228
+ remotePort := sc .Text ()
237
229
238
230
//nolint:dogsled
239
231
conn , _ , stats , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 )
240
- defer conn .Close ()
241
-
242
232
sshClient , err := conn .SSHClient (ctx )
243
233
require .NoError (t , err )
244
234
245
- tunnel4 , err := sshClient .Dial ("tcp4" , fmt .Sprintf ("127.0.0.1:%s" , port4 ))
246
- require .NoError (t , err )
247
- defer tunnel4 .Close ()
248
-
249
- tunnel6 , err := sshClient .Dial ("tcp6" , fmt .Sprintf ("[::]:%s" , port6 ))
235
+ tunneledConn , err := sshClient .Dial ("tcp" , fmt .Sprintf ("127.0.0.1:%s" , remotePort ))
250
236
require .NoError (t , err )
251
- defer tunnel6 .Close ()
237
+ t .Cleanup (func () {
238
+ // always close on failure of test
239
+ _ = conn .Close ()
240
+ _ = tunneledConn .Close ()
241
+ })
252
242
253
243
require .Eventuallyf (t , func () bool {
254
244
s , ok := <- stats
255
245
t .Logf ("got stats with conn open: ok=%t, ConnectionCount=%d, SessionCountJetBrains=%d" ,
256
246
ok , s .ConnectionCount , s .SessionCountJetBrains )
257
247
return ok && s .ConnectionCount > 0 &&
258
- s .SessionCountJetBrains == 2
248
+ s .SessionCountJetBrains == 1
259
249
}, testutil .WaitLong , testutil .IntervalFast ,
260
250
"never saw stats with conn open" ,
261
251
)
262
252
263
253
// Kill the server and connection after checking for the echo.
264
- requireEcho (t , tunnel4 )
265
- requireEcho (t , tunnel6 )
266
- _ = cmd4 .Process .Kill ()
267
- _ = cmd6 .Process .Kill ()
268
- _ = tunnel4 .Close ()
269
- _ = tunnel6 .Close ()
254
+ requireEcho (t , tunneledConn )
255
+ _ = echoServerCmd .Process .Kill ()
256
+ _ = tunneledConn .Close ()
270
257
271
258
require .Eventuallyf (t , func () bool {
272
259
s , ok := <- stats
0 commit comments