test(archied): stop racing state-store startup against a wall clock

TestRunStateStoreAcceptsEnvironmentToken wrapped the whole of RunStateStore --
SQLite schema init and the serve loop -- in a 200ms context deadline, so
whether it passed depended on how fast the disk was. Measured before the fix:
6 failures in 12 consecutive runs. A gate that fails half the time teaches
people to ignore it, and the failure it reported ('store: init schema:
context deadline exceeded') named a time budget, not a defect.

Cancellation is the assertion, so the test now cancels and then asserts:
the call must not return before cancellation (that would mean startup refused
the config, the thing this test exists to catch) and must return within a
generous grace once cancelled. The graces bound 'did startup reject us', not
'machine speed'. Measured after: 12 passes in 12 runs.

Also applies the canonical gofumpt and 'go fix' output to
internal/infrastructure/staterpc/dial.go, cmd/archie-ui/main_test.go and
internal/releaseupdate/check_script_test.go. task check runs gofumpt -w over
the tree, so leaving these unformatted made the gate rewrite unrelated files
mid-run and report dirty state for changes it had made itself.
This commit is contained in:
sam committed 2026-09-14 08:09:14 +00:00
1 parent 4b250ba929
commit c2ebcb8298
4 files changed
+40 -7

No files matched your search

+2 -1
View File
@@ -235,7 +235,8 @@ func startUIProcess(t *testing.T, bin, cfg string) *uiProcess {
// ignored SIGTERM.
ctx, cancel := context.WithCancel(context.WithoutCancel(t.Context()))
t.Cleanup(cancel)
cmd := exec.CommandContext(ctx, bin,
cmd := exec.CommandContext(
ctx, bin,
"-config", cfg,
"-listen", "127.0.0.1:0",
"-token", dashboardToken,
@@ -2,6 +2,7 @@ package archied
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
@@ -15,6 +16,15 @@ import (
"github.com/samcharles93/archie-core/internal/config"
)
// startupGrace is how long RunStateStore is given to either refuse the config
// or settle into serving before the test cancels it. It bounds "did startup
// reject us" (fast, deterministic) rather than "is the machine fast enough"
// (I/O-bound, which is what made the previous 200ms budget a coin flip).
const startupGrace = 2 * time.Second
// shutdownGrace bounds how long a cancelled RunStateStore may take to return.
const shutdownGrace = 5 * time.Second
func TestExternalServicesDoNotRequireBridgeNetwork(t *testing.T) {
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
@@ -50,9 +60,31 @@ func TestRunStateStoreAcceptsEnvironmentToken(t *testing.T) {
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond)
// Cancellation is the assertion: RunStateStore must start (the env token
// satisfies the non-loopback listener) and then return promptly when the
// context ends. It must NOT be raced against a wall-clock budget -- SQLite
// schema init is I/O, so a test that gives startup a few hundred
// milliseconds is a coin flip, not a check. Cancel first, then assert.
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
if err := RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "0.0.0.0:0"}); err != nil {
t.Fatalf("environment token should permit startup: %v", err)
errCh := make(chan error, 1)
go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "0.0.0.0:0"}) }()
select {
case err := <-errCh:
// Returning before cancellation means startup refused the config, which
// is the failure this test exists to catch.
t.Fatalf("RunStateStore returned before cancellation, want it to serve: %v", err)
case <-time.After(startupGrace):
}
cancel()
select {
case err := <-errCh:
if err != nil && !errors.Is(err, context.Canceled) {
t.Fatalf("RunStateStore returned %v, want nil or context.Canceled", err)
}
case <-time.After(shutdownGrace):
t.Fatal("RunStateStore did not return within the shutdown grace after cancellation")
}
}
+2 -1
View File
@@ -39,7 +39,8 @@ func Dial(target, token string, options ...grpc.DialOption) (*Client, func(), er
// Both call shapes need the credential: the server's interceptors
// guard unary RPCs and the streaming capture reads separately, so a
// unary-only interceptor leaves the streams unauthenticated.
opts = append(opts,
opts = append(
opts,
grpc.WithUnaryInterceptor(UnaryClientTokenInterceptor(token)),
grpc.WithStreamInterceptor(StreamClientTokenInterceptor(token)),
)
+1 -2
View File
@@ -63,8 +63,7 @@ func runUpdateCheckScript(t *testing.T, run checkRun) (Snapshot, []string, strin
)
output, err := cmd.Output()
var stderr string
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
stderr = string(exitErr.Stderr)
}
var calls []string