test: bound WAL reservation witness and cleanup

This commit is contained in:
pingqiu committed 2026-12-02 07:48:36 -08:00
1 parent 4488a94dd5
commit 9f3afc3c71
1 file changed
+50 -4
@@ -2,6 +2,7 @@ package storage
import (
"context"
"sync"
"sync/atomic"
"testing"
"testing/synctest"
@@ -96,6 +97,14 @@ func TestWALAdmissionFastReservationKeepsAppendLock(t *testing.T) {
// A competing real append must not consume reserved room between admission and
// pwrite. Checking TryLock only at pwrite misses a release followed by reacquire.
func TestWALAdmissionFastReservationExcludesCompetingAppend(t *testing.T) {
// Even store cleanup can block after a lock regression. The parent kills
// and joins this child at its own bound rather than hanging the package.
if !inBoundedChild(t) {
if err := runBoundedChild(t.Name(), 20*time.Second); err != nil {
t.Fatal(err)
}
return
}
for _, batch := range []bool{false, true} {
t.Run(map[bool]string{false: "write", true: "batch"}[batch], func(t *testing.T) {
s := admissionPressureV3(t, 0)
@@ -115,6 +124,27 @@ func TestWALAdmissionFastReservationExcludesCompetingAppend(t *testing.T) {
err error
}
first, second := make(chan result, 1), make(chan result, 1)
var releaseOnce sync.Once
unblock := func() { releaseOnce.Do(func() { close(release) }) }
join := func(name string, done <-chan result) result {
select {
case got := <-done:
return got
case <-time.After(2 * time.Second):
t.Errorf("%s append did not finish within 2s after release", name)
return result{}
}
}
firstJoined, secondStarted, secondJoined := false, false, false
defer func() {
unblock()
if !firstJoined {
join("first", first)
}
if secondStarted && !secondJoined {
join("competing", second)
}
}()
go func() {
if batch {
lsns, err := s.WriteBatch(0, [][]byte{blk(1), blk(2)})
@@ -128,10 +158,25 @@ func TestWALAdmissionFastReservationExcludesCompetingAppend(t *testing.T) {
first <- result{uint64(lsn), err}
}
}()
<-reserved
select {
case <-reserved:
case got := <-first:
firstJoined = true
t.Errorf("first append returned before reservation hook: %+v", got)
return
case <-time.After(2 * time.Second):
t.Error("first append did not enter reservation hook within 2s")
return
}
competingStarted := make(chan struct{})
secondStarted = true
go func() { close(competingStarted); lsn, err := s.Write(8, blk(8)); second <- result{uint64(lsn), err} }()
<-competingStarted
select {
case <-competingStarted:
case <-time.After(2 * time.Second):
t.Error("competing append did not start within 2s")
return
}
// A real mutex wait is not a durably blocked synctest operation. Keep the
// competing append scheduled against the paused reservation for a bounded
// real interval; the detached-reservation mutant signals admitted instead.
@@ -141,8 +186,9 @@ func TestWALAdmissionFastReservationExcludesCompetingAppend(t *testing.T) {
overtook = true
case <-time.After(100 * time.Millisecond):
}
close(release)
a, b := <-first, <-second
unblock()
a, b := join("first", first), join("competing", second)
firstJoined, secondJoined = true, true
if overtook || a.err != nil || b.err != nil || a.lsn >= b.lsn {
t.Fatalf("reservation continuity: overtook=%v first=%+v competitor=%+v", overtook, a, b)
}