machine_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package control_test
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. "hth/internal/control"
  7. hthv1 "hth/internal/gen/hth/api/v1"
  8. )
  9. func TestLeaseStateMachineInvariants(t *testing.T) {
  10. rng := rand.New(rand.NewSource(42))
  11. now := time.Unix(1_700_000_000, 0)
  12. m := control.NewManager("d0", 50*time.Millisecond, time.Second, 16)
  13. type ref struct {
  14. id string
  15. epoch uint64
  16. exp time.Time
  17. maxE uint64
  18. }
  19. var r ref
  20. for i := 0; i < 400; i++ {
  21. now = now.Add(time.Duration(rng.Intn(80)) * time.Millisecond)
  22. switch rng.Intn(7) {
  23. case 0:
  24. l, err := m.Acquire(now, "h", 200*time.Millisecond)
  25. if r.id != "" && now.Before(r.exp) {
  26. if err == nil {
  27. t.Fatalf("step %d: acquired while active", i)
  28. }
  29. } else if err == nil {
  30. if l.Epoch <= r.maxE && r.maxE != 0 {
  31. t.Fatalf("step %d: epoch did not increase %d -> %d", i, r.maxE, l.Epoch)
  32. }
  33. r.id, r.epoch, r.exp = l.ID, l.Epoch, l.Expires
  34. r.maxE = l.Epoch
  35. }
  36. case 1:
  37. if r.id == "" {
  38. continue
  39. }
  40. _, err := m.Renew(now, r.id, r.epoch, 200*time.Millisecond)
  41. active := now.Before(r.exp)
  42. if active && err != nil {
  43. t.Fatalf("step %d: renew failed: %v", i, err)
  44. }
  45. if !active && err == nil {
  46. t.Fatalf("step %d: renewed expired lease", i)
  47. }
  48. if err == nil {
  49. r.exp = m.Current().Expires
  50. }
  51. case 2:
  52. if r.id == "" {
  53. continue
  54. }
  55. err := m.Release(now, r.id, r.epoch)
  56. if now.Before(r.exp) && err != nil {
  57. t.Fatalf("step %d: release: %v", i, err)
  58. }
  59. if err == nil {
  60. r.id = ""
  61. r.exp = time.Time{}
  62. }
  63. case 3:
  64. old := m.ExpireIfNeeded(now)
  65. if old != nil {
  66. r.id = ""
  67. r.exp = time.Time{}
  68. }
  69. case 4:
  70. prev := r.maxE
  71. m.InvalidateAndBump()
  72. r.id = ""
  73. r.exp = time.Time{}
  74. if m.Current() != nil {
  75. t.Fatal("lease after invalidate")
  76. }
  77. // epoch is unexported except via next acquire
  78. _ = prev
  79. case 5:
  80. batch := &hthv1.ActionBatch{CommandId: "c", LeaseId: r.id, LeaseEpoch: r.epoch}
  81. rr := m.CheckAction(now, batch, 1, 0)
  82. if r.id == "" || !now.Before(r.exp) {
  83. if rr == hthv1.RejectReason_REJECT_REASON_UNSPECIFIED {
  84. t.Fatalf("step %d: stale lease accepted", i)
  85. }
  86. }
  87. case 6:
  88. if r.id == "" {
  89. continue
  90. }
  91. batch := &hthv1.ActionBatch{CommandId: "old", LeaseId: r.id, LeaseEpoch: r.epoch + 99}
  92. rr := m.CheckAction(now, batch, 1, 0)
  93. if rr != hthv1.RejectReason_REJECT_REASON_LEASE_MISMATCH && rr != hthv1.RejectReason_REJECT_REASON_LEASE_EXPIRED {
  94. t.Fatalf("step %d: old epoch not rejected: %v", i, rr)
  95. }
  96. }
  97. if m.Current() != nil && m.Current().Active(now) {
  98. if r.id == "" {
  99. t.Fatalf("step %d: manager has lease, ref empty", i)
  100. }
  101. }
  102. cur := m.Current()
  103. n := 0
  104. if cur != nil && cur.Active(now) {
  105. n = 1
  106. }
  107. if n > 1 {
  108. t.Fatal("more than one lease")
  109. }
  110. }
  111. }
  112. func TestIdempotentCacheConflict(t *testing.T) {
  113. c := control.NewCache(4)
  114. a := &hthv1.ActionResult{CommandId: "x", Accepted: true}
  115. c.Store("x", []byte{1}, a)
  116. hit, conflict := c.Lookup("x", []byte{1})
  117. if conflict || hit == nil {
  118. t.Fatal("same payload must hit")
  119. }
  120. _, conflict = c.Lookup("x", []byte{2})
  121. if !conflict {
  122. t.Fatal("different payload must conflict")
  123. }
  124. }