MCPcopy Create free account
hub / github.com/coder/coder / TestNewServer_CloseActiveConnections

Function TestNewServer_CloseActiveConnections

agent/agentssh/agentssh_test.go:150–256  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

148}
149
150func TestNewServer_CloseActiveConnections(t *testing.T) {
151 t.Parallel()
152
153 prepare := func(ctx context.Context, t *testing.T) (*agentssh.Server, func()) {
154 t.Helper()
155 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
156 s, err := agentssh.NewServer(ctx, logger, prometheus.NewRegistry(), afero.NewMemMapFs(), agentexec.DefaultExecer, nil)
157 require.NoError(t, err)
158 t.Cleanup(func() {
159 _ = s.Close()
160 })
161 err = s.UpdateHostSigner(42)
162 assert.NoError(t, err)
163
164 ln, err := net.Listen("tcp", "127.0.0.1:0")
165 require.NoError(t, err)
166
167 waitConns := make([]chan struct{}, 4)
168
169 var wg sync.WaitGroup
170 wg.Add(1 + len(waitConns))
171
172 go func() {
173 defer wg.Done()
174 err := s.Serve(ln)
175 assert.Error(t, err) // Server is closed.
176 }()
177
178 for i := 0; i < len(waitConns); i++ {
179 waitConns[i] = make(chan struct{})
180 go func(ch chan struct{}) {
181 defer wg.Done()
182 c := sshClient(t, ln.Addr().String())
183 sess, err := c.NewSession()
184 assert.NoError(t, err)
185 pty := ptytest.New(t)
186 sess.Stdin = pty.Input()
187 sess.Stdout = pty.Output()
188 sess.Stderr = pty.Output()
189
190 // Every other session will request a PTY.
191 if i%2 == 0 {
192 err = sess.RequestPty("xterm", 80, 80, nil)
193 assert.NoError(t, err)
194 }
195 // The 60 seconds here is intended to be longer than the
196 // test. The shutdown should propagate.
197 if runtime.GOOS == "windows" {
198 // Best effort to at least partially test this in Windows.
199 err = sess.Start("echo start\"ed\" && sleep 60")
200 } else {
201 err = sess.Start("/bin/bash -c 'trap \"sleep 60\" SIGTERM; echo start\"ed\"; sleep 60'")
202 }
203 assert.NoError(t, err)
204
205 // Allow the session to settle (i.e. reach echo).
206 pty.ExpectMatchContext(ctx, "started")
207 // Sleep a bit to ensure the sleep has started.

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
UpdateHostSignerMethod · 0.95
ServeMethod · 0.95
ShutdownMethod · 0.95
NewServerFunction · 0.92
NewFunction · 0.92
ContextFunction · 0.92
sshClientFunction · 0.85
NewSessionMethod · 0.80
ExpectMatchContextMethod · 0.80
FatalMethod · 0.80
HelperMethod · 0.65

Tested by

no test coverage detected