@@ -206,12 +206,41 @@ func TestAgent_Stats_Magic(t *testing.T) {
206
206
remotePort := tcpAddr .Port
207
207
go echoOnce (t , rl )
208
208
209
- sshClient := setupAgentSSHClient (ctx , t )
209
+ //nolint:dogsled
210
+ conn , _ , stats , _ , _ := setupAgent (t , agentsdk.Manifest {}, 0 )
211
+ sshClient , err := conn .SSHClient (ctx )
212
+ require .NoError (t , err )
210
213
211
- conn , err := sshClient .Dial ("tcp" , fmt .Sprintf ("127.0.0.1:%d" , remotePort ))
214
+ tunneledConn , err := sshClient .Dial ("tcp" , fmt .Sprintf ("127.0.0.1:%d" , remotePort ))
212
215
require .NoError (t , err )
213
- defer conn .Close ()
214
- requireEcho (t , conn )
216
+ t .Cleanup (func () {
217
+ // always close on failure of test
218
+ _ = conn .Close ()
219
+ _ = tunneledConn .Close ()
220
+ })
221
+
222
+ var s * agentsdk.Stats
223
+ require .Eventuallyf (t , func () bool {
224
+ var ok bool
225
+ s , ok = <- stats
226
+ return ok && s .ConnectionCount > 0 &&
227
+ s .SessionCountJetBrains == 1
228
+ }, testutil .WaitLong , testutil .IntervalFast ,
229
+ "never saw stats with conn open: %+v" , s ,
230
+ )
231
+
232
+ // Manually closing the connection
233
+ requireEcho (t , tunneledConn )
234
+ _ = rl .Close ()
235
+
236
+ require .Eventuallyf (t , func () bool {
237
+ var ok bool
238
+ s , ok = <- stats
239
+ return ok && s .ConnectionCount == 0 &&
240
+ s .SessionCountJetBrains == 0
241
+ }, testutil .WaitLong , testutil .IntervalFast ,
242
+ "never saw stats after conn closes: %+v" , s ,
243
+ )
215
244
})
216
245
}
217
246
0 commit comments