MCPcopy Index your code
hub / github.com/coder/coder / TestServer_AIGatewayShutdownOrdering

Function TestServer_AIGatewayShutdownOrdering

cli/server_test.go:2245–2282  ·  view source on GitHub ↗

TestServer_AIGatewayShutdownOrdering is a regression test for a shutdown ordering bug. The in-memory AI Gateway daemon registers itself with the API WebsocketWaitGroup, so it must be closed before coderAPICloser.Close() waits on that group. If it isn't, API.Close() blocks for the full 10s WebsocketW

(t *testing.T)

Source from the content-addressed store, hash-verified

2243// Windows test-go-pg this extra shutdown tail overlapped across concurrent
2244// package binaries and OOMed the runner.
2245func TestServer_AIGatewayShutdownOrdering(t *testing.T) {
2246 t.Parallel()
2247
2248 ctx, cancel := context.WithCancel(testutil.Context(t, testutil.WaitLong))
2249 defer cancel()
2250
2251 inv, cfg := clitest.New(t,
2252 "server",
2253 dbArg(t),
2254 "--http-address", ":0",
2255 "--access-url", "http://example.com",
2256 "--cache-dir", t.TempDir(),
2257 // Explicit so the test catches the regression even if the
2258 // default for ai-gateway-enabled is ever flipped back to false.
2259 "--ai-gateway-enabled=true",
2260 )
2261
2262 serverErr := make(chan error, 1)
2263 go func() {
2264 serverErr <- inv.WithContext(ctx).Run()
2265 }()
2266
2267 // Wait for the server to come up so the in-memory AI Gateway daemon
2268 // is registered with the API and the WebsocketWaitGroup is nonzero.
2269 _ = waitAccessURL(t, cfg)
2270
2271 // The WebsocketWaitGroup timeout in coderd.API.Close() is hard coded
2272 // to 10s, so any value comfortably below 10s catches the regression
2273 // while leaving headroom for slow CI runners.
2274 shutdownStart := time.Now()
2275 cancel()
2276 if err := <-serverErr; err != nil {
2277 require.ErrorIs(t, err, context.Canceled)
2278 }
2279 require.Less(t, time.Since(shutdownStart), 8*time.Second,
2280 "graceful shutdown took too long; the in-memory AI Gateway daemon is "+
2281 "likely not being closed before coderAPICloser.Close()")
2282}
2283
2284func TestServer_GracefulShutdown(t *testing.T) {
2285 t.Parallel()

Callers

nothing calls this directly

Calls 8

ContextFunction · 0.92
NewFunction · 0.92
WithContextMethod · 0.80
LessMethod · 0.80
dbArgFunction · 0.70
waitAccessURLFunction · 0.70
TempDirMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected