Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76d3a24

Browse files
committed
implement unit test to verify jetbrains functionality
1 parent 2447970 commit 76d3a24

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

agent/agent_test.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,41 @@ func TestAgent_Stats_Magic(t *testing.T) {
206206
remotePort := tcpAddr.Port
207207
go echoOnce(t, rl)
208208

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)
210213

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))
212215
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+
)
215244
})
216245
}
217246

agent/agentssh/jetbrainstrack.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package agentssh
22

33
import (
4+
"sync"
5+
46
"cdr.dev/slog"
57
"go.uber.org/atomic"
68
gossh "golang.org/x/crypto/ssh"
@@ -54,10 +56,13 @@ func (w *ChannelAcceptWatcher) Accept() (gossh.Channel, <-chan *gossh.Request, e
5456

5557
type ChannelOnClose struct {
5658
gossh.Channel
59+
// once ensures close only decrements the counter once.
60+
// Because close can be called multiple times.
61+
once sync.Once
5762
done func()
5863
}
5964

6065
func (c *ChannelOnClose) Close() error {
61-
c.done()
66+
c.once.Do(c.done)
6267
return c.Channel.Close()
6368
}

0 commit comments

Comments
 (0)