mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-24 16:05:44 +08:00
fix: race condition in TestOpsSystemLogSink_StartStopAndFlushSuccess
done 信号在 BatchInsertSystemLogsFn 内发出,但 writtenCount 在函数返回后 才原子递增,导致测试主 goroutine 收到信号后立即检查 WrittenCount 时可能为 0。 改用短轮询等待 WrittenCount > 0,消除竞态。
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user