control_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package control_test
  2. import (
  3. "testing"
  4. "time"
  5. "google.golang.org/protobuf/types/known/timestamppb"
  6. "hth/internal/control"
  7. hthv1 "hth/internal/gen/hth/api/v1"
  8. )
  9. func TestLeaseAcquireRenewExpire(t *testing.T) {
  10. m := control.NewManager("d0", time.Second, 10*time.Second, 8)
  11. now := time.Unix(1000, 0)
  12. l, err := m.Acquire(now, "alice", 2*time.Second)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if l.ID == "" || l.Epoch == 0 {
  17. t.Fatal("id/epoch")
  18. }
  19. if _, err := m.Acquire(now, "bob", time.Second); err == nil {
  20. t.Fatal("second holder")
  21. }
  22. if _, err := m.Renew(now.Add(time.Second), l.ID, l.Epoch, 2*time.Second); err != nil {
  23. t.Fatal(err)
  24. }
  25. if expired := m.ExpireIfNeeded(now.Add(10 * time.Second)); expired == nil {
  26. t.Fatal("should expire")
  27. }
  28. if m.Current() != nil {
  29. t.Fatal("cleared")
  30. }
  31. }
  32. func TestReconnectInvalidates(t *testing.T) {
  33. m := control.NewManager("d0", time.Second, 10*time.Second, 8)
  34. now := time.Unix(1, 0)
  35. l, _ := m.Acquire(now, "alice", time.Second)
  36. m.InvalidateAndBump()
  37. batch := &hthv1.ActionBatch{CommandId: "c1", LeaseId: l.ID, LeaseEpoch: l.Epoch}
  38. if got := m.CheckAction(now, batch, 1, 1); got != hthv1.RejectReason_REJECT_REASON_LEASE_EXPIRED {
  39. t.Fatalf("got %v", got)
  40. }
  41. }
  42. func TestCommandIDCache(t *testing.T) {
  43. c := control.NewCache(2)
  44. b1 := &hthv1.ActionBatch{CommandId: "c1", DeviceId: "d", ContinuousTargets: []*hthv1.ContinuousTarget{{Id: "fan.chamber.1", Value: 10}}}
  45. b2 := &hthv1.ActionBatch{CommandId: "c1", DeviceId: "d", ContinuousTargets: []*hthv1.ContinuousTarget{{Id: "fan.chamber.1", Value: 11}}}
  46. h1 := control.PayloadHash(b1)
  47. h2 := control.PayloadHash(b2)
  48. res := &hthv1.ActionResult{CommandId: "c1", Accepted: true}
  49. c.Store("c1", h1, res)
  50. hit, conflict := c.Lookup("c1", h1)
  51. if hit == nil || conflict {
  52. t.Fatal("replay")
  53. }
  54. hit, conflict = c.Lookup("c1", h2)
  55. if hit != nil || !conflict {
  56. t.Fatal("conflict")
  57. }
  58. }
  59. func TestStaleSnapshotAndDeadline(t *testing.T) {
  60. m := control.NewManager("d0", time.Second, 10*time.Second, 8)
  61. now := time.Unix(50, 0)
  62. l, _ := m.Acquire(now, "alice", time.Second)
  63. batch := &hthv1.ActionBatch{
  64. CommandId: "c1",
  65. LeaseId: l.ID,
  66. LeaseEpoch: l.Epoch,
  67. BasedOnSnapshotSeq: 1,
  68. ExpectedControlRevision: 9,
  69. ValidUntil: timestamppb.New(now.Add(-time.Millisecond)),
  70. }
  71. if got := m.CheckAction(now, batch, 2, 9); got != hthv1.RejectReason_REJECT_REASON_EXPIRED_DEADLINE {
  72. t.Fatalf("deadline got %v", got)
  73. }
  74. batch.ValidUntil = timestamppb.New(now.Add(time.Second))
  75. if got := m.CheckAction(now, batch, 100, 9); got != hthv1.RejectReason_REJECT_REASON_STALE_SNAPSHOT {
  76. t.Fatalf("stale snap got %v", got)
  77. }
  78. batch.BasedOnSnapshotSeq = 100
  79. if got := m.CheckAction(now, batch, 100, 1); got != hthv1.RejectReason_REJECT_REASON_STALE_CONTROL_REVISION {
  80. t.Fatalf("stale rev got %v", got)
  81. }
  82. }
  83. func TestCacheBounded(t *testing.T) {
  84. c := control.NewCache(2)
  85. r := &hthv1.ActionResult{Accepted: true}
  86. c.Store("a", []byte{1}, r)
  87. c.Store("b", []byte{2}, r)
  88. c.Store("c", []byte{3}, r)
  89. if hit, _ := c.Lookup("a", []byte{1}); hit != nil {
  90. t.Fatal("oldest should evict")
  91. }
  92. }