From fd7a8e00be333e887719ee6a0e1c7666d400ecf7 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 10 Aug 2026 13:29:41 +0200 Subject: [PATCH] Fix flaky TestKeepAlive_ConcurrentFramesNoInterleaving The comments>0 assertion raced 1600 in-memory writes against a 1ms ticker and lost deterministically on fast machines. Split it into a separate test that waits for a tick via require.Eventually instead of tightening the timing. Fixes #219 --- mcpcompat/server/keepalive_internal_test.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mcpcompat/server/keepalive_internal_test.go b/mcpcompat/server/keepalive_internal_test.go index baef883..da68af7 100644 --- a/mcpcompat/server/keepalive_internal_test.go +++ b/mcpcompat/server/keepalive_internal_test.go @@ -360,7 +360,26 @@ func TestKeepAlive_ConcurrentFramesNoInterleaving(t *testing.T) { } } assert.Equal(t, writers*framesPerWriter, frames, "every whole frame must appear intact") - assert.Positive(t, comments, "the ticker should have emitted at least one comment") + // The ticker firing at least once is covered by TestKeepAlive_TickerEmitsComment, + // which waits deterministically instead of racing wall-clock time here. + _ = comments +} + +// TestKeepAlive_TickerEmitsComment asserts the ticker emits a keep-alive +// comment, waiting deterministically for evidence of a tick instead of +// racing wall-clock time against a fixed amount of work. +func TestKeepAlive_TickerEmitsComment(t *testing.T) { + t.Parallel() + rw := newRecordingWriter() + rw.setSSEHeaders() + k := newKeepAliveWriter(rw, time.Millisecond, nil) + defer k.stopKeepAlive() + + k.WriteHeader(http.StatusOK) + + require.Eventually(t, func() bool { + return rw.commentCount() > 0 + }, time.Second, time.Millisecond, "the ticker should have emitted at least one comment") } func TestKeepAlive_DisabledInterval(t *testing.T) {