|
| 1 | +package events |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log/slog" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// newTestHub returns a Hub backed by a buffered slog handler so tests can |
| 13 | +// inspect emitted warnings. |
| 14 | +func newTestHub(t *testing.T) (*Hub, *bytes.Buffer, *sync.Mutex) { |
| 15 | + t.Helper() |
| 16 | + var buf bytes.Buffer |
| 17 | + var mu sync.Mutex |
| 18 | + handler := slog.NewTextHandler(&lockedWriter{w: &buf, mu: &mu}, &slog.HandlerOptions{Level: slog.LevelDebug}) |
| 19 | + logger := slog.New(handler) |
| 20 | + return NewHub(logger), &buf, &mu |
| 21 | +} |
| 22 | + |
| 23 | +// lockedWriter serialises writes from goroutines that share the slog handler |
| 24 | +// so the buffer doesn't get torn output under -race. |
| 25 | +type lockedWriter struct { |
| 26 | + w *bytes.Buffer |
| 27 | + mu *sync.Mutex |
| 28 | +} |
| 29 | + |
| 30 | +func (l *lockedWriter) Write(p []byte) (int, error) { |
| 31 | + l.mu.Lock() |
| 32 | + defer l.mu.Unlock() |
| 33 | + return l.w.Write(p) |
| 34 | +} |
| 35 | + |
| 36 | +func countLines(buf *bytes.Buffer, mu *sync.Mutex, substr string) int { |
| 37 | + mu.Lock() |
| 38 | + defer mu.Unlock() |
| 39 | + return strings.Count(buf.String(), substr) |
| 40 | +} |
| 41 | + |
| 42 | +func TestSubscriberEventBufferIsLarge(t *testing.T) { |
| 43 | + hub, _, _ := newTestHub(t) |
| 44 | + hub.Start() |
| 45 | + defer hub.Stop() |
| 46 | + |
| 47 | + sub := hub.Subscribe("sub-1", "") |
| 48 | + if got := cap(sub.Events); got != subscriberEventBufferSize { |
| 49 | + t.Fatalf("subscriber event channel capacity = %d, want %d", got, subscriberEventBufferSize) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestRecordDropFirstDropLogsImmediately(t *testing.T) { |
| 54 | + hub, buf, mu := newTestHub(t) |
| 55 | + t0 := time.Date(2026, 5, 13, 12, 0, 0, 0, time.UTC) |
| 56 | + |
| 57 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0) |
| 58 | + |
| 59 | + if got := countLines(buf, mu, "subscriber event channel full"); got != 1 { |
| 60 | + t.Fatalf("expected 1 warning on first drop, got %d. log=%q", got, buf.String()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestRecordDropCoalescesWithinWindow(t *testing.T) { |
| 65 | + hub, buf, mu := newTestHub(t) |
| 66 | + t0 := time.Date(2026, 5, 13, 12, 0, 0, 0, time.UTC) |
| 67 | + |
| 68 | + // First drop logs. |
| 69 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0) |
| 70 | + // 99 more drops within the window — should not log again. |
| 71 | + for i := 1; i <= 99; i++ { |
| 72 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0.Add(time.Duration(i)*time.Millisecond)) |
| 73 | + } |
| 74 | + |
| 75 | + if got := countLines(buf, mu, "subscriber event channel full"); got != 1 { |
| 76 | + t.Fatalf("expected 1 coalesced warning, got %d. log=%q", got, buf.String()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestRecordDropEmitsAfterIntervalWithCount(t *testing.T) { |
| 81 | + hub, buf, mu := newTestHub(t) |
| 82 | + t0 := time.Date(2026, 5, 13, 12, 0, 0, 0, time.UTC) |
| 83 | + |
| 84 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0) |
| 85 | + for i := 1; i <= 49; i++ { |
| 86 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0.Add(time.Duration(i)*time.Millisecond)) |
| 87 | + } |
| 88 | + // Cross the interval: the next drop should log again, this time with |
| 89 | + // dropped=50 (the 49 coalesced + this one). |
| 90 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0.Add(dropLogInterval+time.Millisecond)) |
| 91 | + |
| 92 | + if got := countLines(buf, mu, "subscriber event channel full"); got != 2 { |
| 93 | + t.Fatalf("expected 2 warnings after crossing interval, got %d. log=%q", got, buf.String()) |
| 94 | + } |
| 95 | + if !strings.Contains(buf.String(), "dropped=50") { |
| 96 | + t.Fatalf("expected coalesced count of 50 in second warning, log=%q", buf.String()) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestRecordDropPerSubscriberPerEventTypeIsIndependent(t *testing.T) { |
| 101 | + hub, buf, mu := newTestHub(t) |
| 102 | + t0 := time.Date(2026, 5, 13, 12, 0, 0, 0, time.UTC) |
| 103 | + |
| 104 | + // Three distinct (subscriber, event_type) streams in the same instant — |
| 105 | + // each should produce its own first-drop warning. |
| 106 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0) |
| 107 | + hub.recordDropAt("sub-1", EventTypeMetricsUpdated, t0) |
| 108 | + hub.recordDropAt("sub-2", EventTypeLogsUpdated, t0) |
| 109 | + |
| 110 | + if got := countLines(buf, mu, "subscriber event channel full"); got != 3 { |
| 111 | + t.Fatalf("expected 3 independent first-drop warnings, got %d. log=%q", got, buf.String()) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestCleanupDropTrackerRemovesSubscriberEntries(t *testing.T) { |
| 116 | + hub, _, _ := newTestHub(t) |
| 117 | + t0 := time.Date(2026, 5, 13, 12, 0, 0, 0, time.UTC) |
| 118 | + |
| 119 | + hub.recordDropAt("sub-1", EventTypeLogsUpdated, t0) |
| 120 | + hub.recordDropAt("sub-1", EventTypeMetricsUpdated, t0) |
| 121 | + hub.recordDropAt("sub-2", EventTypeLogsUpdated, t0) |
| 122 | + |
| 123 | + if got := len(hub.subscriberDrops); got != 3 { |
| 124 | + t.Fatalf("expected 3 tracker entries, got %d", got) |
| 125 | + } |
| 126 | + |
| 127 | + hub.cleanupDropTracker("sub-1") |
| 128 | + |
| 129 | + if got := len(hub.subscriberDrops); got != 1 { |
| 130 | + t.Fatalf("expected 1 tracker entry after cleanup, got %d", got) |
| 131 | + } |
| 132 | + if _, ok := hub.subscriberDrops[dropKey{subID: "sub-2", eventType: EventTypeLogsUpdated}]; !ok { |
| 133 | + t.Fatal("sub-2 entry should have survived cleanup of sub-1") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestSlowSubscriberDropsAreCoalesced(t *testing.T) { |
| 138 | + // End-to-end through the broadcast loop: a subscriber that never drains |
| 139 | + // will overflow, but we should still emit only one warning per event-type |
| 140 | + // during the burst (no clock fast-forwarding here, so we stay inside |
| 141 | + // dropLogInterval). |
| 142 | + hub, buf, mu := newTestHub(t) |
| 143 | + hub.Start() |
| 144 | + defer hub.Stop() |
| 145 | + |
| 146 | + sub := hub.Subscribe("slow-sub", "") |
| 147 | + // Never read sub.Events. |
| 148 | + _ = sub |
| 149 | + |
| 150 | + // Publish well past the buffer size so a flood of drops occurs. |
| 151 | + for i := 0; i < subscriberEventBufferSize*3; i++ { |
| 152 | + hub.Publish(Event{Type: EventTypeLogsUpdated}) |
| 153 | + } |
| 154 | + |
| 155 | + // Let the broadcast loop drain. |
| 156 | + deadline := time.Now().Add(2 * time.Second) |
| 157 | + for time.Now().Before(deadline) { |
| 158 | + if countLines(buf, mu, "subscriber event channel full") >= 1 { |
| 159 | + break |
| 160 | + } |
| 161 | + time.Sleep(10 * time.Millisecond) |
| 162 | + } |
| 163 | + |
| 164 | + got := countLines(buf, mu, "subscriber event channel full") |
| 165 | + if got == 0 { |
| 166 | + t.Fatalf("expected at least one drop warning under burst, got none. log=%q", buf.String()) |
| 167 | + } |
| 168 | + // Under the throttle, a single burst should produce far fewer warnings |
| 169 | + // than dropped events. Allow some slack but assert it's not 1-per-drop. |
| 170 | + if got > 5 { |
| 171 | + t.Fatalf("expected coalesced warning(s), got %d (no throttling?). log=%q", got, buf.String()) |
| 172 | + } |
| 173 | +} |
0 commit comments