Skip to content

test(enterprise/cli): use quartz clock in standalone AI Gateway shutdown tests - #32095

Merged
mollymaefraser merged 12 commits into
mainfrom
molly/aigov-557-use-quartz-for-standalone-ai-gateway
Nov 17, 2026
Merged

mollymaefraser merged 12 commits into
mainfrom
molly/aigov-557-use-quartz-for-standalone-ai-gateway

Conversation

@mollymaefraser

@mollymaefraser mollymaefraser commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Injects a quartz.Clock into the standalone AI Gateway so its shutdown timeouts (provider reload, HTTP drain and daemon shutdown) can be driven by a mock clock in tests, as described in AIGOV-557. Production uses the real clock, so shutdown behavior, timings and error values are unchanged.

Previously each timeout used context.WithTimeout, so tests could only check shutdown ordering through channels and require.Eventually, and none of the timeout paths were covered. Each timeout is now built from the gateway's clock and tagged (aigateway plus provider_reload_shutdown, http_shutdown or daemon_shutdown). The shutdown order tests trap each timer to assert the gateway's state at the start of every phase, which keeps the ordering invariant covered: provider reload stops before HTTP draining, and daemon shutdown starts only after HTTP serving stops. New tests advance the mock clock past each timeout and check that shutdown gives up waiting, reports context.DeadlineExceeded and carries on.

Refs AIGOV-557

Notes for reviewers

  • clockTimeout returns a small timeoutContext wrapper that matches context.WithTimeout: Deadline() reports when the clock timer fires, and Err() reports context.DeadlineExceeded once it has fired. Without it, a context canceled with a cause reports context.Canceled from Err(). Every consumer of these shutdown contexts (http.Server.Shutdown, aibridged.Server.Shutdown, InflightGate.Shutdown and the pool) reads ctx.Err() on the context it was given; contexts derived from the wrapper would still report context.Canceled, which the doc comment notes.
  • The provider reload timer is stopped by a defer when serve returns, so any future early return still cleans it up. Because that timer is still pending during HTTP draining, the HTTP timeout test advances the mock clock past it first and then the rest of the way to the HTTP shutdown timeout.
  • The clock is injected through standaloneGatewayParams and defaults to quartz.NewReal() when nil, so the CLI and existing tests need no changes. There are no test-only code paths in production.
  • shutdownWithTimeout (enterprise/cli/proxyserver.go) is unchanged because the workspace proxy also uses it. The trace shutdown timeout in the CLI command is also unchanged, because it runs outside the gateway and is out of scope for this ticket.
  • Quartz has no context helper, so clockTimeout is similar to timeout in agent/x/agentacp/manager.go. Sharing one helper could be a follow-up.
Implementation notes
  • enterprise/cli/aigatewaystart.go:
    • clock quartz.Clock on standaloneGatewayParams and standaloneGateway; newStandaloneGateway falls back to quartz.NewReal().
    • clockTimeout(ctx, clock, d, tags...): context.WithCancelCause plus clock.AfterFunc, wrapped in timeoutContext, which also records the deadline from clock.Now(). The returned cancel stops the timer.
    • The provider reload and HTTP drain timeouts in serve and the daemon timeout in shutdownDaemon use clockTimeout with s.clock.
  • enterprise/cli/aigatewaystart_internal_test.go:
    • withClock test option passes a mock clock to newTestStandaloneGateway.
    • TestClockTimeout_Deadline: the context's Deadline() is the mock clock's time plus the timeout.
    • TestStandaloneGatewayServe_ShutdownOrder traps the provider reload and HTTP drain timers: when provider reload starts stopping, HTTP draining has not begun and the listener and DRPC connection are open; when HTTP draining is about to start, provider refresh has stopped.
    • TestRunStandaloneGateway_HTTPStopsBeforeDaemonShutdown traps the daemon shutdown timer: provider refresh has stopped and the listener is closed before the daemon shutdown begins.
    • TestStandaloneGatewayServe_ProviderReloadShutdownTimeout: the reloader ignores cancellation; after 5s, serve returns "provider reload did not stop within", does not mark provider refresh stopped, and still closes the listener.
    • TestStandaloneGatewayServe_HTTPShutdownTimeout: an in-flight request never finishes; after 5m, serve force-closes the server, the request is cut off and serve returns "shutdown http server:" with context.DeadlineExceeded.
    • TestRunStandaloneGateway_DaemonShutdownTimeout: the pool shutdown never returns; after 5s, run returns "shutdown AI Gateway daemon:" with context.DeadlineExceeded and the DRPC connection is not marked closed.

Code written by @mollymaefraser, with Coder Agents used as a guide: explaining the existing code path, reviewing each step, and running tests.

@mollymaefraser mollymaefraser self-assigned this Nov 12, 2026
@mollymaefraser
mollymaefraser temporarily deployed to main November 12, 2026 16:36 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 12, 2026 16:36 — with GitHub Actions Inactive
@linear-code

linear-code Bot commented Nov 12, 2026

Copy link
Copy Markdown

AIGOV-557

@mollymaefraser
mollymaefraser temporarily deployed to main November 12, 2026 16:37 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 12, 2026 16:39 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 12, 2026 16:56 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 08:29 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 08:51 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 10:00 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 10:35 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 11:00 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 11:10 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 11:22 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 11:27 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 13:41 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 13:44 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser marked this pull request as ready for review November 13, 2026 13:49
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 13:49 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 13, 2026 13:49 — with GitHub Actions Inactive
Comment thread enterprise/cli/aigatewaystart.go Resolved
Comment thread enterprise/cli/aigatewaystart.go Resolved
@mollymaefraser
mollymaefraser temporarily deployed to main November 16, 2026 11:12 — with GitHub Actions Inactive
@mollymaefraser
mollymaefraser temporarily deployed to main November 16, 2026 11:14 — with GitHub Actions Inactive

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great job 💪 thanks for addressing the comments

@mollymaefraser
mollymaefraser merged commit e9c4fb1 into main Nov 17, 2026
23 of 24 checks passed
@mollymaefraser
mollymaefraser deleted the molly/aigov-557-use-quartz-for-standalone-ai-gateway branch November 17, 2026 13:15
@mollymaefraser
mollymaefraser temporarily deployed to main November 17, 2026 13:15 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 17, 2026

This branch had an error being deployed

1 failed and 1 inactive deployments
docs-preview — 24d3af8c Deployed Nov 17, 2026 by mollymaefraser via cleanup #164
main — 24d3af8c Deployed Nov 17, 2026 by mollymaefraser via cla #258647
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants