tests: s3fake applies start-after to common prefixes; exact page counts and bounded held-request cleanup
ListObjectsV2 no longer returns a common prefix at or before start-after. The fixture test holds the cold view to the exact count now the edge fold is attributed, captures the expected over-budget warnings, and reads the disarmed host delay off a cancelled request rather than a clock. The held-request test releases its hold and cancels its client in cleanup and bounds its final receive.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+136
−29
internal/console/perflatency_test.go+78 −17internal/s3fake/s3fake.go+10 −5internal/s3fake/s3fake_test.go+48 −7
diff --git a/internal/console/perflatency_test.go b/internal/console/perflatency_test.go| index 819afd196..df32ea772 100644 |
| --- a/internal/console/perflatency_test.go |
| +++ b/internal/console/perflatency_test.go |
| @@ -1,9 +1,14 @@ |
| package console |
| import ( |
| + "bytes" |
| + "context" |
| + "errors" |
| "io" |
| + "log" |
| "net/http" |
| "strings" |
| + "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| @@ -114,32 +119,39 @@ func TestLatencyAppClientAnswersTheContract(t *testing.T) { |
| t.Errorf("Calls() = (%d, %d), want (0, 1)", s3, host) |
| } |
| + // Whether the delay is armed is read off a cancelled request, not off |
| + // a clock: an armed transport waits on the request's context and so |
| + // fails at once, a disarmed one answers. |
| + probe := func() (*http.Response, error) { |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + cancel() |
| + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://shop.example.com/elsewhere", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return l.AppClient().Transport.RoundTrip(req) |
| + } |
| + if _, err := probe(); !errors.Is(err, context.Canceled) { |
| + t.Errorf("an enabled host call on a cancelled request = %v, want it to wait on the context", err) |
| + } |
| l.Enable(false) |
| - start = time.Now() |
| - resp, err = l.AppClient().Get("http://shop.example.com/elsewhere") |
| + resp, err = probe() |
| if err != nil { |
| - t.Fatal(err) |
| + t.Fatalf("a disabled host call waited on the context: %v", err) |
| } |
| resp.Body.Close() |
| if resp.StatusCode != http.StatusNotFound { |
| t.Errorf("another path = %d, want 404", resp.StatusCode) |
| } |
| - if took := time.Since(start); took >= 40*time.Millisecond { |
| - t.Errorf("a disabled host call took %v, want no delay", took) |
| - } |
| - if _, host := l.Calls(); host != 2 { |
| - t.Errorf("host calls = %d after two requests, want 2", host) |
| + if _, host := l.Calls(); host != 3 { |
| + t.Errorf("host calls = %d after three requests, want 3", host) |
| } |
| } |
| // The fixture over the S3 fake serves a real screen, and the perf sample |
| -// for it counts exactly the S3 requests the fake answered for it. |
| -// |
| -// The first view refreshes the edge fold (server.go foldedEdgeIPs), whose |
| -// two listings run on the request's goroutine but under |
| -// context.Background(), so the recorder counts them as background, not on |
| -// the page. The cold view is held to "every request accounted for"; the |
| -// warm view, inside edgeFoldTTL, to the exact count. |
| +// for each view counts exactly the S3 requests the fake answered for it: |
| +// the cold view's edge-fold refresh included, the warm one's with the |
| +// delays on. |
| func TestFixtureOverTheS3FakeServesAppPage(t *testing.T) { |
| lat := &perfLatency{S3Delay: 25 * time.Millisecond, HostDelay: 40 * time.Millisecond} |
| fx := newConsoleFixtureWith(t, consoleFixtureOptions{WithKey: true, S3: lat, AppClient: lat.AppClient()}) |
| @@ -170,13 +182,21 @@ func TestFixtureOverTheS3FakeServesAppPage(t *testing.T) { |
| if fake <= 0 { |
| t.Fatalf("the app page made %d S3 requests, want some (is the server on the fake?)", fake) |
| } |
| - if sample+background != fake { |
| - t.Errorf("cold view: sample Calls[s3] %d + background %d, the fake answered %d", sample, background, fake) |
| + if sample != fake || background != 0 { |
| + t.Errorf("cold view: sample Calls[s3] = %d (background %d), the fake answered %d; want them equal", sample, background, fake) |
| } |
| if host != 0 { |
| t.Errorf("the app page made %d app-host calls, want none", host) |
| } |
| + // With the delays on the views go over budget, and the console says |
| + // so in its log: capture the lines rather than let them reach the |
| + // test's output, and hold them to the screens. |
| + logs := &syncBuffer{} |
| + prev := log.Writer() |
| + log.SetOutput(logs) |
| + t.Cleanup(func() { log.SetOutput(prev) }) |
| + |
| lat.Enable(true) |
| start := time.Now() |
| fake, sample, background, _ = view() |
| @@ -198,4 +218,45 @@ func TestFixtureOverTheS3FakeServesAppPage(t *testing.T) { |
| if _, h1 := lat.Calls(); h1-h0 != 1 { |
| t.Errorf("the instances tab made %d app-host calls, want 1", h1-h0) |
| } |
| + |
| + for _, want := range []string{`screen="GET /a/{sqid}/apps/{app}"`, `screen="GET /a/{sqid}/apps/{app}/instances"`} { |
| + if !logs.waitFor(want, 2*time.Second) { |
| + t.Errorf("no over-budget warning for %s; log:\n%s", want, logs.String()) |
| + } |
| + } |
| + if n := strings.Count(logs.String(), "request over performance budget"); n != 2 { |
| + t.Errorf("over-budget warnings = %d, want 2 (one per screen); log:\n%s", n, logs.String()) |
| + } |
| +} |
| + |
| +// syncBuffer is a log destination a test can read while the server writes. |
| +type syncBuffer struct { |
| + mu sync.Mutex |
| + b bytes.Buffer |
| +} |
| + |
| +func (b *syncBuffer) Write(p []byte) (int, error) { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + return b.b.Write(p) |
| +} |
| + |
| +func (b *syncBuffer) String() string { |
| + b.mu.Lock() |
| + defer b.mu.Unlock() |
| + return b.b.String() |
| +} |
| + |
| +// waitFor reports whether s reaches the buffer within d: the sample, and |
| +// its warning, are recorded as the handler returns, which can be just |
| +// after the client has read the whole body. |
| +func (b *syncBuffer) waitFor(s string, d time.Duration) bool { |
| + for deadline := time.Now().Add(d); ; time.Sleep(5 * time.Millisecond) { |
| + if strings.Contains(b.String(), s) { |
| + return true |
| + } |
| + if time.Now().After(deadline) { |
| + return false |
| + } |
| + } |
| } |
diff --git a/internal/s3fake/s3fake.go b/internal/s3fake/s3fake.go| index f045bf456..1a3c67beb 100644 |
| --- a/internal/s3fake/s3fake.go |
| +++ b/internal/s3fake/s3fake.go |
| @@ -55,8 +55,8 @@ type Server struct { |
| hookMu sync.RWMutex |
| hook func(r *http.Request) (status int, hold <-chan struct{}) |
| - // holding, when set (package tests only), is called as a request |
| - // starts waiting on its hold, after its operation ran. |
| + // holding, when set (package tests only, under hookMu), is called as |
| + // a request starts waiting on its hold, after its operation ran. |
| holding func(r *http.Request) |
| } |
| @@ -97,7 +97,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| } |
| } |
| s.hookMu.RLock() |
| - hook := s.hook |
| + hook, holding := s.hook, s.holding |
| s.hookMu.RUnlock() |
| var hold <-chan struct{} |
| if hook != nil { |
| @@ -114,8 +114,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| } |
| buf := &bufferedResponse{header: http.Header{}} |
| s.serve(buf, r) |
| - if s.holding != nil { |
| - s.holding(r) |
| + if holding != nil { |
| + holding(r) |
| } |
| select { |
| case <-hold: |
| @@ -480,6 +480,11 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request, bucket string) { |
| if delim != "" { |
| if i := strings.Index(ke.Key[len(prefix):], delim); i >= 0 { |
| cp := ke.Key[:len(prefix)+i+len(delim)] |
| + // S3 applies start-after to common prefixes too: a key |
| + // past it can still roll up into one that sorts before. |
| + if startAfter != "" && cp <= startAfter { |
| + continue |
| + } |
| if n := len(entries); n > 0 && entries[n-1].isPrefix && entries[n-1].name == cp { |
| continue |
| } |
diff --git a/internal/s3fake/s3fake_test.go b/internal/s3fake/s3fake_test.go| index 4b9b280d6..fa55ca7ac 100644 |
| --- a/internal/s3fake/s3fake_test.go |
| +++ b/internal/s3fake/s3fake_test.go |
| @@ -9,6 +9,7 @@ import ( |
| "os" |
| "path/filepath" |
| "slices" |
| + "sync" |
| "testing" |
| "time" |
| @@ -97,8 +98,10 @@ func TestS3FakePagesLikeS3(t *testing.T) { |
| if n := requestsFor(t, fake, func() (err error) { mods, err = s.ListMod(ctx, "big/"); return }); n != 2 { |
| t.Errorf("ListMod of 1,001 objects took %d requests, want 2", n) |
| } |
| - if len(mods) != 1001 || mods[0].Mod.IsZero() { |
| - t.Errorf("ListMod returned %d entries (first %+v), want 1,001 with stamps", len(mods), mods[0]) |
| + if len(mods) != 1001 { |
| + t.Errorf("ListMod returned %d entries, want 1,001", len(mods)) |
| + } else if mods[0].Mod.IsZero() { |
| + t.Errorf("ListMod's first entry %+v has no stamp", mods[0]) |
| } |
| var level []string |
| @@ -114,6 +117,29 @@ func TestS3FakePagesLikeS3(t *testing.T) { |
| if want := []string{"lvl/a/", "lvl/zz/"}; !slices.Equal(dirs, want) { |
| t.Errorf("ListDirs = %q, want %q", dirs, want) |
| } |
| + // start-after inside a common prefix: S3 returns no common prefix that |
| + // sorts at or before it, though a key under it (sa/a/y.json) is past it. |
| + seedFiles(t, root, "sa/a/x.json", "sa/a/y.json", "sa/b.json", "sa/zz/z.json") |
| + out, err := s.Client.ListObjectsV2(ctx, &awss3.ListObjectsV2Input{ |
| + Bucket: aws.String("b"), Prefix: aws.String("sa/"), Delimiter: aws.String("/"), |
| + StartAfter: aws.String("sa/a/x.json"), |
| + }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var cps, objs []string |
| + for _, cp := range out.CommonPrefixes { |
| + cps = append(cps, aws.ToString(cp.Prefix)) |
| + } |
| + for _, o := range out.Contents { |
| + objs = append(objs, aws.ToString(o.Key)) |
| + } |
| + if want := []string{"sa/zz/"}; !slices.Equal(cps, want) { |
| + t.Errorf("common prefixes after sa/a/x.json = %q, want %q", cps, want) |
| + } |
| + if want := []string{"sa/b.json"}; !slices.Equal(objs, want) { |
| + t.Errorf("contents after sa/a/x.json = %q, want %q", objs, want) |
| + } |
| if n := requestsFor(t, fake, func() (err error) { listed, err = s.List(ctx, "empty/"); return }); n != 1 || len(listed) != 0 { |
| t.Errorf("List of an empty prefix took %d requests and returned %q, want 1 and nothing", n, listed) |
| @@ -134,8 +160,10 @@ func TestS3FakePagesLikeS3(t *testing.T) { |
| if n := requestsFor(t, fake, func() (err error) { page, err = s.ListPage(ctx, "big/", "big/k0000", 0); return }); n != 1 { |
| t.Errorf("unbounded ListPage after the first key took %d requests, want 1 (1,000 keys left)", n) |
| } |
| - if len(page) != 1000 || page[0] != "big/k0001" || page[999] != "big/k1000" { |
| - t.Errorf("unbounded ListPage returned %d keys from %q", len(page), page[0]) |
| + if len(page) != 1000 { |
| + t.Errorf("unbounded ListPage returned %d keys, want 1,000", len(page)) |
| + } else if page[0] != "big/k0001" || page[999] != "big/k1000" { |
| + t.Errorf("unbounded ListPage ran %q to %q, want big/k0001 to big/k1000", page[0], page[999]) |
| } |
| } |
| @@ -207,14 +235,22 @@ func TestS3FakeDelayAndHook(t *testing.T) { |
| } |
| holding := make(chan struct{}) |
| + fake.hookMu.Lock() |
| fake.holding = func(*http.Request) { close(holding) } |
| + fake.hookMu.Unlock() |
| type got struct { |
| body []byte |
| err error |
| } |
| + // Whatever fails below, the held request is released and its client |
| + // cancelled before the test server closes, so cleanup never hangs. |
| + var releaseOnce sync.Once |
| + releaseHold := func() { releaseOnce.Do(func() { close(hold) }) } |
| + getCtx, cancelGet := context.WithCancel(ctx) |
| + t.Cleanup(func() { releaseHold(); cancelGet() }) |
| done := make(chan got, 1) |
| go func() { |
| - b, _, err := s.Get(ctx, "k/held") |
| + b, _, err := s.Get(getCtx, "k/held") |
| done <- got{b, err} |
| }() |
| select { |
| @@ -232,8 +268,13 @@ func TestS3FakeDelayAndHook(t *testing.T) { |
| t.Fatalf("the held Get answered before its hold closed: %q %v", g.body, g.err) |
| case <-time.After(50 * time.Millisecond): |
| } |
| - close(hold) |
| - g := <-done |
| + releaseHold() |
| + var g got |
| + select { |
| + case g = <-done: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the held Get did not answer after its hold closed") |
| + } |
| if g.err != nil || string(g.body) != "body of k/held" { |
| t.Errorf("held Get = %q, %v; want the body read before the hold", g.body, g.err) |
| } |