| new file mode 100644 |
| index 000000000..b9a76bd57 |
| --- /dev/null |
| +++ b/internal/lease/renew_attempt_test.go |
| @@ -0,0 +1,269 @@ |
| +package lease |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + s3 "github.com/aws/aws-sdk-go-v2/service/s3" |
| + lss3 "github.com/benbjohnson/litestream/s3" |
| +) |
| + |
| +func TestRenewAttemptDeadline(t *testing.T) { |
| + started := time.Now() |
| + for _, tc := range []struct { |
| + name string |
| + ttl, safe, want time.Duration |
| + }{ |
| + {"production", 15 * time.Minute, 10 * time.Minute, 30 * time.Second}, |
| + {"short", 3 * time.Second, 2 * time.Second, time.Second}, |
| + {"safe_until", 15 * time.Minute, 5 * time.Second, 5 * time.Second}, |
| + {"equal_bounds", 15 * time.Minute, 30 * time.Second, 30 * time.Second}, |
| + {"exact_boundary", 15 * time.Minute, 0, 0}, |
| + {"spent", 15 * time.Minute, -time.Nanosecond, -time.Nanosecond}, |
| + {"tiny", 2 * time.Nanosecond, time.Second, time.Nanosecond}, |
| + {"zero", 0, time.Second, time.Nanosecond}, |
| + {"negative", -time.Second, time.Second, time.Nanosecond}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + got := renewAttemptDeadline(started, started.Add(tc.safe), tc.ttl) |
| + if want := started.Add(tc.want); !got.Equal(want) { |
| + t.Fatalf("deadline = %s from start, want %s", got.Sub(started), tc.want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func attemptRenewer(t *testing.T, backend *lockStore, ttl time.Duration, beforeWrite func()) *renewer { |
| + t.Helper() |
| + leaser := lss3.NewLeaser() |
| + leaser.SetClient(backend) |
| + leaser.TTL = ttl |
| + started := time.Now() |
| + lock, err := leaser.AcquireLease(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + rn := startRenewerPause(backend, leaser, lock, started, marginForTTL(ttl), 0, beforeWrite) |
| + t.Cleanup(func() { |
| + if err := leaser.ReleaseLease(context.Background(), rn.stop()); err != nil { |
| + t.Error(err) |
| + } |
| + }) |
| + return rn |
| +} |
| + |
| +func TestRenewAttemptStall(t *testing.T) { |
| + const ttl = 1500 * time.Millisecond |
| + var calls atomic.Int32 |
| + requests := make(chan context.Context, 1) |
| + backend := &lockStore{beforePut: func(ctx context.Context, p *s3.PutObjectInput) error { |
| + if p.IfMatch == nil { |
| + return nil |
| + } |
| + calls.Add(1) |
| + select { |
| + case requests <- ctx: |
| + default: |
| + } |
| + <-ctx.Done() |
| + return ctx.Err() |
| + }} |
| + rn := attemptRenewer(t, backend, ttl, nil) |
| + until, err := rn.SafeUntil() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + lost := rn.lost |
| + await(t, lost) |
| + await(t, rn.done) |
| + var ctx context.Context |
| + select { |
| + case ctx = <-requests: |
| + default: |
| + t.Fatal("lost ownership before sending a renewal") |
| + } |
| + deadline, ok := ctx.Deadline() |
| + if !ok || !deadline.Before(until) { |
| + t.Errorf("attempt deadline %v (present %t) must precede SafeUntil %v", deadline, ok, until) |
| + } |
| + if !errors.Is(rn.Err(), context.DeadlineExceeded) { |
| + t.Errorf("stalled attempt health = %v, want deadline exceeded", rn.Err()) |
| + } |
| + if !time.Now().Before(until) { |
| + t.Error("attempt was only stopped at SafeUntil") |
| + } |
| + for i := 0; i < 3; i++ { |
| + if _, err := rn.SafeUntil(); err == nil { |
| + t.Fatal("lost lease still has a safe deadline") |
| + } |
| + if rn.lost != lost { |
| + t.Fatal("Lost channel replaced") |
| + } |
| + await(t, lost) |
| + } |
| + time.Sleep(ttl/3 + 20*time.Millisecond) |
| + if got := calls.Load(); got != 1 { |
| + t.Errorf("sent %d renewals after a stalled attempt", got) |
| + } |
| + if puts, _ := backend.counts(); puts != 1 { |
| + t.Errorf("stalled attempt changed the lock: %d PUTs", puts) |
| + } |
| +} |
| + |
| +func TestRenewAttemptLateSuccessCannotRestoreHealth(t *testing.T) { |
| + const ttl = 1500 * time.Millisecond |
| + var calls atomic.Int32 |
| + backend := &lockStore{beforePut: func(ctx context.Context, p *s3.PutObjectInput) error { |
| + if p.IfMatch != nil { |
| + calls.Add(1) |
| + <-ctx.Done() |
| + } |
| + return nil |
| + }} |
| + rn := attemptRenewer(t, backend, ttl, nil) |
| + rn.mu.Lock() |
| + oldETag, oldUntil := rn.lease.ETag, rn.until |
| + rn.mu.Unlock() |
| + await(t, rn.done) |
| + await(t, rn.lost) |
| + if !errors.Is(rn.Err(), context.DeadlineExceeded) { |
| + t.Errorf("late success health = %v, want attempt deadline exceeded", rn.Err()) |
| + } |
| + if _, err := rn.SafeUntil(); err == nil { |
| + t.Error("late success restored SafeUntil") |
| + } |
| + rn.mu.Lock() |
| + newest, until := rn.lease.ETag, rn.until |
| + rn.mu.Unlock() |
| + if newest == oldETag { |
| + t.Error("late success discarded cleanup ETag") |
| + } |
| + backend.mu.Lock() |
| + stored := backend.etag |
| + backend.mu.Unlock() |
| + if newest != stored { |
| + t.Errorf("cleanup ETag = %q, stored %q", newest, stored) |
| + } |
| + if !until.Equal(oldUntil) { |
| + t.Errorf("late success extended SafeUntil from %v to %v", oldUntil, until) |
| + } |
| + if calls.Load() != 1 { |
| + t.Errorf("late success sent %d renewals", calls.Load()) |
| + } |
| + // Cleanup uses the retained ETag; a stale ETag makes ReleaseLease fail. |
| +} |
| + |
| +func TestRenewAttemptDeadlineSpentBeforeWrite(t *testing.T) { |
| + var calls atomic.Int32 |
| + backend := &lockStore{beforePut: func(ctx context.Context, p *s3.PutObjectInput) error { |
| + if p.IfMatch != nil { |
| + calls.Add(1) |
| + } |
| + return nil |
| + }} |
| + rn := attemptRenewer(t, backend, 900*time.Millisecond, func() { time.Sleep(350 * time.Millisecond) }) |
| + await(t, rn.done) |
| + await(t, rn.lost) |
| + if calls.Load() != 0 { |
| + t.Errorf("sent %d writes after the attempt budget was spent", calls.Load()) |
| + } |
| + if !errors.Is(rn.Err(), context.DeadlineExceeded) { |
| + t.Errorf("spent attempt health = %v", rn.Err()) |
| + } |
| +} |
| + |
| +func TestRenewAttemptRechecksBeforeWrite(t *testing.T) { |
| + for _, state := range []string{"lost", "stopped", "canceled"} { |
| + t.Run(state, func(t *testing.T) { |
| + var calls atomic.Int32 |
| + backend := &lockStore{beforePut: func(ctx context.Context, p *s3.PutObjectInput) error { |
| + if p.IfMatch != nil { |
| + calls.Add(1) |
| + } |
| + return nil |
| + }} |
| + current := make(chan *renewer, 1) |
| + rn := attemptRenewer(t, backend, 900*time.Millisecond, func() { |
| + r := <-current |
| + r.mu.Lock() |
| + switch state { |
| + case "lost": |
| + r.loseLocked(ErrPaused) |
| + case "stopped": |
| + r.stopped = true |
| + case "canceled": |
| + r.cancel() |
| + } |
| + r.mu.Unlock() |
| + }) |
| + current <- rn |
| + await(t, rn.done) |
| + if calls.Load() != 0 { |
| + t.Errorf("sent %d writes after %s before-write seam", calls.Load(), state) |
| + } |
| + if rn.Err() == nil { |
| + t.Fatal("invalidated handle remained healthy") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestRenewAttemptCancelsSuccessfulChild(t *testing.T) { |
| + requests := make(chan context.Context, 1) |
| + backend := &lockStore{beforePut: func(ctx context.Context, p *s3.PutObjectInput) error { |
| + if p.IfMatch != nil { |
| + select { |
| + case requests <- ctx: |
| + default: |
| + } |
| + } |
| + return nil |
| + }} |
| + rn := attemptRenewer(t, backend, 1500*time.Millisecond, nil) |
| + select { |
| + case ctx := <-requests: |
| + await(t, ctx.Done()) |
| + if !errors.Is(ctx.Err(), context.Canceled) { |
| + t.Fatalf("successful child not canceled promptly: %v", ctx.Err()) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("no renewal sent") |
| + } |
| + if err := rn.Err(); err != nil { |
| + t.Fatalf("healthy success lost ownership: %v", err) |
| + } |
| +} |
| + |
| +func TestRenewAttemptUsesEarlierSafeUntil(t *testing.T) { |
| + requests := make(chan context.Context, 1) |
| + backend := &lockStore{beforePut: func(ctx context.Context, p *s3.PutObjectInput) error { |
| + if p.IfMatch == nil { |
| + return nil |
| + } |
| + requests <- ctx |
| + <-ctx.Done() |
| + return ctx.Err() |
| + }} |
| + rn := attemptRenewer(t, backend, 1500*time.Millisecond, nil) |
| + until := time.Now().Add(850 * time.Millisecond) |
| + rn.mu.Lock() |
| + rn.until = until |
| + rn.mu.Unlock() |
| + rn.changed <- struct{}{} |
| + select { |
| + case ctx := <-requests: |
| + if deadline, ok := ctx.Deadline(); !ok || !deadline.Equal(until) { |
| + t.Errorf("attempt deadline %v (present %t), want SafeUntil %v", deadline, ok, until) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("no renewal sent") |
| + } |
| + await(t, rn.done) |
| + await(t, rn.lost) |
| + if _, err := rn.SafeUntil(); err == nil { |
| + t.Error("expired handle retained SafeUntil") |
| + } |
| +} |