console-economics-speed: the oversized-usage test measures the stream
Astra round 2 (READY AFTER FIXES), the test-only P2: the bucket under the oversized-usage test is instrumented. Its Get fails the test, and each stream it hands out counts the bytes it gave and its Close. Each object over the cap gives at most the cap and one byte of its stream, and every stream opened is closed, for a streaming open of the page store, the concurrent page read and the pass's sequential read. Checked: removing usageDayStore.Open, or reading the object whole through Get, fails it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+118
−0
internal/console/economicsspeed_test.go+118 −0
CI passed — run details
diff --git a/internal/console/economicsspeed_test.go b/internal/console/economicsspeed_test.go| index b4f2ee1b9..1a425457e 100644 |
| --- a/internal/console/economicsspeed_test.go |
| +++ b/internal/console/economicsspeed_test.go |
| @@ -5,16 +5,19 @@ import ( |
| "encoding/json" |
| "errors" |
| "fmt" |
| + "io" |
| "maps" |
| "net/http" |
| "reflect" |
| "strings" |
| + "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| "github.com/carlosframework/platform/internal/economics" |
| "github.com/carlosframework/platform/internal/metrics" |
| + "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/usage" |
| ) |
| @@ -432,10 +435,59 @@ func TestEconomicsOversizedUsageHoldsTheWindow(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| + // The console's bucket, instrumented: every read must stream (its Get |
| + // fails the test), and each stream counts what it gave and its Close. |
| + bucket := fx.srv.cfg.Releases |
| + instrument := func(label string) *streamCountStore { |
| + st := &streamCountStore{Store: bucket, t: t, label: label, consumed: map[string]int64{}} |
| + fx.srv.cfg.Releases = st |
| + return st |
| + } |
| + t.Cleanup(func() { fx.srv.cfg.Releases = bucket }) |
| + checkStreams := func(st *streamCountStore, keys []string) { |
| + t.Helper() |
| + for _, k := range keys { |
| + if n := st.read(k); n > usageDayReadCap+1 || n == 0 { |
| + t.Errorf("%s: over-cap %s gave %d bytes of its stream, want at most the cap and one byte (%d)", st.label, k, n, usageDayReadCap+1) |
| + } |
| + } |
| + if opened, closed := st.counts(); opened == 0 || closed != opened { |
| + t.Errorf("%s: %d streams opened, %d closed", st.label, opened, closed) |
| + } |
| + } |
| + |
| + // The page's store streams a usage day itself (release.Opener), and |
| + // stops one byte past the cap. |
| + direct := instrument("a streaming open") |
| + rc, _, err := release.OpenObjectStreaming(ctx, fx.srv.usageDayReader(now), floods[0]) |
| + if err != nil { |
| + t.Fatalf("the page store does not stream a usage day: %v", err) |
| + } |
| + if _, err := io.Copy(io.Discard, rc); !errors.Is(err, errUsageDayTooLarge) { |
| + t.Errorf("streaming over-cap %s: %v, want the cap's error", floods[0], err) |
| + } |
| + rc.Close() |
| + checkStreams(direct, floods[:1]) |
| + econAllCold(fx) |
| + |
| + // The concurrent page read. |
| + page := instrument("the page read") |
| got, held := fx.srv.readEconomicsUsage(ctx, from, to, attrib, now) |
| if got.err != nil { |
| t.Fatal(got.err) |
| } |
| + checkStreams(page, floods) |
| + |
| + // The economics pass's sequential read, as statementsPass makes it. |
| + pass := instrument("the pass read") |
| + fill := fx.srv.usageDayFiller(now) |
| + if sweep, err := fx.srv.sweepUsage(ctx, fill, from, to, attrib); err != nil { |
| + t.Fatal(err) |
| + } else if _, ok := sweep.PerBox["flood"]; ok { |
| + t.Errorf("the pass counted an over-cap rollup") |
| + } |
| + checkStreams(pass, floods) |
| + fx.srv.cfg.Releases = bucket |
| if bound := int64(readConcurrency) * usageDayReadCap; held.Peak > bound { |
| t.Errorf("the sweep held %d bytes at once, over %d (%d bodies at the cap)", held.Peak, bound, readConcurrency) |
| } |
| @@ -466,6 +518,72 @@ func TestEconomicsOversizedUsageHoldsTheWindow(t *testing.T) { |
| } |
| } |
| +// streamCountStore is a bucket every read of which must stream: its Get |
| +// fails the test, and each reader Open hands out counts the bytes it gave |
| +// and whether it was closed. |
| +type streamCountStore struct { |
| + release.Store |
| + t *testing.T |
| + label string |
| + |
| + mu sync.Mutex |
| + consumed map[string]int64 |
| + opened, closed int |
| +} |
| + |
| +func (s *streamCountStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + s.t.Errorf("%s: Get(%s): a usage read buffered the object rather than streaming it", s.label, key) |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +func (s *streamCountStore) Open(ctx context.Context, key string) (io.ReadCloser, string, error) { |
| + rc, etag, err := release.OpenObjectStreaming(ctx, s.Store, key) |
| + if err != nil { |
| + return nil, "", err |
| + } |
| + s.mu.Lock() |
| + s.opened++ |
| + s.mu.Unlock() |
| + return &countedStream{rc: rc, s: s, key: key}, etag, nil |
| +} |
| + |
| +func (s *streamCountStore) read(key string) int64 { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + return s.consumed[key] |
| +} |
| + |
| +func (s *streamCountStore) counts() (opened, closed int) { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + return s.opened, s.closed |
| +} |
| + |
| +type countedStream struct { |
| + rc io.ReadCloser |
| + s *streamCountStore |
| + key string |
| + closed bool |
| +} |
| + |
| +func (c *countedStream) Read(p []byte) (int, error) { |
| + n, err := c.rc.Read(p) |
| + c.s.mu.Lock() |
| + c.s.consumed[c.key] += int64(n) |
| + c.s.mu.Unlock() |
| + return n, err |
| +} |
| + |
| +func (c *countedStream) Close() error { |
| + c.s.mu.Lock() |
| + if !c.closed { |
| + c.closed = true |
| + c.s.closed++ |
| + } |
| + c.s.mu.Unlock() |
| + return c.rc.Close() |
| +} |
| + |
| // TestEconomicsSweepPastItsDeadlineIsNotPublished: the scenario, its |
| // listing and the bills answer at once while one usage day is held past |
| // the sweep's deadline. The view fails with the deadline, as the reads one |