test(volume): the lease-deadline control sees the ACK dispatched and its deadline

The fake ACK records that it was entered with a live context and the
deadline it carried; the test requires DeadlineExceeded, that deadline at
the 1 s lease deadline (before the 5 s ACK bound and the caller's), no
held lease afterwards and no completed ACK. A pre-dispatch error and an
ACK bounded by the parent context alone each fail it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-26 05:55:50 -08:00
1 parent 043aee6490
commit eec08968ba
1 file changed
+44 -9
@@ -2,6 +2,8 @@ package volume
import (
"context"
"errors"
"sync"
"testing"
"time"
@@ -15,6 +17,17 @@ type slowAckClient struct {
delay time.Duration
acked chan struct{}
valid time.Duration // the revoking lease's validity (default 30 s)
seen *ackCall // what the ACK call was entered with (optional)
}
// ackCall records the ACK call as dispatched: entered with a live context,
// and the deadline that context carried.
type ackCall struct {
mu sync.Mutex
entered bool
live bool
deadline time.Time
hasDeadline bool
}
func (c slowAckClient) RenewWorkspaceLease(context.Context, *control.WorkspaceIdentity, ...grpc.CallOption) (*control.WorkspaceLeaseFact, error) {
@@ -26,6 +39,12 @@ func (c slowAckClient) RenewWorkspaceLease(context.Context, *control.WorkspaceId
}
func (c slowAckClient) AckWorkspaceRevoked(ctx context.Context, _ *control.WorkspaceRevocationAck, _ ...grpc.CallOption) (*control.WorkspaceEmpty, error) {
if c.seen != nil {
c.seen.mu.Lock()
c.seen.entered, c.seen.live = true, ctx.Err() == nil
c.seen.deadline, c.seen.hasDeadline = ctx.Deadline()
c.seen.mu.Unlock()
}
select {
case <-time.After(c.delay):
close(c.acked)
@@ -60,29 +79,45 @@ func TestWorkspaceRevocationAckOutlastsTheMastersBoundedSteps(t *testing.T) {
}
// The lease deadline still bounds the ACK: with the revoking lease valid for
// 1 s and a master that answers after 3 s, the ACK call is cancelled at the
// lease deadline (about 1 s), not at WorkspaceAckTimeout (5 s), and the
// revocation is not reported as acknowledged.
// 1 s and a master that answers after 3 s, the ACK call is dispatched with a
// live context whose deadline is the lease deadline (earlier than the 5 s
// ACK bound and the caller's), it ends with DeadlineExceeded at about 1 s,
// and afterwards the guard holds no lease and no ACK completed.
func TestWorkspaceRevocationAckIsCutAtTheLeaseDeadline(t *testing.T) {
client := slowAckClient{delay: 3 * time.Second, acked: make(chan struct{}), valid: time.Second}
seen := &ackCall{}
client := slowAckClient{delay: 3 * time.Second, acked: make(chan struct{}), valid: time.Second, seen: seen}
g, err := NewWorkspaceLeaseGuard(workspaceTestView{}, client, &control.WorkspaceIdentity{VolumeId: "v", ReplicaId: "r", Epoch: 1}, func(context.Context) (uint64, error) { return 7, nil }, nil)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
parent := 20 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), parent)
defer cancel()
start := time.Now()
err = g.Run(ctx)
elapsed := time.Since(start)
if err == nil {
t.Fatal("an ACK the master answered after the lease ended was reported as acknowledged")
seen.mu.Lock()
entered, live, deadline, hasDeadline := seen.entered, seen.live, seen.deadline, seen.hasDeadline
seen.mu.Unlock()
if !entered || !live {
t.Fatalf("the ACK was not dispatched with a live context (entered %v, live %v); err %v", entered, live, err)
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("err %v; want DeadlineExceeded at the lease deadline", err)
}
if !hasDeadline || !deadline.Before(start.Add(WorkspaceAckTimeout)) || !deadline.Before(start.Add(parent)) || deadline.After(start.Add(1500*time.Millisecond)) {
t.Fatalf("the ACK's deadline %v (set %v) is not the 1 s lease deadline: start %v, ACK bound %v, parent %v", deadline, hasDeadline, start, WorkspaceAckTimeout, parent)
}
if elapsed >= 2500*time.Millisecond {
t.Fatalf("the ACK ran %v: it was not cut at the 1 s lease deadline (err %v)", elapsed, err)
t.Fatalf("the ACK ran %v: it was not cut at the 1 s lease deadline", elapsed)
}
if g.Held() {
t.Fatal("the guard still holds the lease after its deadline")
}
select {
case <-client.acked:
t.Fatal("the master's late answer was awaited past the lease deadline")
t.Fatal("an ACK completed after the lease deadline")
default:
}
}