oficina / docs Public

Live push slice 1a: the race step's concurrency test takes the warm path (#174 delta 2)

Under -race -short the old TestWarmHandlesUnderConcurrentPOSTs made 0
warm hits and 0 re-seeds in 320 POSTs: churn every millisecond kept every
handle cold. It is now pre-filled to just under the snapshot cadence and
runs three phases of 8 goroutines: no churn (at least half the POSTs must
be warm hits, and a re-seed must run), drops and sweeps every 20 ms, and a
one-handle cap with two documents (concurrent cap eviction). Under -race
-short: 530 warm hits, 2 re-seeds, race-clean. With handles never kept it
fails: 0 warm hits in 200 POSTs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 42f0b918f6cdbf02c4497a2e99066515dcc5c309 parent ca487ae
1 file changed, +38 −36
  • internal/docs/warm_test.go +38 −36
diff --git a/internal/docs/warm_test.go b/internal/docs/warm_test.go
index 47636ec..fcf0ba0 100644
--- a/internal/docs/warm_test.go
+++ b/internal/docs/warm_test.go
@@ -491,16 +491,17 @@ func TestWarmHandleContentionDoesNotReplay(t *testing.T) {
}
}
-// Rule 1 and the cache's locking, under -race in the gate: 8 goroutines ×
-// 150 POSTs to two documents, with drops and sweeps (on a clock that moves
-// past warmIdle) between them. Every POST succeeds, and at the end the log,
-// a cold replay and any warm handle agree. (The review ran 8 × 400; each
-// POST here is a new Yjs client, and with drops forcing replays that many
-// clients made the replays themselves time out on a loaded machine.)
+// Rule 1 and the cache's locking, run under -race in the gate (review #174,
+// delta finding 2: the earlier version never reached the warm path under
+// -short). Pre-filled so a re-seed runs under the detector; then three
+// phases, each by 8 goroutines: no churn (warm hits must dominate), drops
+// and sweeps on a clock past warmIdle, and a one-handle cap with two
+// documents (concurrent cap eviction). At the end the log, a cold replay
+// and any warm handle agree.
func TestWarmHandlesUnderConcurrentPOSTs(t *testing.T) {
- per := 150
+ per := 60
if testing.Short() {
- per = 40
+ per = 25
}
f := newWarmFixture(t)
second, err := f.s.Create(f.ctx, f.owner, "Second")
@@ -508,6 +509,23 @@ func TestWarmHandlesUnderConcurrentPOSTs(t *testing.T) {
t.Fatal(err)
}
docs := []*Document{f.doc, second}
+ var next atomic.Int64
+ for k := 0; k < snapshotCadence-40; k++ {
+ f.mustPost(f.word(int(next.Add(1)) + 900000))
+ }
+ c := &f.s.warm.counts
+
+ // No churn: the warm path, and a re-seed, under the detector.
+ hits0, reseeds0 := c.hits.Load(), c.reseeds.Load()
+ f.concurrentPOSTs(docs[:1], 8, per, &next)
+ if hits := c.hits.Load() - hits0; hits < int64(8*per/2) {
+ t.Fatalf("no-churn phase: %d warm hits in %d POSTs", hits, 8*per)
+ }
+ if c.reseeds.Load() == reseeds0 {
+ t.Fatal("no-churn phase: no re-seed ran")
+ }
+
+ // Churn: drops and sweeps every 20 ms on a clock that moves past warmIdle.
var clockMu sync.Mutex
clock := time.Now()
f.s.warm.mu.Lock()
@@ -522,7 +540,7 @@ func TestWarmHandlesUnderConcurrentPOSTs(t *testing.T) {
select {
case <-stop:
return
- default:
+ case <-time.After(20 * time.Millisecond):
}
switch i % 3 {
case 0:
@@ -535,41 +553,24 @@ func TestWarmHandlesUnderConcurrentPOSTs(t *testing.T) {
default:
f.s.warm.stats()
}
- time.Sleep(5 * time.Millisecond)
}
}()
- var wg sync.WaitGroup
- errs := make(chan error, 8)
- for g := 0; g < 8; g++ {
- wg.Add(1)
- go func(g int) {
- defer wg.Done()
- for i := 0; i < per; i++ {
- d := docs[(g+i)%2]
- u, err := f.app.core.InitialUpdate(f.ctx, d.GUID, uint64(300000+g*10000+i), []core.InitialBlock{{Text: fmt.Sprintf("g%di%d", g, i)}})
- if err == nil {
- _, err = f.s.ApplyUpdate(f.ctx, d, f.actor, u)
- }
- if err != nil {
- errs <- fmt.Errorf("goroutine %d POST %d: %w", g, i, err)
- return
- }
- }
- }(g)
- }
- wg.Wait()
+ f.concurrentPOSTs(docs, 8, per, &next)
close(stop)
churn.Wait()
- close(errs)
- for err := range errs {
- t.Fatal(err)
+
+ // A one-handle cap with two documents: eviction under concurrency.
+ f.s.warm.mu.Lock()
+ f.s.warm.maxHandles = 1
+ f.s.warm.mu.Unlock()
+ f.concurrentPOSTs(docs, 8, per, &next)
+ if n, _ := f.s.warm.stats(); n > 1 {
+ t.Fatalf("%d handles kept under a cap of 1", n)
}
+
for _, d := range docs {
g := &warmFixture{t: t, app: f.app, s: f.s, owner: f.owner, actor: f.actor, doc: d, ctx: f.ctx}
truth := sortedWords(g.rowsText(-1))
- if got := len(strings.Fields(truth)); got != 8*per/2 {
- t.Fatalf("document %d holds %d words, want %d", d.ID, got, 8*per/2)
- }
if sortedWords(g.coldText()) != truth {
t.Fatalf("document %d: a cold replay differs from the log", d.ID)
}
@@ -577,6 +578,7 @@ func TestWarmHandlesUnderConcurrentPOSTs(t *testing.T) {
t.Fatalf("document %d: the warm handle differs from the log", d.ID)
}
}
+ t.Logf("replays %d, hits %d, stale %d, re-seeds %d", c.replays.Load(), c.hits.Load(), c.stale.Load(), c.reseeds.Load())
}
// Team mode with sealed rows, as production stores them (review finding 4):