| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package control_test
- import (
- "math/rand"
- "testing"
- "time"
- "hth/internal/control"
- hthv1 "hth/internal/gen/hth/api/v1"
- )
- func TestLeaseStateMachineInvariants(t *testing.T) {
- rng := rand.New(rand.NewSource(42))
- now := time.Unix(1_700_000_000, 0)
- m := control.NewManager("d0", 50*time.Millisecond, time.Second, 16)
- type ref struct {
- id string
- epoch uint64
- exp time.Time
- maxE uint64
- }
- var r ref
- for i := 0; i < 400; i++ {
- now = now.Add(time.Duration(rng.Intn(80)) * time.Millisecond)
- switch rng.Intn(7) {
- case 0:
- l, err := m.Acquire(now, "h", 200*time.Millisecond)
- if r.id != "" && now.Before(r.exp) {
- if err == nil {
- t.Fatalf("step %d: acquired while active", i)
- }
- } else if err == nil {
- if l.Epoch <= r.maxE && r.maxE != 0 {
- t.Fatalf("step %d: epoch did not increase %d -> %d", i, r.maxE, l.Epoch)
- }
- r.id, r.epoch, r.exp = l.ID, l.Epoch, l.Expires
- r.maxE = l.Epoch
- }
- case 1:
- if r.id == "" {
- continue
- }
- _, err := m.Renew(now, r.id, r.epoch, 200*time.Millisecond)
- active := now.Before(r.exp)
- if active && err != nil {
- t.Fatalf("step %d: renew failed: %v", i, err)
- }
- if !active && err == nil {
- t.Fatalf("step %d: renewed expired lease", i)
- }
- if err == nil {
- r.exp = m.Current().Expires
- }
- case 2:
- if r.id == "" {
- continue
- }
- err := m.Release(now, r.id, r.epoch)
- if now.Before(r.exp) && err != nil {
- t.Fatalf("step %d: release: %v", i, err)
- }
- if err == nil {
- r.id = ""
- r.exp = time.Time{}
- }
- case 3:
- old := m.ExpireIfNeeded(now)
- if old != nil {
- r.id = ""
- r.exp = time.Time{}
- }
- case 4:
- prev := r.maxE
- m.InvalidateAndBump()
- r.id = ""
- r.exp = time.Time{}
- if m.Current() != nil {
- t.Fatal("lease after invalidate")
- }
- // epoch is unexported except via next acquire
- _ = prev
- case 5:
- batch := &hthv1.ActionBatch{CommandId: "c", LeaseId: r.id, LeaseEpoch: r.epoch}
- rr := m.CheckAction(now, batch, 1, 0)
- if r.id == "" || !now.Before(r.exp) {
- if rr == hthv1.RejectReason_REJECT_REASON_UNSPECIFIED {
- t.Fatalf("step %d: stale lease accepted", i)
- }
- }
- case 6:
- if r.id == "" {
- continue
- }
- batch := &hthv1.ActionBatch{CommandId: "old", LeaseId: r.id, LeaseEpoch: r.epoch + 99}
- rr := m.CheckAction(now, batch, 1, 0)
- if rr != hthv1.RejectReason_REJECT_REASON_LEASE_MISMATCH && rr != hthv1.RejectReason_REJECT_REASON_LEASE_EXPIRED {
- t.Fatalf("step %d: old epoch not rejected: %v", i, rr)
- }
- }
- if m.Current() != nil && m.Current().Active(now) {
- if r.id == "" {
- t.Fatalf("step %d: manager has lease, ref empty", i)
- }
- }
- cur := m.Current()
- n := 0
- if cur != nil && cur.Active(now) {
- n = 1
- }
- if n > 1 {
- t.Fatal("more than one lease")
- }
- }
- }
- func TestIdempotentCacheConflict(t *testing.T) {
- c := control.NewCache(4)
- a := &hthv1.ActionResult{CommandId: "x", Accepted: true}
- c.Store("x", []byte{1}, a)
- hit, conflict := c.Lookup("x", []byte{1})
- if conflict || hit == nil {
- t.Fatal("same payload must hit")
- }
- _, conflict = c.Lookup("x", []byte{2})
- if !conflict {
- t.Fatal("different payload must conflict")
- }
- }
|