Repository navigation
test(enterprise/cli): use quartz clock in standalone AI Gateway shutdown tests - #32095
Merged
mollymaefraser merged 12 commits intoNov 17, 2026
Merged
Conversation
…eway shutdown order test
…way shutdown timeouts
ssncferreira
approved these changes
Nov 16, 2026
ssncferreira
left a comment
Collaborator
There was a problem hiding this comment.
LGTM, great job 💪 thanks for addressing the comments
mollymaefraser
deleted the
molly/aigov-557-use-quartz-for-standalone-ai-gateway
branch
November 17, 2026 13:15
mollymaefraser
had a problem deploying
to
docs-preview
November 17, 2026 13:15 — with
GitHub Actions
Failure
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Injects a
quartz.Clockinto 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 andrequire.Eventually, and none of the timeout paths were covered. Each timeout is now built from the gateway's clock and tagged (aigatewayplusprovider_reload_shutdown,http_shutdownordaemon_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, reportscontext.DeadlineExceededand carries on.Refs AIGOV-557
Notes for reviewers
clockTimeoutreturns a smalltimeoutContextwrapper that matchescontext.WithTimeout:Deadline()reports when the clock timer fires, andErr()reportscontext.DeadlineExceededonce it has fired. Without it, a context canceled with a cause reportscontext.CanceledfromErr(). Every consumer of these shutdown contexts (http.Server.Shutdown,aibridged.Server.Shutdown,InflightGate.Shutdownand the pool) readsctx.Err()on the context it was given; contexts derived from the wrapper would still reportcontext.Canceled, which the doc comment notes.deferwhenservereturns, 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.standaloneGatewayParamsand defaults toquartz.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.clockTimeoutis similar totimeoutinagent/x/agentacp/manager.go. Sharing one helper could be a follow-up.Implementation notes
enterprise/cli/aigatewaystart.go:clock quartz.ClockonstandaloneGatewayParamsandstandaloneGateway;newStandaloneGatewayfalls back toquartz.NewReal().clockTimeout(ctx, clock, d, tags...):context.WithCancelCauseplusclock.AfterFunc, wrapped intimeoutContext, which also records the deadline fromclock.Now(). The returned cancel stops the timer.serveand the daemon timeout inshutdownDaemonuseclockTimeoutwiths.clock.enterprise/cli/aigatewaystart_internal_test.go:withClocktest option passes a mock clock tonewTestStandaloneGateway.TestClockTimeout_Deadline: the context'sDeadline()is the mock clock's time plus the timeout.TestStandaloneGatewayServe_ShutdownOrdertraps 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_HTTPStopsBeforeDaemonShutdowntraps 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:" withcontext.DeadlineExceeded.TestRunStandaloneGateway_DaemonShutdownTimeout: the pool shutdown never returns; after 5s, run returns "shutdown AI Gateway daemon:" withcontext.DeadlineExceededand 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.