|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.uber.org/zap" |
| 12 | + |
| 13 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/contracts" |
| 14 | +) |
| 15 | + |
| 16 | +// Spec 047 — Phase 5 (US3): coalesce servers.changed bursts to ≤ 1 publish per |
| 17 | +// interval window, last-write-wins. |
| 18 | + |
| 19 | +func newCoalescerTestRuntime(t *testing.T) *Runtime { |
| 20 | + t.Helper() |
| 21 | + logger, err := zap.NewDevelopment() |
| 22 | + require.NoError(t, err) |
| 23 | + rt := &Runtime{ |
| 24 | + logger: logger, |
| 25 | + eventSubs: make(map[chan Event]struct{}), |
| 26 | + managementService: &fakeServersLister{servers: []*contracts.Server{{Name: "s1"}}, stats: &contracts.ServerStats{}}, |
| 27 | + } |
| 28 | + rt.coalescer = newServersChangedCoalescer(rt, 50*time.Millisecond) |
| 29 | + return rt |
| 30 | +} |
| 31 | + |
| 32 | +func collectEvents(ch <-chan Event, window time.Duration) []Event { |
| 33 | + deadline := time.After(window) |
| 34 | + var out []Event |
| 35 | + for { |
| 36 | + select { |
| 37 | + case evt, ok := <-ch: |
| 38 | + if !ok { |
| 39 | + return out |
| 40 | + } |
| 41 | + if evt.Type == EventTypeServersChanged { |
| 42 | + out = append(out, evt) |
| 43 | + } |
| 44 | + case <-deadline: |
| 45 | + return out |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestCoalescer_CollapsesBurstToSingleEvent(t *testing.T) { |
| 51 | + rt := newCoalescerTestRuntime(t) |
| 52 | + ctx, cancel := context.WithCancel(context.Background()) |
| 53 | + defer cancel() |
| 54 | + rt.coalescer.start(ctx) |
| 55 | + |
| 56 | + ch := rt.SubscribeEvents() |
| 57 | + defer rt.UnsubscribeEvents(ch) |
| 58 | + |
| 59 | + // Fire 100 rapid updates within 10 ms. |
| 60 | + for i := 0; i < 100; i++ { |
| 61 | + rt.emitServersChanged("burst", map[string]any{"i": i}) |
| 62 | + } |
| 63 | + |
| 64 | + // Wait > 1 interval window for the drainer to publish. |
| 65 | + events := collectEvents(ch, 200*time.Millisecond) |
| 66 | + assert.LessOrEqual(t, len(events), 1, "expected ≤ 1 published event after burst, got %d", len(events)) |
| 67 | + if len(events) == 1 { |
| 68 | + // Last submitted i was 99. |
| 69 | + assert.Equal(t, 99, events[0].Payload["i"], "last-write-wins: expected i=99") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestCoalescer_LastWriteWins(t *testing.T) { |
| 74 | + rt := newCoalescerTestRuntime(t) |
| 75 | + ctx, cancel := context.WithCancel(context.Background()) |
| 76 | + defer cancel() |
| 77 | + rt.coalescer.start(ctx) |
| 78 | + |
| 79 | + ch := rt.SubscribeEvents() |
| 80 | + defer rt.UnsubscribeEvents(ch) |
| 81 | + |
| 82 | + rt.emitServersChanged("first", nil) |
| 83 | + rt.emitServersChanged("middle", nil) |
| 84 | + rt.emitServersChanged("last", nil) |
| 85 | + |
| 86 | + events := collectEvents(ch, 200*time.Millisecond) |
| 87 | + require.Len(t, events, 1, "expected exactly 1 coalesced event") |
| 88 | + assert.Equal(t, "last", events[0].Payload["reason"]) |
| 89 | +} |
| 90 | + |
| 91 | +func TestCoalescer_FlushesOnShutdown(t *testing.T) { |
| 92 | + rt := newCoalescerTestRuntime(t) |
| 93 | + ctx, cancel := context.WithCancel(context.Background()) |
| 94 | + rt.coalescer.start(ctx) |
| 95 | + |
| 96 | + ch := rt.SubscribeEvents() |
| 97 | + defer rt.UnsubscribeEvents(ch) |
| 98 | + |
| 99 | + // Submit one event, immediately cancel — drainer must flush before exiting. |
| 100 | + rt.emitServersChanged("right-before-shutdown", nil) |
| 101 | + cancel() |
| 102 | + |
| 103 | + events := collectEvents(ch, 500*time.Millisecond) |
| 104 | + require.GreaterOrEqual(t, len(events), 1, "expected at least 1 event flushed on shutdown") |
| 105 | + assert.Equal(t, "right-before-shutdown", events[0].Payload["reason"]) |
| 106 | +} |
| 107 | + |
| 108 | +func TestCoalescer_NoStarvationOnSingleEvent(t *testing.T) { |
| 109 | + rt := newCoalescerTestRuntime(t) |
| 110 | + ctx, cancel := context.WithCancel(context.Background()) |
| 111 | + defer cancel() |
| 112 | + rt.coalescer.start(ctx) |
| 113 | + |
| 114 | + ch := rt.SubscribeEvents() |
| 115 | + defer rt.UnsubscribeEvents(ch) |
| 116 | + |
| 117 | + rt.emitServersChanged("solo", nil) |
| 118 | + |
| 119 | + events := collectEvents(ch, 250*time.Millisecond) |
| 120 | + require.Len(t, events, 1, "single event must publish within ~1 interval window") |
| 121 | + assert.Equal(t, "solo", events[0].Payload["reason"]) |
| 122 | +} |
| 123 | + |
| 124 | +func TestCoalescer_FlushNowSynchronousHook(t *testing.T) { |
| 125 | + // flushNow lets tests force a publish without sleeping. Useful for the |
| 126 | + // deterministic harness below: we submit, flushNow, assert immediately. |
| 127 | + rt := newCoalescerTestRuntime(t) |
| 128 | + |
| 129 | + ch := rt.SubscribeEvents() |
| 130 | + defer rt.UnsubscribeEvents(ch) |
| 131 | + |
| 132 | + rt.emitServersChanged("instant", nil) |
| 133 | + rt.coalescer.flushNow() |
| 134 | + |
| 135 | + select { |
| 136 | + case evt := <-ch: |
| 137 | + assert.Equal(t, "instant", evt.Payload["reason"]) |
| 138 | + case <-time.After(100 * time.Millisecond): |
| 139 | + t.Fatal("flushNow should have caused immediate publish") |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestCoalescer_ConcurrentSubmittersAreSafe(t *testing.T) { |
| 144 | + rt := newCoalescerTestRuntime(t) |
| 145 | + ctx, cancel := context.WithCancel(context.Background()) |
| 146 | + defer cancel() |
| 147 | + rt.coalescer.start(ctx) |
| 148 | + |
| 149 | + ch := rt.SubscribeEvents() |
| 150 | + defer rt.UnsubscribeEvents(ch) |
| 151 | + |
| 152 | + var wg sync.WaitGroup |
| 153 | + for i := 0; i < 20; i++ { |
| 154 | + wg.Add(1) |
| 155 | + go func(i int) { |
| 156 | + defer wg.Done() |
| 157 | + for j := 0; j < 50; j++ { |
| 158 | + rt.emitServersChanged("concurrent", map[string]any{"goroutine": i, "j": j}) |
| 159 | + } |
| 160 | + }(i) |
| 161 | + } |
| 162 | + wg.Wait() |
| 163 | + |
| 164 | + // 20 × 50 = 1000 submissions in << 1 interval. After the first window |
| 165 | + // elapses and a follow-up window, expect a small bounded number of events. |
| 166 | + events := collectEvents(ch, 200*time.Millisecond) |
| 167 | + assert.LessOrEqual(t, len(events), 5, "expected coalesced result; got %d events", len(events)) |
| 168 | +} |
0 commit comments