package telemetry_test import ( "errors" "testing" "time" "google.golang.org/protobuf/types/known/timestamppb" hthv1 "hth/internal/gen/hth/api/v1" "hth/internal/telemetry" ) func snap(id uint64) *hthv1.Snapshot { return &hthv1.Snapshot{DeviceId: "d0", SnapshotSeq: id, EventId: id} } func assertStrictIDs(t *testing.T, ids []uint64) { t.Helper() var last uint64 for i, id := range ids { if id == 0 { t.Fatalf("event_id 0 at %d", i) } if i > 0 && id <= last { t.Fatalf("event ids not strictly increasing at %d: %v", i, ids) } last = id } } func TestSlowSubscriberDoesNotBlockAndReportsGap(t *testing.T) { h := telemetry.NewHub(8, 1, 0, 0) sub, _, _ := h.SubscribeSnapshots(0) defer sub.Cancel() now := time.Unix(1, 0) h.PublishSnapshot(snap(1), now) h.PublishSnapshot(snap(2), now) h.PublishSnapshot(snap(3), now) if sub.Dropped() == 0 { t.Fatal("expected drops") } n := 0 for { select { case s := <-sub.C: n++ if s.DataGap && s.DroppedTelemetryCount == 0 { t.Fatal("gap should include dropped count") } default: if n == 0 { t.Fatal("should have received at least one") } return } } } func TestEventGapAttachesToNextRealEvent(t *testing.T) { h := telemetry.NewHub(8, 1, 0, 0) sub, _, _ := h.SubscribeEvents(0) defer sub.Cancel() now := time.Unix(1, 0) h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) h.PublishEvent(&hthv1.Event{EventId: 2, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) h.PublishEvent(&hthv1.Event{EventId: 3, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) select { case <-sub.C: default: t.Fatal("expected buffered event") } h.PublishEvent(&hthv1.Event{EventId: 4, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) select { case e := <-sub.C: if e.Kind == hthv1.EventKind_EVENT_KIND_DATA_GAP { t.Fatal("per-subscriber gap must not allocate a DATA_GAP event id") } if e.EventId != 4 { t.Fatalf("want real event 4, got %d kind=%v", e.EventId, e.Kind) } if e.GetDeliveryGap() == nil || e.GetDeliveryGap().DroppedCount == 0 { t.Fatalf("expected delivery_gap on real event: %+v", e.GetDeliveryGap()) } default: t.Fatal("expected next real event with delivery_gap") } } func TestSlowSubscriberIDsStayStrictlyIncreasing(t *testing.T) { h := telemetry.NewHub(32, 1, 0, 0) sub, _, _ := h.SubscribeEvents(0) defer sub.Cancel() now := time.Unix(1, 0) h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) <-sub.C for i := uint64(2); i <= 20; i++ { h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) } h.PublishEvent(&hthv1.Event{EventId: 21, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) var ids []uint64 for { select { case e := <-sub.C: ids = append(ids, e.EventId) if e.EventId == 0 { t.Fatal("event_id 0") } if e.Kind == hthv1.EventKind_EVENT_KIND_DATA_GAP { t.Fatal("standalone DATA_GAP must not appear on the live stream") } default: assertStrictIDs(t, ids) if len(ids) < 1 { t.Fatalf("expected recovered event, got %v", ids) } return } } } func TestRingResumeAttachesDeliveryGapWithoutNewID(t *testing.T) { h := telemetry.NewHub(4, 8, 0, 0) now := time.Unix(1, 0) for i := uint64(1); i <= 10; i++ { h.PublishSnapshot(snap(i), now) } _, replay, gap := h.SubscribeEvents(1) if gap != nil { t.Fatalf("resume gap must not consume a global event: %+v", gap) } if len(replay) == 0 { t.Fatal("should replay remaining ring") } if replay[0].GetDeliveryGap() == nil { t.Fatal("first replayed event must carry delivery_gap") } var ids []uint64 for _, e := range replay { ids = append(ids, e.EventId) } assertStrictIDs(t, ids) } func TestSnapshotResumeIgnoresNonSnapshotEvents(t *testing.T) { h := telemetry.NewHub(16, 8, 0, 0) now := time.Unix(1, 0) h.PublishSnapshot(snap(1), now) h.PublishEvent(&hthv1.Event{ EventId: 2, Kind: hthv1.EventKind_EVENT_KIND_FAULT_RAISED, DeviceId: "d0", Snapshot: snap(99), WallTime: timestamppb.New(now), }) h.PublishSnapshot(snap(3), now) _, replay, _ := h.SubscribeSnapshots(0) if len(replay) != 0 { t.Fatalf("resume 0 should not dump ring, got %d", len(replay)) } _, replay, _ = h.SubscribeSnapshots(1) if len(replay) != 1 || replay[0].EventId != 3 { t.Fatalf("want only SNAPSHOT event 3, got %+v", replay) } } func TestSubscriberLimit(t *testing.T) { h := telemetry.NewHub(4, 2, 2, 1) _, _, _, err := h.SubscribeSnapshotsPeer(0, "p1") if err != nil { t.Fatal(err) } _, _, _, err = h.SubscribeEventsPeer(0, "p1") if err != telemetry.ErrTooManyPeerStreams { t.Fatalf("peer limit: %v", err) } _, _, _, err = h.SubscribeEventsPeer(0, "p2") if err != nil { t.Fatal(err) } _, _, _, err = h.SubscribeSnapshotsPeer(0, "p3") if err != telemetry.ErrTooManySubscribers { t.Fatalf("global limit: %v", err) } } func TestResumeOlderThanRingDoesNotAllocateGapID(t *testing.T) { h := telemetry.NewHub(4, 1, 0, 0) now := time.Unix(1, 0) for i := uint64(1); i <= 10; i++ { h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) } sub, replay, gap := h.SubscribeEvents(1) defer sub.Cancel() if gap != nil && gap.EventId != 0 { t.Fatal("standalone resume DATA_GAP must not allocate a stream event_id") } if len(replay) == 0 || replay[0].GetDeliveryGap() == nil { t.Fatal("expected delivery_gap on first available event") } if replay[0].GetDeliveryGap().LastContiguousEventId != 1 { t.Fatalf("resume last contiguous: %+v", replay[0].GetDeliveryGap()) } var ids []uint64 for _, e := range replay { ids = append(ids, e.EventId) } assertStrictIDs(t, ids) } func TestEventIDsStayMonotonicWithDuplicateSourceIDs(t *testing.T) { h := telemetry.NewHub(16, 8, 0, 0) var next uint64 h.SetIDAlloc(func() uint64 { next++ return next }) now := time.Unix(1, 0) srcs := []uint64{9, 9, 1, 1, 100, 2} var last uint64 for i, src := range srcs { next++ local := next h.PublishEvent(&hthv1.Event{ EventId: local, SourceEventId: src, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now), }) if local <= last { t.Fatalf("local event id not monotonic at %d: %d <= %d", i, local, last) } last = local } } func TestRepeatedGapsThenRecover(t *testing.T) { h := telemetry.NewHub(64, 1, 0, 0) sub, _, _ := h.SubscribeEvents(0) defer sub.Cancel() now := time.Unix(1, 0) h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_SNAPSHOT, DeviceId: "d0", WallTime: timestamppb.New(now)}) <-sub.C for i := uint64(2); i <= 8; i++ { h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) } <-sub.C h.PublishEvent(&hthv1.Event{EventId: 9, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) e := <-sub.C if e.EventId <= 1 { t.Fatalf("recovered id must be > 1, got %d", e.EventId) } if e.GetDeliveryGap() == nil { t.Fatal("recovery event must describe the transport gap") } h.PublishEvent(&hthv1.Event{EventId: 10, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) e2 := <-sub.C if e2.EventId <= e.EventId { t.Fatalf("ids must keep increasing: %d then %d", e.EventId, e2.EventId) } } func FuzzSubscribeResume(f *testing.F) { f.Add(uint64(0), uint64(1)) f.Add(uint64(1), uint64(0)) f.Add(uint64(9), uint64(9)) f.Fuzz(func(t *testing.T, resumeAfter, extra uint64) { extra = extra % 32 h := telemetry.NewHub(8, 2, 0, 0) now := time.Unix(1, 0) for i := uint64(1); i <= extra+1; i++ { src := extra + 2 - i h.PublishEvent(&hthv1.Event{ EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now), SourceEventId: src, }) } sub, replay, gap := h.SubscribeEvents(resumeAfter) if sub != nil { sub.Cancel() } if gap != nil && gap.EventId == 0 { t.Fatal("gap event id 0") } var ids []uint64 for _, e := range replay { ids = append(ids, e.EventId) } assertStrictIDs(t, ids) }) } func TestFutureResumeRejected(t *testing.T) { h := telemetry.NewHub(8, 8, 0, 0) _, _, _, err := h.SubscribeEventsPeer(1, "p") if !errors.Is(err, telemetry.ErrResumeOutOfRange) { t.Fatalf("empty ring resume 1: %v", err) } sub0, replay0, _, err := h.SubscribeEventsPeer(0, "empty0") if err != nil { t.Fatal(err) } if len(replay0) != 0 { t.Fatal("empty ring resume 0 must not invent events") } sub0.Cancel() h.PublishSnapshot(snap(1), time.Unix(1, 0)) sub, _, _, err := h.SubscribeEventsPeer(1, "p") if err != nil { t.Fatal(err) } sub.Cancel() newest := uint64(1) subN, _, _, err := h.SubscribeEventsPeer(newest, "newest") if err != nil { t.Fatal(err) } subN.Cancel() _, _, _, err = h.SubscribeEventsPeer(9, "p") if !errors.Is(err, telemetry.ErrResumeOutOfRange) { t.Fatalf("future resume: %v", err) } _, _, _, err = h.SubscribeSnapshotsPeer(99, "p") if !errors.Is(err, telemetry.ErrResumeOutOfRange) { t.Fatalf("future snapshot resume: %v", err) } } func TestResumeOlderThanOldestThenConcurrentPublish(t *testing.T) { h := telemetry.NewHub(4, 8, 0, 0) now := time.Unix(1, 0) for i := uint64(1); i <= 8; i++ { h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) } sub, replay, _, err := h.SubscribeEventsPeer(1, "old") if err != nil { t.Fatal(err) } defer sub.Cancel() if len(replay) == 0 { t.Fatal("resume older than ring should still replay remaining events") } done := make(chan struct{}) go func() { defer close(done) for i := uint64(9); i <= 16; i++ { h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)}) } }() <-done _, _, _, err = h.SubscribeEventsPeer(100, "future-after") if !errors.Is(err, telemetry.ErrResumeOutOfRange) { t.Fatalf("future after concurrent publish: %v", err) } }