From 180c515f9a7aceabb77057d1ffe19ed2cf2addd2 Mon Sep 17 00:00:00 2001 From: erio Date: Fri, 6 Mar 2026 13:02:04 +0800 Subject: [PATCH] fix: race condition in TestOpsSystemLogSink_StartStopAndFlushSuccess MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit done 信号在 BatchInsertSystemLogsFn 内发出,但 writtenCount 在函数返回后 才原子递增,导致测试主 goroutine 收到信号后立即检查 WrittenCount 时可能为 0。 改用短轮询等待 WrittenCount > 0,消除竞态。 --- backend/internal/service/ops_system_log_sink_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/internal/service/ops_system_log_sink_test.go b/backend/internal/service/ops_system_log_sink_test.go index 12a2ec0c7d..137ee33c72 100644 --- a/backend/internal/service/ops_system_log_sink_test.go +++ b/backend/internal/service/ops_system_log_sink_test.go @@ -183,6 +183,15 @@ func TestOpsSystemLogSink_StartStopAndFlushSuccess(t *testing.T) { if strings.TrimSpace(item.Message) == "" { t.Fatalf("message should not be empty") } + // writtenCount is incremented after BatchInsertSystemLogsFn returns, + // so poll briefly to avoid a race between the done signal and the atomic add. + deadline := time.Now().Add(time.Second) + for time.Now().Before(deadline) { + if sink.Health().WrittenCount > 0 { + break + } + time.Sleep(time.Millisecond) + } health := sink.Health() if health.WrittenCount == 0 { t.Fatalf("written_count should be >0")