telemetry_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package telemetry_test
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. "google.golang.org/protobuf/types/known/timestamppb"
  7. hthv1 "hth/internal/gen/hth/api/v1"
  8. "hth/internal/telemetry"
  9. )
  10. func snap(id uint64) *hthv1.Snapshot {
  11. return &hthv1.Snapshot{DeviceId: "d0", SnapshotSeq: id, EventId: id}
  12. }
  13. func assertStrictIDs(t *testing.T, ids []uint64) {
  14. t.Helper()
  15. var last uint64
  16. for i, id := range ids {
  17. if id == 0 {
  18. t.Fatalf("event_id 0 at %d", i)
  19. }
  20. if i > 0 && id <= last {
  21. t.Fatalf("event ids not strictly increasing at %d: %v", i, ids)
  22. }
  23. last = id
  24. }
  25. }
  26. func TestSlowSubscriberDoesNotBlockAndReportsGap(t *testing.T) {
  27. h := telemetry.NewHub(8, 1, 0, 0)
  28. sub, _, _ := h.SubscribeSnapshots(0)
  29. defer sub.Cancel()
  30. now := time.Unix(1, 0)
  31. h.PublishSnapshot(snap(1), now)
  32. h.PublishSnapshot(snap(2), now)
  33. h.PublishSnapshot(snap(3), now)
  34. if sub.Dropped() == 0 {
  35. t.Fatal("expected drops")
  36. }
  37. n := 0
  38. for {
  39. select {
  40. case s := <-sub.C:
  41. n++
  42. if s.DataGap && s.DroppedTelemetryCount == 0 {
  43. t.Fatal("gap should include dropped count")
  44. }
  45. default:
  46. if n == 0 {
  47. t.Fatal("should have received at least one")
  48. }
  49. return
  50. }
  51. }
  52. }
  53. func TestEventGapAttachesToNextRealEvent(t *testing.T) {
  54. h := telemetry.NewHub(8, 1, 0, 0)
  55. sub, _, _ := h.SubscribeEvents(0)
  56. defer sub.Cancel()
  57. now := time.Unix(1, 0)
  58. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  59. h.PublishEvent(&hthv1.Event{EventId: 2, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  60. h.PublishEvent(&hthv1.Event{EventId: 3, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  61. select {
  62. case <-sub.C:
  63. default:
  64. t.Fatal("expected buffered event")
  65. }
  66. h.PublishEvent(&hthv1.Event{EventId: 4, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  67. select {
  68. case e := <-sub.C:
  69. if e.Kind == hthv1.EventKind_EVENT_KIND_DATA_GAP {
  70. t.Fatal("per-subscriber gap must not allocate a DATA_GAP event id")
  71. }
  72. if e.EventId != 4 {
  73. t.Fatalf("want real event 4, got %d kind=%v", e.EventId, e.Kind)
  74. }
  75. if e.GetDeliveryGap() == nil || e.GetDeliveryGap().DroppedCount == 0 {
  76. t.Fatalf("expected delivery_gap on real event: %+v", e.GetDeliveryGap())
  77. }
  78. default:
  79. t.Fatal("expected next real event with delivery_gap")
  80. }
  81. }
  82. func TestSlowSubscriberIDsStayStrictlyIncreasing(t *testing.T) {
  83. h := telemetry.NewHub(32, 1, 0, 0)
  84. sub, _, _ := h.SubscribeEvents(0)
  85. defer sub.Cancel()
  86. now := time.Unix(1, 0)
  87. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  88. <-sub.C
  89. for i := uint64(2); i <= 20; i++ {
  90. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  91. }
  92. h.PublishEvent(&hthv1.Event{EventId: 21, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  93. var ids []uint64
  94. for {
  95. select {
  96. case e := <-sub.C:
  97. ids = append(ids, e.EventId)
  98. if e.EventId == 0 {
  99. t.Fatal("event_id 0")
  100. }
  101. if e.Kind == hthv1.EventKind_EVENT_KIND_DATA_GAP {
  102. t.Fatal("standalone DATA_GAP must not appear on the live stream")
  103. }
  104. default:
  105. assertStrictIDs(t, ids)
  106. if len(ids) < 1 {
  107. t.Fatalf("expected recovered event, got %v", ids)
  108. }
  109. return
  110. }
  111. }
  112. }
  113. func TestRingResumeAttachesDeliveryGapWithoutNewID(t *testing.T) {
  114. h := telemetry.NewHub(4, 8, 0, 0)
  115. now := time.Unix(1, 0)
  116. for i := uint64(1); i <= 10; i++ {
  117. h.PublishSnapshot(snap(i), now)
  118. }
  119. _, replay, gap := h.SubscribeEvents(1)
  120. if gap != nil {
  121. t.Fatalf("resume gap must not consume a global event: %+v", gap)
  122. }
  123. if len(replay) == 0 {
  124. t.Fatal("should replay remaining ring")
  125. }
  126. if replay[0].GetDeliveryGap() == nil {
  127. t.Fatal("first replayed event must carry delivery_gap")
  128. }
  129. var ids []uint64
  130. for _, e := range replay {
  131. ids = append(ids, e.EventId)
  132. }
  133. assertStrictIDs(t, ids)
  134. }
  135. func TestSnapshotResumeIgnoresNonSnapshotEvents(t *testing.T) {
  136. h := telemetry.NewHub(16, 8, 0, 0)
  137. now := time.Unix(1, 0)
  138. h.PublishSnapshot(snap(1), now)
  139. h.PublishEvent(&hthv1.Event{
  140. EventId: 2, Kind: hthv1.EventKind_EVENT_KIND_FAULT_RAISED, DeviceId: "d0",
  141. Snapshot: snap(99), WallTime: timestamppb.New(now),
  142. })
  143. h.PublishSnapshot(snap(3), now)
  144. _, replay, _ := h.SubscribeSnapshots(0)
  145. if len(replay) != 0 {
  146. t.Fatalf("resume 0 should not dump ring, got %d", len(replay))
  147. }
  148. _, replay, _ = h.SubscribeSnapshots(1)
  149. if len(replay) != 1 || replay[0].EventId != 3 {
  150. t.Fatalf("want only SNAPSHOT event 3, got %+v", replay)
  151. }
  152. }
  153. func TestSubscriberLimit(t *testing.T) {
  154. h := telemetry.NewHub(4, 2, 2, 1)
  155. _, _, _, err := h.SubscribeSnapshotsPeer(0, "p1")
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. _, _, _, err = h.SubscribeEventsPeer(0, "p1")
  160. if err != telemetry.ErrTooManyPeerStreams {
  161. t.Fatalf("peer limit: %v", err)
  162. }
  163. _, _, _, err = h.SubscribeEventsPeer(0, "p2")
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. _, _, _, err = h.SubscribeSnapshotsPeer(0, "p3")
  168. if err != telemetry.ErrTooManySubscribers {
  169. t.Fatalf("global limit: %v", err)
  170. }
  171. }
  172. func TestResumeOlderThanRingDoesNotAllocateGapID(t *testing.T) {
  173. h := telemetry.NewHub(4, 1, 0, 0)
  174. now := time.Unix(1, 0)
  175. for i := uint64(1); i <= 10; i++ {
  176. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  177. }
  178. sub, replay, gap := h.SubscribeEvents(1)
  179. defer sub.Cancel()
  180. if gap != nil && gap.EventId != 0 {
  181. t.Fatal("standalone resume DATA_GAP must not allocate a stream event_id")
  182. }
  183. if len(replay) == 0 || replay[0].GetDeliveryGap() == nil {
  184. t.Fatal("expected delivery_gap on first available event")
  185. }
  186. if replay[0].GetDeliveryGap().LastContiguousEventId != 1 {
  187. t.Fatalf("resume last contiguous: %+v", replay[0].GetDeliveryGap())
  188. }
  189. var ids []uint64
  190. for _, e := range replay {
  191. ids = append(ids, e.EventId)
  192. }
  193. assertStrictIDs(t, ids)
  194. }
  195. func TestEventIDsStayMonotonicWithDuplicateSourceIDs(t *testing.T) {
  196. h := telemetry.NewHub(16, 8, 0, 0)
  197. var next uint64
  198. h.SetIDAlloc(func() uint64 {
  199. next++
  200. return next
  201. })
  202. now := time.Unix(1, 0)
  203. srcs := []uint64{9, 9, 1, 1, 100, 2}
  204. var last uint64
  205. for i, src := range srcs {
  206. next++
  207. local := next
  208. h.PublishEvent(&hthv1.Event{
  209. EventId: local, SourceEventId: src, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT,
  210. DeviceId: "d0", WallTime: timestamppb.New(now),
  211. })
  212. if local <= last {
  213. t.Fatalf("local event id not monotonic at %d: %d <= %d", i, local, last)
  214. }
  215. last = local
  216. }
  217. }
  218. func TestRepeatedGapsThenRecover(t *testing.T) {
  219. h := telemetry.NewHub(64, 1, 0, 0)
  220. sub, _, _ := h.SubscribeEvents(0)
  221. defer sub.Cancel()
  222. now := time.Unix(1, 0)
  223. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_SNAPSHOT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  224. <-sub.C
  225. for i := uint64(2); i <= 8; i++ {
  226. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  227. }
  228. <-sub.C
  229. h.PublishEvent(&hthv1.Event{EventId: 9, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  230. e := <-sub.C
  231. if e.EventId <= 1 {
  232. t.Fatalf("recovered id must be > 1, got %d", e.EventId)
  233. }
  234. if e.GetDeliveryGap() == nil {
  235. t.Fatal("recovery event must describe the transport gap")
  236. }
  237. h.PublishEvent(&hthv1.Event{EventId: 10, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  238. e2 := <-sub.C
  239. if e2.EventId <= e.EventId {
  240. t.Fatalf("ids must keep increasing: %d then %d", e.EventId, e2.EventId)
  241. }
  242. }
  243. func FuzzSubscribeResume(f *testing.F) {
  244. f.Add(uint64(0), uint64(1))
  245. f.Add(uint64(1), uint64(0))
  246. f.Add(uint64(9), uint64(9))
  247. f.Fuzz(func(t *testing.T, resumeAfter, extra uint64) {
  248. extra = extra % 32
  249. h := telemetry.NewHub(8, 2, 0, 0)
  250. now := time.Unix(1, 0)
  251. for i := uint64(1); i <= extra+1; i++ {
  252. src := extra + 2 - i
  253. h.PublishEvent(&hthv1.Event{
  254. EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0",
  255. WallTime: timestamppb.New(now), SourceEventId: src,
  256. })
  257. }
  258. sub, replay, gap := h.SubscribeEvents(resumeAfter)
  259. if sub != nil {
  260. sub.Cancel()
  261. }
  262. if gap != nil && gap.EventId == 0 {
  263. t.Fatal("gap event id 0")
  264. }
  265. var ids []uint64
  266. for _, e := range replay {
  267. ids = append(ids, e.EventId)
  268. }
  269. assertStrictIDs(t, ids)
  270. })
  271. }
  272. func TestFutureResumeRejected(t *testing.T) {
  273. h := telemetry.NewHub(8, 8, 0, 0)
  274. _, _, _, err := h.SubscribeEventsPeer(1, "p")
  275. if !errors.Is(err, telemetry.ErrResumeOutOfRange) {
  276. t.Fatalf("empty ring resume 1: %v", err)
  277. }
  278. sub0, replay0, _, err := h.SubscribeEventsPeer(0, "empty0")
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. if len(replay0) != 0 {
  283. t.Fatal("empty ring resume 0 must not invent events")
  284. }
  285. sub0.Cancel()
  286. h.PublishSnapshot(snap(1), time.Unix(1, 0))
  287. sub, _, _, err := h.SubscribeEventsPeer(1, "p")
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. sub.Cancel()
  292. newest := uint64(1)
  293. subN, _, _, err := h.SubscribeEventsPeer(newest, "newest")
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. subN.Cancel()
  298. _, _, _, err = h.SubscribeEventsPeer(9, "p")
  299. if !errors.Is(err, telemetry.ErrResumeOutOfRange) {
  300. t.Fatalf("future resume: %v", err)
  301. }
  302. _, _, _, err = h.SubscribeSnapshotsPeer(99, "p")
  303. if !errors.Is(err, telemetry.ErrResumeOutOfRange) {
  304. t.Fatalf("future snapshot resume: %v", err)
  305. }
  306. }
  307. func TestResumeOlderThanOldestThenConcurrentPublish(t *testing.T) {
  308. h := telemetry.NewHub(4, 8, 0, 0)
  309. now := time.Unix(1, 0)
  310. for i := uint64(1); i <= 8; i++ {
  311. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  312. }
  313. sub, replay, _, err := h.SubscribeEventsPeer(1, "old")
  314. if err != nil {
  315. t.Fatal(err)
  316. }
  317. defer sub.Cancel()
  318. if len(replay) == 0 {
  319. t.Fatal("resume older than ring should still replay remaining events")
  320. }
  321. done := make(chan struct{})
  322. go func() {
  323. defer close(done)
  324. for i := uint64(9); i <= 16; i++ {
  325. h.PublishEvent(&hthv1.Event{EventId: i, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  326. }
  327. }()
  328. <-done
  329. _, _, _, err = h.SubscribeEventsPeer(100, "future-after")
  330. if !errors.Is(err, telemetry.ErrResumeOutOfRange) {
  331. t.Fatalf("future after concurrent publish: %v", err)
  332. }
  333. }
  334. func TestEventHubRenumbersNonIncreasingIDs(t *testing.T) {
  335. h := telemetry.NewHub(16, 8, 0, 0)
  336. now := time.Unix(1, 0)
  337. h.PublishEvent(&hthv1.Event{EventId: 5, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now)})
  338. if h.NewestEventID() != 5 {
  339. t.Fatalf("newest=%d", h.NewestEventID())
  340. }
  341. h.PublishEvent(&hthv1.Event{EventId: 5, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now), Message: "dup"})
  342. if h.NewestEventID() <= 5 {
  343. t.Fatalf("duplicate id must be renumbered, newest=%d", h.NewestEventID())
  344. }
  345. h.PublishEvent(&hthv1.Event{EventId: 1, Kind: hthv1.EventKind_EVENT_KIND_ACTION_RESULT, DeviceId: "d0", WallTime: timestamppb.New(now), Message: "back"})
  346. if h.NewestEventID() <= 5 {
  347. t.Fatalf("backward id must not rewind stream, newest=%d", h.NewestEventID())
  348. }
  349. }