console: pin that an over-budget page logs once, by pattern
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+54
−0
internal/console/perfwiring_test.go+54 −0
CI failed — run details
diff --git a/internal/console/perfwiring_test.go b/internal/console/perfwiring_test.go| index b28f139ab..b8704d40a 100644 |
| --- a/internal/console/perfwiring_test.go |
| +++ b/internal/console/perfwiring_test.go |
| @@ -1,16 +1,21 @@ |
| package console |
| import ( |
| + "bytes" |
| + "context" |
| "errors" |
| "io" |
| + "log" |
| "net/http" |
| "net/http/httptest" |
| "regexp" |
| "strings" |
| + "sync/atomic" |
| "testing" |
| "time" |
| "github.com/carlosframework/platform/internal/perf" |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| // The perf middleware is wired in NewServer, so these run through the |
| @@ -229,3 +234,52 @@ func TestRunningTabTimesTheAppHost(t *testing.T) { |
| t.Errorf("Server-Timing = %q, want a box span", st) |
| } |
| } |
| + |
| +// slowGetStore is a release store whose Get takes 160 ms once slow is set, |
| +// longer than the 150 ms read budget. The account-manifest read on every |
| +// app view is such a Get, so the page goes over budget. |
| +type slowGetStore struct { |
| + release.Store |
| + slow atomic.Bool |
| +} |
| + |
| +func (s *slowGetStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if s.slow.Load() { |
| + time.Sleep(160 * time.Millisecond) |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +// An over-budget page reaches the console's own log: the console logs |
| +// through the standard log package, and slog.Default writes through it, |
| +// so no logger is configured. The warning names the route pattern, never |
| +// the path, and a second slow request to the same route inside the |
| +// minute adds no second line. |
| +func TestOverBudgetPageIsLogged(t *testing.T) { |
| + rel := &slowGetStore{Store: release.NewDirStore(t.TempDir())} |
| + fx := newConsoleFixtureOn(t, true, rel) |
| + c := signInPaul(t, fx) |
| + |
| + var buf bytes.Buffer |
| + prev := log.Writer() |
| + log.SetOutput(&buf) |
| + t.Cleanup(func() { log.SetOutput(prev) }) |
| + rel.slow.Store(true) |
| + |
| + for i := 0; i < 2; i++ { |
| + if resp, _ := perfGet(t, c, fx.u("/apps/kass")); resp.StatusCode != http.StatusOK { |
| + t.Fatalf("GET app page = %d, want 200", resp.StatusCode) |
| + } |
| + } |
| + |
| + out := buf.String() |
| + if n := strings.Count(out, "request over performance budget"); n != 1 { |
| + t.Errorf("over-budget warnings = %d, want 1; log:\n%s", n, out) |
| + } |
| + if !strings.Contains(out, `screen="GET /a/{sqid}/apps/{app}"`) { |
| + t.Errorf("warning does not name the route pattern; log:\n%s", out) |
| + } |
| + if strings.Contains(out, "kass") { |
| + t.Errorf("log carries a path segment; log:\n%s", out) |
| + } |
| +} |