fix(server): 修复 TestSubscribeTask_ConcurrentSafety 数据竞争问题
- 修复多个 goroutine 并发 append 同一 slice 导致的 DATA RACE - 使用预分配 slice + 索引赋值替代并发 append - 通过 go test -race 验证
This commit is contained in:
@@ -354,14 +354,14 @@ func TestSubscribeTask_ConcurrentSafety(t *testing.T) {
|
||||
const goroutines = 50
|
||||
|
||||
// 并发订阅
|
||||
cancels := make([]func(), 0, goroutines)
|
||||
cancels := make([]func(), goroutines)
|
||||
for i := 0; i < goroutines; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
_, cancel := svc.SubscribeTask(taskID)
|
||||
cancels = append(cancels, cancel)
|
||||
}()
|
||||
cancels[idx] = cancel
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user