fix: race condition in TestOpsSystemLogSink_StartStopAndFlushSuccess

done 信号在 BatchInsertSystemLogsFn 内发出,但 writtenCount 在函数返回后
才原子递增,导致测试主 goroutine 收到信号后立即检查 WrittenCount 时可能为 0。
改用短轮询等待 WrittenCount > 0,消除竞态。
This commit is contained in:
erio
2026-03-06 13:02:04 +08:00
parent 896fb2b0b4
commit 180c515f9a
@@ -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")