hostlimit: red tests, the hold's expiry is exact, lock-free and monotonic

Seam: testExpireRace, run between a hold's expiry decision and its
revocation.

- TestProgressRacingTheExpiryIsHonoured: a progress report landing while
  the expiry is decided must be honoured. Fails here: the decision is made,
  the lock dropped, and the hold revoked anyway.
- TestProgressDoesNotWaitForTheLimiterLock: Progress must not wait for the
  limiter's lock, which admission holds across file-system work. Fails
  here: it takes that lock.
- TestProgressStampIsMonotonicElapsed: the progress stamp must be an
  elapsed time on the limiter's monotonic base. Fails here: a wall-clock
  timestamp, so a clock step revokes or keeps holds wrongly.
- TestLaunchMismatchNamesTheFileAndTheCure: the launch refusal of a pool
  written with other flags must name configuration.json and the cure
  (stop every process, remove the file). Fails here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 09:57:02 -08:00
1 parent 4f7e211adc
commit a2978497cd
2 files changed
+104

No files matched your search

+4
View File
@@ -100,6 +100,7 @@ type lease struct {
type Limiter struct {
testBeforeConfigWrite func() // test-only crash boundary; immutable before first admission
testBeforeConfigLock func(*os.File) // test-only fault boundary; immutable before first admission
testExpireRace func() // tests: runs between a hold's expiry decision and its revocation
cfg Config
mu sync.Mutex
admitWG sync.WaitGroup
@@ -385,6 +386,9 @@ func (l *Limiter) expire(id uint64) {
return
}
l.mu.Unlock()
if h := l.testExpireRace; h != nil {
h() // tests: a progress report racing the expiry's decision
}
l.revoke(id)
}
@@ -0,0 +1,100 @@
package hostlimit
import (
"context"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// A progress report that lands while a hold's expiry is being decided is
// honoured: the decision and the revocation are one step, re-checking the
// last progress, so a hold that progressed is never revoked for it.
func TestProgressRacingTheExpiryIsHonoured(t *testing.T) {
c := testConfig(t)
c.NoProgress = 50 * time.Millisecond
l := newTest(t, c)
revoked := make(chan struct{}, 1)
x, err := l.Admit(context.Background(), 1, func(error) { revoked <- struct{}{} })
if err != nil {
t.Fatal(err)
}
defer l.Release(x)
raced := make(chan struct{})
var once sync.Once
l.testExpireRace = func() { once.Do(func() { l.Progress(x); close(raced) }) }
select {
case <-raced:
case <-time.After(2 * time.Second):
t.Fatal("the expiry never ran")
}
select {
case <-revoked:
t.Fatal("a hold whose progress landed during its expiry was revoked")
case <-time.After(20 * time.Millisecond):
}
}
// Progress never waits for the limiter's lock, which admission holds across
// file-system work: a cut's progress tick cannot stall behind an admission.
func TestProgressDoesNotWaitForTheLimiterLock(t *testing.T) {
l := newTest(t, testConfig(t))
x, err := l.Admit(context.Background(), 1, nil)
if err != nil {
t.Fatal(err)
}
defer l.Release(x)
l.mu.Lock()
done := make(chan struct{})
go func() { l.Progress(x); close(done) }()
select {
case <-done:
l.mu.Unlock()
case <-time.After(time.Second):
l.mu.Unlock()
<-done
t.Fatal("Progress waited for the limiter's lock")
}
}
// The window is measured on the monotonic clock: a hold's progress stamp is
// an elapsed time since the limiter's own base, never a wall-clock timestamp
// (a wall-clock step, an NTP correction or a VM resume, would otherwise revoke
// every progressing hold, or keep a stalled one).
func TestProgressStampIsMonotonicElapsed(t *testing.T) {
l := newTest(t, testConfig(t))
x, err := l.Admit(context.Background(), 1, nil)
if err != nil {
t.Fatal(err)
}
defer l.Release(x)
l.Progress(x)
l.mu.Lock()
stamp := l.live[x].lastProgress.Load()
l.mu.Unlock()
if d := time.Duration(stamp); d < 0 || d > time.Hour {
t.Fatalf("progress stamp %d is not an elapsed time on the limiter's monotonic base", stamp)
}
}
// The launch check's refusal of a pool written with other flags names the
// file and the cure: after an upgrade it is the old binary's file, and no
// process may be using the directory at all.
func TestLaunchMismatchNamesTheFileAndTheCure(t *testing.T) {
c := DefaultConfig(t.TempDir())
if err := os.WriteFile(filepath.Join(c.Directory, "configuration.json"), []byte(`{"Directory":"elsewhere"}`), 0o600); err != nil {
t.Fatal(err)
}
err := checkLaunch(c, nil, func(string) (string, bool) { return "", false })
if err == nil {
t.Fatal("a pool with another configuration was accepted")
}
for _, want := range []string{"configuration.json", "stop every process", "remove"} {
if !strings.Contains(err.Error(), want) {
t.Fatalf("launch refusal %q does not name %q", err, want)
}
}
}