carlos / platform Public

test(edge): run the held-stream simulation on virtual time

Run all 300 lifecycle seeds inside synctest, including under -short. Wait
for runnable stream tasks before checking H1/H2/H3, remove scheduler
starvation exemptions, and tighten H2 from 185ms to 160ms. Join the driver
and stream tasks on failure as well as success.

Keep Go 1.26 race builds on one CPU for this test to avoid the upstream
synctest timer race-context crash (golang/go#76691). Other runs still
honor -cpu, and all seeds and assertions remain enabled under -race.

Validation: 150 runs across cpu=1,2,4 and 15 race runs passed. One original
300-seed run passed in 194.87s; the virtual-time runs took 0.27-1.29s.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev bc5e344d52b3e35bf22c267779c3dbb4aa625d42 parent 16f29ed
1 file changed, +36 −50
  • internal/edge/heldstream_sim_test.go +36 −50

Intent

Attributed to this commit: 83.7 g CO₂e (7,173,020 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on ci-timing-flakes-526, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/edge/heldstream_sim_test.go b/internal/edge/heldstream_sim_test.go
index a8530362c..628471b38 100644
--- a/internal/edge/heldstream_sim_test.go
+++ b/internal/edge/heldstream_sim_test.go
@@ -7,10 +7,12 @@ import (
"math/rand"
"net/http"
"net/http/httptest"
+ "runtime"
"strings"
"sync"
"sync/atomic"
"testing"
+ "testing/synctest"
"time"
"github.com/carlosframework/platform/internal/registry"
@@ -33,11 +35,8 @@ import (
// admissible, the stream has been running, and its last leg has been
// done for longer than Tick + ReopenSpacing + BackoffCap (spec G4:
// outside a bounded reopen delay, no silent stream past one tick).
-// An observation is conclusive only when the checker overslept by no
-// more than starveLimit anywhere in its window; others are skipped,
-// never excused by widening the bound. Each seed runs until it has at
-// least one conclusive H2 observation past the bound (up to maxRun)
-// and fails as inconclusive without one;
+// Virtual time advances only when all stream tasks are blocked. Each
+// seed must produce an H2 observation past the bound (up to maxRun);
// H3: a stream never has two legs open at once.
// At the end of each seed every admitted leg must have been Done.
@@ -246,24 +245,28 @@ func (w *simWriter) Write(b []byte) (int, error) { w.n.Add(int64(len(b))); retur
func (w *simWriter) Flush() {}
func TestHeldStreamLifecycleSim(t *testing.T) {
- if testing.Short() {
- t.Skip("simulation")
+ // Go 1.26 shares a race context between bubble timers: concurrent timer
+ // callbacks can crash TSan (golang/go#76691, fixed in Go 1.27). Keep all
+ // seeds and assertions under -race; ordinary builds still run on every CPU.
+ if raceEnabled && strings.HasPrefix(runtime.Version(), "go1.26.") {
+ defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
}
+ synctest.Test(t, heldStreamLifecycleSim)
+}
+
+func heldStreamLifecycleSim(t *testing.T) {
const (
- seeds = 300
- runFor = 600 * time.Millisecond
- maxRun = 10 * time.Second
- slack = 25 * time.Millisecond // fixed scheduler jitter allowance
- checkEv = 5 * time.Millisecond
- starveLimit = 20 * time.Millisecond
+ seeds = 300
+ runFor = 600 * time.Millisecond
+ maxRun = 10 * time.Second
+ checkEv = 5 * time.Millisecond
)
cfg := heldTestConfig()
cfg.MaxHold = time.Minute
cfg.LegHeader = time.Second
cfg.Commit = 20 * time.Millisecond // the timing the simulation was calibrated with
- bound := cfg.Tick + cfg.ReopenSpacing + cfg.BackoffCap + slack
- var totalAdmitted, totalClosedShort, conclusiveTotal, skipped int
- var maxLag time.Duration
+ bound := cfg.Tick + cfg.ReopenSpacing + cfg.BackoffCap
+ var totalAdmitted, totalClosedShort, conclusiveTotal int
for seed := int64(1); seed <= seeds; seed++ {
m := newSimModel(seed)
rng := rand.New(rand.NewSource(seed * 7919))
@@ -338,23 +341,23 @@ func TestHeldStreamLifecycleSim(t *testing.T) {
}
}()
- // Check the invariants. The checker measures its own oversleep; an H2
- // window it was starved in for more than starveLimit says nothing
- // about the stream task, so that observation is skipped. A fresh
- // window starts when the oversleep ends: the bound is never widened,
- // it is only measured over a window with no oversleep in it.
- var lastStarve time.Time
- starves := 0
+ var stopOnce sync.Once
+ stop := func() {
+ stopOnce.Do(func() {
+ close(stopDrive)
+ <-driveDone
+ cancel()
+ wg.Wait()
+ })
+ }
+ defer stop()
+
+ // Let every task runnable at this virtual instant finish before checking.
conclusive := 0
for time.Since(start) < runFor || (conclusive == 0 && time.Since(start) < maxRun) {
- before := time.Now()
time.Sleep(checkEv)
+ synctest.Wait()
now := time.Now()
- if lag := now.Sub(before) - checkEv; lag > starveLimit {
- lastStarve = now
- starves++
- maxLag = max(maxLag, lag)
- }
if m.served.Load() != 0 {
t.Fatalf("seed %d: H1: activator ServeHTTP called", seed)
}
@@ -373,10 +376,6 @@ func TestHeldStreamLifecycleSim(t *testing.T) {
quiet = le
}
if q := now.Sub(quiet); q > bound {
- if now.Sub(lastStarve) <= bound {
- skipped++ // no unstarved window past the bound yet
- continue
- }
conclusive++
if m.open[i] == 0 {
m.violate("H2: stream %d has no leg %v into an admissible window (bound %v)", i, q, bound)
@@ -392,29 +391,16 @@ func TestHeldStreamLifecycleSim(t *testing.T) {
v := append([]string(nil), m.violations...)
m.mu.Unlock()
if len(v) > 0 {
- close(stopDrive)
- cancel()
t.Fatalf("seed %d (n=%d): %v\nhistory:\n%s", seed, n, v, strings.Join(m.history, "\n"))
}
}
if conclusive == 0 {
- close(stopDrive)
- cancel()
- t.Fatalf("seed %d: inconclusive: no H2 observation past the bound in an unstarved window within %v (%d checker oversleeps over %v)", seed, maxRun, starves, starveLimit)
+ t.Fatalf("seed %d: no H2 observation past the bound within %v", seed, maxRun)
}
conclusiveTotal += conclusive
- close(stopDrive)
- <-driveDone
- cancel()
- waited := make(chan struct{})
- go func() { wg.Wait(); close(waited) }()
- select {
- case <-waited:
- case <-time.After(5 * time.Second):
- t.Fatalf("seed %d: stream tasks did not end", seed)
- }
+ stop()
m.mu.Lock()
if len(m.legs) != 0 {
t.Fatalf("seed %d: %d admitted legs never Done", seed, len(m.legs))
@@ -426,8 +412,8 @@ func TestHeldStreamLifecycleSim(t *testing.T) {
}
totalClosedShort += int(closedShort.Load())
}
- t.Logf("%d seeds: %d legs admitted, %d streams closed by short legs; H2 observations past the bound: %d conclusive, %d skipped as starved (worst checker oversleep over %v: %v)",
- seeds, totalAdmitted, totalClosedShort, conclusiveTotal, skipped, starveLimit, maxLag)
+ t.Logf("%d seeds: %d legs admitted, %d streams closed by short legs; %d H2 observations past the bound",
+ seeds, totalAdmitted, totalClosedShort, conclusiveTotal)
if totalAdmitted == 0 {
t.Fatal("no leg was ever admitted")
}