oficina / home Public

roster: a deterministic test that a refresh wakes its waiter unlocked

Fable's #104 suggestion: a test-only signalled seam, captured under c.mu and
called in the flight goroutine just after done is closed, lets a test park
the closer and ask Lease.Valid from the woken waiter.
TestRefreshWakesItsWaiterWithTheCacheUnlocked passes 20/20 on the fix and
fails every run with the close (and the park) moved back under the lock.
Also N1: TestLeaseSurvivesAnotherMembersChange asserts the first decision is
Member, so a recurrence names the FetchTimeout path instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 7db3832f6a8e6f37b569208e5c055a95047a7d16 parent 12b19ea
2 files changed, +32 −0
  • roster/cache.go +8 −0
  • roster/lease_test.go +24 −0

CI passed — run details

diff --git a/roster/cache.go b/roster/cache.go
index eae1773..2f52e9f 100644
--- a/roster/cache.go
+++ b/roster/cache.go
@@ -174,6 +174,10 @@ type Cache struct {
flight *flight
attempted bool
lastAttempt time.Duration
+ // signalled, when set (tests only), runs in the flight goroutine just
+ // after done is closed: a test parks the closer there to prove the lock
+ // is already free when a waiter wakes. Read under mu with the result.
+ signalled func()
}
func New(ctx context.Context, cfg Config) (*Cache, error) {
@@ -413,12 +417,16 @@ func (c *Cache) startRefresh(now time.Duration) *flight {
c.mu.Lock()
f.err = err
c.flight = nil
+ signalled := c.signalled
c.mu.Unlock()
// Signal only once the lock is free: a waiter that wakes on done and
// asks Lease.Valid at once must not meet this goroutine's lock, which
// TryLock reports as contention (Unknown). f.err is written before
// the close, so the receive still sees it.
close(f.done)
+ if signalled != nil {
+ signalled()
+ }
}()
return f
}
diff --git a/roster/lease_test.go b/roster/lease_test.go
index 57c9984..5c1e1e0 100644
--- a/roster/lease_test.go
+++ b/roster/lease_test.go
@@ -300,6 +300,7 @@ func TestLeaseSurvivesAnotherMembersChange(t *testing.T) {
})
c := f.cache()
d := f.check(c, 1)
+ state(t, d, Member)
f.clock.step(30 * time.Second)
f.refreshTo(c, 2, []MemberEntry{{person(1), "owner"}, {person(3), "owner"}})
if !d.Lease.Valid() {
@@ -307,6 +308,29 @@ func TestLeaseSurvivesAnotherMembersChange(t *testing.T) {
}
}
+// A waiter woken by a finished refresh finds the cache unlocked: the flight
+// releases c.mu before it closes done. The closer is parked just after the
+// close (the signalled seam), so a Lease.Valid asked by the woken waiter
+// meets the lock if, and only if, the close still happens under it, which
+// TryLock reads as contention and a valid lease as invalid (#104).
+func TestRefreshWakesItsWaiterWithTheCacheUnlocked(t *testing.T) {
+ f := setup(t)
+ c := f.cache()
+ d := f.check(c, 1)
+ state(t, d, Member)
+ parked, release := make(chan struct{}), make(chan struct{})
+ c.mu.Lock()
+ c.signalled = func() { close(parked); <-release }
+ c.mu.Unlock()
+ defer close(release)
+ f.clock.step(30 * time.Second)
+ f.refreshTo(c, 2, []MemberEntry{{person(1), "member"}})
+ <-parked
+ if !d.Lease.Valid() {
+ t.Fatal("the refresh woke its waiter with the cache still locked")
+ }
+}
+
// A fence ends every earlier lease even when a later publication lists the
// subject unchanged: the deadline was measured on a clock the fence distrusts.
func TestLeaseEndedByFenceStaysEndedAfterPublication(t *testing.T) {