telemetry_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package telemetry_test
  2. import (
  3. "testing"
  4. "time"
  5. "google.golang.org/protobuf/types/known/timestamppb"
  6. hthv1 "hth/internal/gen/hth/api/v1"
  7. "hth/internal/telemetry"
  8. )
  9. func snap(id uint64) *hthv1.Snapshot {
  10. return &hthv1.Snapshot{DeviceId: "d0", SnapshotSeq: id, EventId: id}
  11. }
  12. func assertStrictIDs(t *testing.T, ids []uint64) {
  13. t.Helper()
  14. var last uint64
  15. for i, id := range ids {
  16. if id == 0 {
  17. t.Fatalf("event_id 0 at %d", i)
  18. }
  19. if i > 0 && id <= last {
  20. t.Fatalf("event ids not strictly increasing at %d: %v", i, ids)
  21. }
  22. last = id
  23. }
  24. }
  25. func TestSlowSubscriberDoesNotBlockAndReportsGap(t *testing.T) {
  26. h := telemetry.NewHub(8, 1, 0, 0)
  27. sub, _, _ := h.SubscribeSnapshots(0)
  28. defer sub.Cancel()
  29. now := time.Unix(1, 0)
  30. h.PublishSnapshot(snap(1), now)
  31. h.PublishSnapshot(snap(2), now)
  32. h.PublishSnapshot(snap(3), now)
  33. if sub.Dropped() == 0 {
  34. t.Fatal("expected drops")
  35. }
  36. n := 0
  37. for {
  38. select {
  39. case s := <-sub.C:
  40. n++
  41. if s.DataGap && s.DroppedTelemetryCount == 0 {
  42. t.Fatal("gap should include dropped count")
  43. }
  44. default:
  45. if n == 0 {
  46. t.Fatal("should have received at least one")
  47. }
  48. return
  49. }
  50. }
  51. }
  52. func TestEventGapAttachesToNextRealEvent(t *testing.T) {
  53. h := telemetry.NewHub(8, 1, 0, 0)
  54. sub, _, _ := h.SubscribeEvents(0)
  55. defer sub.Cancel()
  56. now := time.Unix(1, 0)
  57. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  58. h.PublishEvent(&hthv1.Event{EventId: 2, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  59. h.PublishEvent(&hthv1.Event{EventId: 3, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  60. select {
  61. case <-sub.C:
  62. default:
  63. t.Fatal("expected buffered event")
  64. }
  65. h.PublishEvent(&hthv1.Event{EventId: 4, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  66. select {
  67. case e := <-sub.C:
  68. if e.Kind == hthv1.EventKind_EVENT_KIND_DATA_GAP {
  69. t.Fatal("per-subscriber gap must not allocate a DATA_GAP event id")
  70. }
  71. if e.EventId != 4 {
  72. t.Fatalf("want real event 4, got %d kind=%v", e.EventId, e.Kind)
  73. }
  74. if e.GetDeliveryGap() == nil || e.GetDeliveryGap().DroppedCount == 0 {
  75. t.Fatalf("expected delivery_gap on real event: %+v", e.GetDeliveryGap())
  76. }
  77. default:
  78. t.Fatal("expected next real event with delivery_gap")
  79. }
  80. }
  81. func TestSlowSubscriberIDsStayStrictlyIncreasing(t *testing.T) {
  82. h := telemetry.NewHub(32, 1, 0, 0)
  83. sub, _, _ := h.SubscribeEvents(0)
  84. defer sub.Cancel()
  85. now := time.Unix(1, 0)
  86. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  87. <-sub.C
  88. for i := uint64(2); i <= 20; i++ {
  89. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  90. }
  91. h.PublishEvent(&hthv1.Event{EventId: 21, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  92. var ids []uint64
  93. for {
  94. select {
  95. case e := <-sub.C:
  96. ids = append(ids, e.EventId)
  97. if e.EventId == 0 {
  98. t.Fatal("event_id 0")
  99. }
  100. if e.Kind == hthv1.EventKind_EVENT_KIND_DATA_GAP {
  101. t.Fatal("standalone DATA_GAP must not appear on the live stream")
  102. }
  103. default:
  104. assertStrictIDs(t, ids)
  105. if len(ids) < 1 {
  106. t.Fatalf("expected recovered event, got %v", ids)
  107. }
  108. return
  109. }
  110. }
  111. }
  112. func TestRingResumeAttachesDeliveryGapWithoutNewID(t *testing.T) {
  113. h := telemetry.NewHub(4, 8, 0, 0)
  114. now := time.Unix(1, 0)
  115. for i := uint64(1); i <= 10; i++ {
  116. h.PublishSnapshot(snap(i), now)
  117. }
  118. _, replay, gap := h.SubscribeEvents(1)
  119. if gap != nil {
  120. t.Fatalf("resume gap must not consume a global event: %+v", gap)
  121. }
  122. if len(replay) == 0 {
  123. t.Fatal("should replay remaining ring")
  124. }
  125. if replay[0].GetDeliveryGap() == nil {
  126. t.Fatal("first replayed event must carry delivery_gap")
  127. }
  128. var ids []uint64
  129. for _, e := range replay {
  130. ids = append(ids, e.EventId)
  131. }
  132. assertStrictIDs(t, ids)
  133. }
  134. func TestSnapshotResumeIgnoresNonSnapshotEvents(t *testing.T) {
  135. h := telemetry.NewHub(16, 8, 0, 0)
  136. now := time.Unix(1, 0)
  137. h.PublishSnapshot(snap(1), now)
  138. h.PublishEvent(&hthv1.Event{
  139. EventId: 2, Kind: hthv1.EventKind_EVENT_KIND_FAULT_RAISED, DeviceId: "d0",
  140. Snapshot: snap(99), WallTime: timestamppb.New(now),
  141. })
  142. h.PublishSnapshot(snap(3), now)
  143. _, replay, _ := h.SubscribeSnapshots(0)
  144. if len(replay) != 0 {
  145. t.Fatalf("resume 0 should not dump ring, got %d", len(replay))
  146. }
  147. _, replay, _ = h.SubscribeSnapshots(1)
  148. if len(replay) != 1 || replay[0].EventId != 3 {
  149. t.Fatalf("want only SNAPSHOT event 3, got %+v", replay)
  150. }
  151. }
  152. func TestSubscriberLimit(t *testing.T) {
  153. h := telemetry.NewHub(4, 2, 2, 1)
  154. _, _, _, err := h.SubscribeSnapshotsPeer(0, "p1")
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. _, _, _, err = h.SubscribeEventsPeer(0, "p1")
  159. if err != telemetry.ErrTooManyPeerStreams {
  160. t.Fatalf("peer limit: %v", err)
  161. }
  162. _, _, _, err = h.SubscribeEventsPeer(0, "p2")
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. _, _, _, err = h.SubscribeSnapshotsPeer(0, "p3")
  167. if err != telemetry.ErrTooManySubscribers {
  168. t.Fatalf("global limit: %v", err)
  169. }
  170. }
  171. func TestResumeOlderThanRingDoesNotAllocateGapID(t *testing.T) {
  172. h := telemetry.NewHub(4, 1, 0, 0)
  173. now := time.Unix(1, 0)
  174. for i := uint64(1); i <= 10; i++ {
  175. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  176. }
  177. sub, replay, gap := h.SubscribeEvents(1)
  178. defer sub.Cancel()
  179. if gap != nil && gap.EventId != 0 {
  180. t.Fatal("standalone resume DATA_GAP must not allocate a stream event_id")
  181. }
  182. if len(replay) == 0 || replay[0].GetDeliveryGap() == nil {
  183. t.Fatal("expected delivery_gap on first available event")
  184. }
  185. if replay[0].GetDeliveryGap().LastContiguousEventId != 1 {
  186. t.Fatalf("resume last contiguous: %+v", replay[0].GetDeliveryGap())
  187. }
  188. var ids []uint64
  189. for _, e := range replay {
  190. ids = append(ids, e.EventId)
  191. }
  192. assertStrictIDs(t, ids)
  193. }
  194. func TestEventIDsStayMonotonicWithDuplicateSourceIDs(t *testing.T) {
  195. h := telemetry.NewHub(16, 8, 0, 0)
  196. var next uint64
  197. h.SetIDAlloc(func() uint64 {
  198. next++
  199. return next
  200. })
  201. now := time.Unix(1, 0)
  202. srcs := []uint64{9, 9, 1, 1, 100, 2}
  203. var last uint64
  204. for i, src := range srcs {
  205. next++
  206. local := next
  207. h.PublishEvent(&hthv1.Event{
  208. EventId: local, SourceEventId: src, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT,
  209. DeviceId: "d0", WallTime: timestamppb.New(now),
  210. })
  211. if local <= last {
  212. t.Fatalf("local event id not monotonic at %d: %d <= %d", i, local, last)
  213. }
  214. last = local
  215. }
  216. }
  217. func TestRepeatedGapsThenRecover(t *testing.T) {
  218. h := telemetry.NewHub(64, 1, 0, 0)
  219. sub, _, _ := h.SubscribeEvents(0)
  220. defer sub.Cancel()
  221. now := time.Unix(1, 0)
  222. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_SNAPSHOT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  223. <-sub.C
  224. for i := uint64(2); i <= 8; i++ {
  225. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  226. }
  227. <-sub.C
  228. h.PublishEvent(&hthv1.Event{EventId: 9, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  229. e := <-sub.C
  230. if e.EventId <= 1 {
  231. t.Fatalf("recovered id must be > 1, got %d", e.EventId)
  232. }
  233. if e.GetDeliveryGap() == nil {
  234. t.Fatal("recovery event must describe the transport gap")
  235. }
  236. h.PublishEvent(&hthv1.Event{EventId: 10, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  237. e2 := <-sub.C
  238. if e2.EventId <= e.EventId {
  239. t.Fatalf("ids must keep increasing: %d then %d", e.EventId, e2.EventId)
  240. }
  241. }
  242. func FuzzSubscribeResume(f *testing.F) {
  243. f.Add(uint64(0), uint64(1))
  244. f.Add(uint64(1), uint64(0))
  245. f.Add(uint64(9), uint64(9))
  246. f.Fuzz(func(t *testing.T, resumeAfter, extra uint64) {
  247. extra = extra % 32
  248. h := telemetry.NewHub(8, 2, 0, 0)
  249. now := time.Unix(1, 0)
  250. for i := uint64(1); i <= extra+1; i++ {
  251. src := extra + 2 - i
  252. h.PublishEvent(&hthv1.Event{
  253. EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0",
  254. WallTime: timestamppb.New(now), SourceEventId: src,
  255. })
  256. }
  257. sub, replay, gap := h.SubscribeEvents(resumeAfter)
  258. if sub != nil {
  259. sub.Cancel()
  260. }
  261. if gap != nil && gap.EventId == 0 {
  262. t.Fatal("gap event id 0")
  263. }
  264. var ids []uint64
  265. for _, e := range replay {
  266. ids = append(ids, e.EventId)
  267. }
  268. assertStrictIDs(t, ids)
  269. })
  270. }