TestMemoryStability 测试内存稳定性(防止泄漏)
(t *testing.T)
| 281 | |
| 282 | // TestMemoryStability 测试内存稳定性(防止泄漏) |
| 283 | func TestMemoryStability(t *testing.T) { |
| 284 | config := &EventBusConfig{ |
| 285 | MaxTimelineSize: 1000, |
| 286 | MaxTimelineAge: 0, |
| 287 | CleanupInterval: 50 * time.Millisecond, |
| 288 | } |
| 289 | eb := NewEventBusWithConfig(config) |
| 290 | defer eb.Close() |
| 291 | |
| 292 | // 分批发送事件,给清理 worker 时间工作 |
| 293 | batches := 100 |
| 294 | eventsPerBatch := 100 |
| 295 | |
| 296 | for batch := range batches { |
| 297 | // 快速发送一批事件 |
| 298 | for i := range eventsPerBatch { |
| 299 | eb.EmitProgress(&types.ProgressTextChunkEvent{ |
| 300 | Step: batch*eventsPerBatch + i, |
| 301 | Delta: "test data", |
| 302 | }) |
| 303 | } |
| 304 | |
| 305 | // 每批之后给清理 worker 一些时间 |
| 306 | if batch%10 == 0 { |
| 307 | time.Sleep(100 * time.Millisecond) |
| 308 | count := eb.GetTimelineCount() |
| 309 | // 应该在合理范围内 |
| 310 | if count > 1500 { |
| 311 | t.Errorf("timeline growing too large: %d at batch %d", count, batch) |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // 等待最后一次清理 |
| 317 | time.Sleep(200 * time.Millisecond) |
| 318 | |
| 319 | // 最终检查 |
| 320 | finalCount := eb.GetTimelineCount() |
| 321 | if finalCount > 1100 { |
| 322 | t.Errorf("final timeline count too large: %d (expected <= 1100)", finalCount) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // TestUnsubscribeMultipleChannels 测试多通道取消订阅 |
| 327 | func TestUnsubscribeMultipleChannels(t *testing.T) { |
nothing calls this directly
no test coverage detected